[英]Simple Z-Index not working
I am trying to place my header <h1>
over a div class named light
which contains a background image named light.png
. 我试图将标题
<h1>
放在名为light
的div类上,该类包含名为light.png
的背景图像。 I have stacked them using Z-index
and added position:relative
, but still <h1>
is not placed over <div class=light>
. 我已经使用
Z-index
堆叠了它们,并添加了position:relative
,但是<h1>
仍然没有放在<div class=light>
。
What am I doing wrong? 我究竟做错了什么? Here is my html code :
这是我的html代码 :
<body>
<div class="light"></div>
<h1>sdfdsf</h1>
</body>
css code: CSS代码:
body {
background-image: url(images/bg.jpg);
background-repeat: repeat;
z-index:1;
}
.light {
background-image: url(images/light.png);
margin-left:auto;
margin-right:auto;
z-index:2;
width:763px;
height:566px;
position:relative;
}
h1 {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 48px;
color: rgba(255,255,255,1);
position:relative;
z-index:5;
}
All you have to do is to place your <h1>
tag inside the <div>
tag. 您要做的就是将您的
<h1>
标签放在<div>
标签内。
<div class="light">
<h1>sdfdsf</h1>
</div>
Live example: Tinkerbin 现场示例: Tinkerbin
I modified your background color to show the example . 我修改了背景色以显示示例 。
You don't need to use z-index
for this. 您不需要为此使用
z-index
。
Here is an exmaple of what I said in my comment: 这是我在评论中说过的话的典范:
html code: html代码:
<body>
<div class='outer'>
<div class="light"></div>
<h1>sdfdsf</h1>
</div>
</body>
css code: CSS代码:
body {
background-image: url(images/bg.jpg);
background-repeat: repeat;
}
.outer{
position:relative;
}
.light {
background-image: url(images/light.png);
margin-left:auto;
margin-right:auto;
width:763px;
height:566px;
position:absolute;
}
h1 {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 48px;
color: rgba(255,255,255,1);
position:absolute;
z-index:99999999999;
top:0;
left:0;
}
You can use all posiiton relatives but it is not fully browser compliant due to CSS
bugs so I prefer to do this way since this will look ok on older browsers too, not just the new ones. 您可以使用所有posiiton亲戚,但由于
CSS
错误,它不能完全兼容浏览器,因此我更喜欢这样做,因为在较旧的浏览器上,不仅是新的浏览器,这看起来还可以。
There is no need to assign z-index
to the body 无需将
z-index
分配给主体
HTML - HTML-
<div class="light"></div>
<h1>hi</h1>
CSS - CSS-
.light {
background-image: url(http://www.ostpl.com/Gallery/web-hosting.jpg);
margin-left:auto;
margin-right:auto;
z-index:1;
width:763px;
height:566px;
border: 1px solid #f00;
position: relative;
}
h1 {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 48px;
color: rgba(0,0,0,1);
position:absolute;
left: 0;
top: 0;
z-index:2;
}
put <h1>
tag inside the div light
将
<h1>
标签放在div light
<body>
<div class="light">
<h1>sdfdsf</h1>
</div>
</body>
use absolute instead 使用绝对代替
<style type="text/css" media="screen">
body {
background-color: black;
background-repeat: repeat;
z-index:1;
}
.light {
background-color: yellow;
margin-left:auto;
margin-right:auto;
z-index:2;
width:763px;
height:566px;
position:absolute;
}
h1 {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 48px;
color: rgba(255,255,255,1);
position:absolute;
z-index:5;
}
</style>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.