绝对定位方法:
(1)将父元素设置为相对定位,不写父元素的高度时,会随着左边的子元素高度变化而变化
.pare两个一百nt {/*关键代码*/position: relative;/*其他样式*/width: 800px;color: #fff;font-family: "microsoft yahei";text-align: center;}
(2)左边一个元素有个最小高度的情况
.left {min-height: 700px;width: 600px;}
(3)右边元素要想跟父元素的高度是一致,那么可以用绝对定位这样设置,如果不想同时写top和bottom,写一个时,再写上height:100%,也可以龙潭湖公园达到一样的效果
.right {/*关键代码*/width: 200px;position: absolute;top: 0;right: 0;bottom: 0;/*其他样式*/background: #ccc;}
(4)完整例子代码:
<!doctype html><html ><head><meta chart="utf-8"><title>子元素高度与父元素一致</ti女子的阴阳口的样子tle><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">左侧 left 不定高,parent的高度随着左侧left 的高度变化而变化,右侧也跟着变</div><div class="right">这边的高度跟父元素高度一致</div></div></body></html>
(5)效果
(6)问题来了:
如果右侧的子元素高度超出了.parent,怎么办?
.right-inner {background: limegreen;height: 1024px;}
<div class="right"><div class="right-inner">right的子元素,高度为1024px,会撑破容器,给.right加上 overflow:auto 就防止溢出了</div&像风一样自由gt;</div>
效果图如下:
完整代码:
<!doctype html><html ><head><meta chart="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">左侧 left 不定高,parent的高度随着左侧left 的高度变化而变化,右侧也跟着变</div><div class="right"><div class="right-inner">right的子元素,高度为1024px,会撑破容器,给.right加上 overflow:auto 就防止溢出了</div></div></div></body></html>
(7)其他资考雅思源
到此这篇关于css子元素跟父元素的高度一致的实现方法的文章就介绍到这了,更多相关css子元素父元素高度内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章,希望大家以后多多支持www.887551.com!
本文发布于:2023-04-03 19:48:21,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/42b6841b73b825a1ce90ab1e9afd4875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:CSS子元素跟父元素的高度一致的实现方法.doc
本文 PDF 下载地址:CSS子元素跟父元素的高度一致的实现方法.pdf
留言与评论(共有 0 条评论) |