[英]Centered div, with divs on either side that extend to page margin
我想要一個居中的div,每側的div延伸到頁面邊距。 必須有一種更有效的方法來執行此操作。
目前,我有兩個div,每個div具有49%的寬度,然后在它們下面有一個居中的div,我將其移到其頂部以創建效果。
問題是,這在下面創建了一個尷尬的空間。
HTML:
<div id="green"></div>
<div id="red"></div>
<div id="blue"></div>
CSS:
#green {
height:25em;
width:49.99%;
background-color:#132a10;
display:inline-block;
margin:0;
float:left;
opacity:.47;
}
#red {
height:25em;
width:49%;
background-color:#400120;
display:inline-block;
margin:0;
opacity:.4;
}
#blue {
background-color:#436a97;
position:relative;
bottom:26em;
width:22em;
height:27em;
margin-right:auto;
margin-left:auto;
z-index:1;
opacity:;
}
差距來自在藍色div上使用相對定位。 通過相對定位,元素的原始位置仍然占據空間,並且相對於元素重新定位元素。 您可以使用絕對定位以及以下規則來修復它:
#blue {
background-color:#436a97;
position:absolute;
width:6em;
height:7em;
margin:auto;
top:0;
left:0;
right:0;
}
將所有div設置為相同的高度,並為藍色div的上邊距設置-16px
<html>
<head>
<style>
#green
{
height:7em;
width:49.99%;
background-color:#132a10;
display:inline-block;
margin:0;
float:left;
opacity:.47;
}
#red
{
height:7em;
width:49%;
background-color:#400120;
display:inline-block;
margin:0;
opacity:.4;
}
#blue
{
background-color:#436a97;
position:relative;
bottom:6em;
width:6em;
height:7em; /*edit*/
margin-right:auto;
margin-left:auto;
margin-top:-16px; /*edit*/
z-index:1;
opacity:;
}
</style>
</head>
<body>
<div id="green"></div>
<div id="red"></div>
<div id="blue"></div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.