将图像放在背景前

时间:2013-06-20 18:36:24

标签: jquery html css

我有一个滑入的部分以蓝色背景查看。我试图在左上角放一张照片,但它几乎看不到,蓝色背景消失了。

HTML的一部分:

<header id="main">
    <a href="#" id="stackoverflow" class="icons"></a>
</header>
<section id="stack" class="news"></section>

jQuery的:

$('#stackoverflow').click(function(){
    $('#stack').toggle('slow', $st);
    $('#stack').css({'background': 'url(stack.jpeg) no-repeat', 'position':'absolute', 'z-index':'1'});
});

CSS:

#stack{
float: left;
}

.news{ 
position: absolute;
z-index: -1; 
width: 600px;
background-color: #0000FF;
padding: 10px;
border-radius: 10px;
box-shadow: 3px 2px 3px #000;
}

.news a{
position: absolute;
text-decoration: none;
color: #fff;
}

.news a:hover{
color: #dc692e;
}

.news td{
padding: 5px 10px;
border-bottom: 2px #fff solid;
}

如果没有我尝试将图像放在角落里,这就是它的样子:enter image description here

当我尝试在角落添加图片时,这是一张图片:enter image description here

谢谢!

1 个答案:

答案 0 :(得分:4)

蓝色背景消失的原因是你覆盖了实际的背景。你应该能够得到你所追求的东西:

$('#stackoverflow').click(function(){
    $('#stack').toggle('slow', $st);
    $('#stack').css({'background': 'url(stack.jpeg) no-repeat BLUE', 'position':'absolute', 'z-index':'1'});
}); 

另外要添加,因为您将图像作为背景放置在此处,我建议在所述元素的顶部添加一些填充,以使文本不会覆盖您的图像。

更好的解决方案是将所述图像实际注入特定元素。 IE:

$('#stack').prepend('<img id="theImg" src="theImg.jpg" />')