CSS定位/颜色
我想要一个全灰色的背景,里面有一个较小的蓝色背景。
类似于
<body>
<div id= "background_1">
<div id= "background_2">
</div>
</div>
</body>
css 中的然后
#background_1
{
background-color : gray;
z-index : 1;
}
#background_2
{
background-color : blue;
z-index : 2;
}
有人可以帮我解决这个问题吗?我知道这有点模糊,但我基本上想要一个全灰色的背景,前景中有一个较小的蓝色矩形。
I would like to have an all gray background, with a smaller blue background inside it.
Something like
<body>
<div id= "background_1">
<div id= "background_2">
</div>
</div>
</body>
and then in the css
#background_1
{
background-color : gray;
z-index : 1;
}
#background_2
{
background-color : blue;
z-index : 2;
}
Can anyone help me with this? I know it is a bit vague, but I basically want an all gray background, with a smaller blue rectangle in the foreground.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码一切正常...但不需要 z-index 。
也许,将一些内容放入您的 div 中,或者修复它们的大小
编辑您的其他问题
您可以在此处查看结果:http://jsfiddle.net/Nz5WK/1/
Everything is ok with your code... but no need of z-index for that.
Perhaps, put some content into your divs, or fix their sizes
Edit regarding your other questions
You can see the result here : http://jsfiddle.net/Nz5WK/1/
z-index
仅适用于位置:相对
、固定
或绝对
。另外,如果它看起来很旧,请考虑使用
border
:z-index
only works onposition: relative
,fixed
, orabsolute
.Also, if it's old for looks, consider using
border
:您可以通过将初始灰色背景应用于正文来消除其中一个分隔线:
请注意,在这种情况下,灰色部分的宽度将为 8px,由正文的默认边距定义。
或者,如果您希望蓝色部分占据整个屏幕,周围有灰色“边框”,您甚至不需要划分:
You can eliminate one of the divisions by applying the initial grey background to the body:
Note that in this case, the grey part will be 8px wide, as defined by the body's default margin.
Or, if you want the blue part to take up the entire screen with a grey "border" around it, you don't even need the division: