肯定精准定位方式:
(1)将父原素设定为相对性精准定位,不写父原素的高宽比时,会伴随着左侧的子原素高宽比转变而转变
.parent { /*重要编码*/ 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 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"> 左边 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> </div>
实际效果图以下:
详细编码:
<!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"> 左边 left 不确定高,parent的高宽比伴随着左边left 的高宽比转变而转变,右边也跟随变 </div> <div class="right"> <div class="right-inner">right的子原素,高宽比为1024px,会撑破器皿,给.right再加 overflow:auto 就避免外溢了</div> </div> </div> </body> </html>
(7)别的資源
http://stackoverflow.com/questions/3049783/how-to-make-a-floated-div-100-height-of-its-parent
到此这篇有关CSS子原素跟父原素的高宽比一致的完成方式的文章内容就详细介绍到这了,大量有关CSS子原素父原素高宽比內容请检索脚本制作之家之前的文章内容或再次访问下边的有关文章内容,期待大伙儿之后多多的适用脚本制作之家!