gpt4 book ai didi

html - 如何在不破坏的情况下定位 CSS

转载 作者:太空宇宙 更新时间:2023-11-04 16:26:10 25 4
gpt4 key购买 nike

我有 2 个应该相互重叠的背景,以及一个应该与这 2 个背景重叠的容器。我无法让它工作,即使我指定了一个位置,容器也会被推下。

CSS:

body { 
background:#1b1b1b;
padding:0; margin:0;
color:white;
text-align:center;
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
}
.map {
background:url(../images/map.png) no-repeat top center;
width:1280px;
height:335px;
margin-left:auto;
margin-right:auto;
position:absolute;
}
.map-bg {
background:url(../images/map-bg.png) repeat-x;
height:336px;
position:relative;
}
.stripe {
background:url(../images/stripe.png) repeat-x;
height:35px;
width:100%;
position:absolute;
margin-top:158px;
}
#container {
margin-left:auto;
margin-right:auto;
width:924px;
height:100%;
text-align:left;
}

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Essbee</title>
<link type="text/css" rel="stylesheet" href="css/main.css" />
</head>

<body>

<div class="bg">
<div class="map"></div>
<div class="map-bg"></div>
</div>

<div class="stripe"></div>

<div id="container">
<div id="header"></div>
<div id="content"></div>
<div id="footer">Footer</div>
</div>

</body>
</html>

最佳答案

这里的问题是您的 HTML 结构。据我所知,您希望将四个元素相互叠加呈现,.map、.map-bg.stripe#容器。您拥有 HTML 和 CSS 的方式不会发生这种情况,因为这些元素在树中处于不同级别。你应该做的是将所有内容都包装在.bg中,并将它们的所有位置设置为absolute,并将它们的父容器.bg设置为position:relative。在空元素中放置不间断空格   始终是一个好习惯,因为许多浏览器不会按预期呈现完全空的 block 级元素。

<body>

<div class="bg">
<div class="map">&nbsp;</div>
<div class="map-bg">&nbsp;</div>

<div class="stripe">&nbsp;</div>

<div id="container">
<div id="header">&nbsp;</div>
<div id="content">&nbsp;</div>
<div id="footer">Footer</div>
</div>

</div>

</body>

更新的 CSS:

body { 
background:#1b1b1b;
padding:0; margin:0;
color:white;
text-align:center;
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
}
.bg {
position: relative;
}
.map {
background:url(../images/map.png) no-repeat top center;
width:1280px;
height:335px;
margin-left:auto;
margin-right:auto;
position:absolute;
}
.map-bg {
background:url(../images/map-bg.png) repeat-x;
height:336px;
position: absolute;
}
.stripe {
background:url(../images/stripe.png) repeat-x;
height:35px;
width:100%;
position:absolute;
margin-top:158px;
}
#container {
position: absolute;
margin-left:auto;
margin-right:auto;
width:924px;
height:100%;
text-align:left;
}

尝试一下,您应该会看到更符合我认为您正在寻找的结果。

关于html - 如何在不破坏的情况下定位 CSS <div>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5491433/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com