[英]Trying to cover 100% of parent div width (excluding padding) between 2 child divs.
到目前為止,這是我的CSS。 似乎父div的左填充很好(即不被子div覆蓋),但其右填充被子div覆蓋。 有什么想法嗎?
.div_parent {
position: absolute;
height: 720px;
width: 600px;
top: 30px;
background-color: blue;
padding-left: 10px;
padding-right: 10px;
}
.div_child1 {
position: absolute;
height: inherit;
width: 15%;
background-color: tan;
}
.div_child2 {
position: absolute;
height: inherit;
width: 85%;
left: 15%;
background-color: green;
}
這也是我的html:
<html>
<head>
<link rel='stylesheet' type='text/css' href='calendar.css' >
<script src='calendar.js'></script>
</head>
<body>
<div class = 'div_parent'>
<div class = 'div_child1'>
<h1>test</h1>
<p>test2</p>
<p>test3</p>
</div>
<div class = 'div_child2'>
</div>
</div>
</body>
</html>
嗯,問題是您沒有給第一個孩子“左”,並且默認情況下是自動的,而對於第二個孩子,您給了15%。 同樣,在計算孩子的寬度時,您還必須考慮父母沒有的填充。 如果將子項的總寬度相加,則其等於父項的寬度,即620,因此通過計算,父級div中沒有足夠的空間來讓它們兩個並排堆疊。 解決此問題的一種更好的方法是使用左浮動塊div或更好地使用calc()解決填充問題。
.child-1{
width:calc(15% - 10px);
}
.child-2{
width:calc(85% - 10px);
}
使用float:left; 在兩個div上都離開左側:15%; 位置:絕對;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.