因此,我的目標是創建5個彼此相鄰的矩形,無論您如何重新調整瀏覽器大小,它都以左,右,上,下居中。重疊的css矩形
<body>
<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>
<div id="test4"></div>
<div id="test5"></div>
</body>
#test1 {
background-color:blue;
width:200px;
height:40px;
margin:auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
float:left;
}
#test2 {
background-color:black;
width:200px;
height:40px;
margin:auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 25%;
float:left;
}
#test3 {
background-color:gray;
width:200px;
height:40px;
margin:auto;
position: absolute;
top: 0; left: 25%; bottom: 0; right: 0;
float:left;
}
#test4 {
background-color:yellow;
width:200px;
height:40px;
margin:auto;
position: absolute;
top: 0; left: 50%; bottom: 0; right: 0;
float:left;
}
#test5{
background-color:orange;
width:200px;
height:40px;
margin:auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 50%;
float:left;
}
這是我到目前爲止的代碼,它幾乎可行。但矩形在某個瀏覽器窗口寬度處開始重疊。我認爲它可以將寬度更改爲每個矩形的百分比,但是如果它們都處於相同的百分比,則它們只是彼此重疊。預先感謝希望有人能幫助我更多地理解這一點。
What it looks like with maximized browser
What I wand to avoid when the browser gets too small
永遠不要將'float'與'position:absolute'結合起來 - 這是無稽之談。 –
忘記刪除這些行後,我發現了。沒有改變任何東西,這可能是我忘了的原因。固定。 – user3195984
如果你給它們絕對的寬度和高度,你的元素將會重疊。你必須給他們最小寬度/最大寬度和類似的東西。我會建議將所有寬度更改爲最大寬度,併爲您的所有元素提供一個百分比。 – ntgCleaner