CSS子元素跟父元素的高度一致的實現(xiàn)方法

發(fā)布時間:2024-02-04
這篇文章主要介紹了css子元素跟父元素的高度一致的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
絕對定位方法:
(1)將父元素設(shè)置為相對定位,不寫父元素的高度時,會隨著左邊的子元素高度變化而變化
.parent {
/*其他樣式*/
width: 800px;
color: #fff;
font-family: \”microsoft yahei\”;
text-align: center;
}
(2)左邊一個元素有個最小高度的情況
.left {
min-height: 700px;
width: 600px;
}
(3)右邊元素要想跟父元素的高度是一致,那么可以用絕對定位這樣設(shè)置,如果不想同時寫top和bottom,寫一個時,再寫上height:100%,也可以達(dá)到一樣的效果
.right {
/*其他樣式*/
}
(4)完整例子代碼:
<!doctype html>
<html >
<head>
<meta charset=\”utf-8\”>
<title>子元素高度與父元素一致</title>
<style>
.parent{
position: relative;
background: #f89;
width: 800px;
color: #fff;
font-family: \”microsoft yahei\”;
text-align: center;
}
.left {
min-height: 700px;
width: 600px;
}
.right {
width: 200px;
position: absolute;
top: 0;
right: 0;
bottom: 0;
background: #ccc;
}
</style>
</head>
<body>
<div class=\”parent\”>
<div class=\”left\”>
左側(cè) left 不定高,parent的高度隨著左側(cè)left 的高度變化而變化,右側(cè)也跟著變
</div>
<div class=\”right\”>
這邊的高度跟父元素高度一致
</div>
</div>
</body>
</html>
左側(cè) left 不定高,parent的高度隨著左側(cè)left 的高度變化而變化,右側(cè)也跟著變
這邊的高度跟父元素高度一致
(5)效果
(6)問題來了:
如果右側(cè)的子元素高度超出了.parent,怎么辦?
right的子元素,高度為1024px,會撐破容器,給.right加上 overflow:auto 就防止溢出了
.right-inner {
background: limegreen;
height: 1024px;
}
效果圖如下:
完整代碼:
<!doctype html>
<html >
<head>
<meta charset=\”utf-8\”>
<title>子元素高度與父元素一致</title>
<style>
.parent{
position: relative;
background: #f89;
width: 800px;
color: #fff;
font-family: \”microsoft yahei\”;
text-align: center;
}
.left {
min-height: 700px;
width: 600px;
}
.right {
width: 200px;
position: absolute;
top: 0;
right: 0;
height: 100%;
overflow: auto;
background: #ccc;
}
.right-inner {
background: limegreen;
height: 1024px;
}
</style>
</head>
<body>
<div class=\”parent\”>
<div class=\”left\”>
左側(cè) left 不定高,parent的高度隨著左側(cè)left 的高度變化而變化,右側(cè)也跟著變
</div>
<div class=\”right\”>
<div class=\”right-inner\”>right的子元素,高度為1024px,會撐破容器,給.right加上 overflow:auto 就防止溢出了</div>
</div>
</div>
</body>
</html>
左側(cè) left 不定高,parent的高度隨著左側(cè)left 的高度變化而變化,右側(cè)也跟著變
right的子元素,高度為1024px,會撐破容器,給.right加上 overflow:auto 就防止溢出了
(7)其他資源
http://stackoverflow.com/questions/3049783/how-to-make-a-floated-div-100-height-of-its-parent
文章來源:腳本之家,原文鏈接:https://www.jb51.net/css/743410.html
上一個:小米note基帶怎么恢復(fù),紅米note增強(qiáng)版基帶刷掉了怎么辦
下一個:房產(chǎn)證拿到后多久可以賣房

茶的功效你都知道?
江浙滬最好玩的主題樂園有哪些
華為手寫筆使用技巧(華為手寫筆功能演示)
dr.breit閥門
m2固態(tài)硬盤打游戲怎么樣,固態(tài)硬盤玩網(wǎng)絡(luò)游戲傷嗎
普洱古樹茶與臺地茶鑒別
玉竹主要病蟲害及綜合防治措施
北京文博交流館怎么樣+地址+交通
qc30芯片,這是塊什么芯片
為什么百天多肉容易死亡?
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos