简体   繁体   English

设置浮动div的高度以填充其下方的区域

[英]Set height of the floated div to fill area under it

I have many DIVs in my content div, some of them float right and some float left on screen larger than 400px. 我的内容div中有许多DIV,其中一些在大于400px的屏幕上向右浮动,而有些在屏幕上向左浮动。

Working fiddle is https://jsfiddle.net/cmbpt1hd/ 工作提琴是https://jsfiddle.net/cmbpt1hd/

I want height of area-three so that it fills all area under it on screens larger than 400px 我想要区域高度3,以便在大于400像素的屏幕上填充其下的所有区域

Keep in mind 记住

  1. There may be many DIVs above area-three that float right. 区域3上方可能有许多DIV浮动。
  2. There may be many DIVs in content div that float left. 内容div中可能有许多DIV向左浮动。

CSS solution will be preferred. CSS解决方案将是首选。

My code 我的密码

 #content { box-sizing: border-box; width: 100%; } .area-one { box-sizing: border-box; float: right; width: 30%; background-color: #ffa2a2; padding: 8px; } .area-two { box-sizing: border-box; float: left; width: 70%; background-color: #a4dca4; padding: 8px; } .area-three { box-sizing: border-box; float: right; width: 30%; background-color: #8282ff; padding: 8px; } #footer { clear: both; width: 100%; background-color: #ff8282; } @media only screen and (max-width: 400px) { .area-one, .area-two, .area-three { float: none; width: 100%; } } 
 <div id="content"> <div class="area-one"> Area-one, Area-one. </div> <div class="area-two"> Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two. </div> <div class="area-three"> Area-three, Area-three. </div> </div> <div id="footer"> Footer, Footer, Footer. </div> 

Here is what I want 这就是我想要的

在此处输入图片说明

Take a look at flex-box, and splitting your HTML up so you have the left-side divs and the right-side divs wrapped in their own divs: 看一看flex-box,然后将HTML拆分,以使左侧的div和右侧的div包裹在自己的div中:

If you wrap your floating divs you don't have to give each of them a width and a float, and you can more freely add new divs. 如果包装浮动div,则不必为每个div设置宽度和浮点数,并且可以更自由地添加新的div。

Flex-box is great for filling out empty space or making divs distribute evenly. Flex-box非常适合填充空白或使div均匀分布。

 #content { box-sizing: border-box; width: 100%; display:flex; } .area { box-sizing:border-box; float:left; clear:left; width:100%; padding: 8px; flex-grow:1; } .area-one { background-color: #ffa2a2; } .area-two { background-color: #a4dca4; flex-grow:0; } .area-three { background-color: #8282ff; } #footer { clear: both; width: 100%; background-color: #ff8282; } .float-left { width:70%; } .float-right { width:30%; } .float-box { display:flex; float:left; flex-direction:column; } @media only screen and (max-width: 400px) { .float-left, .float-right { float: none; width: 100%; display: block; } #content { display:block; } } 
 <div id="content"> <div class="float-box float-left"> <div class="area area-one"> Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. Area-one, Area-one. </div> </div> <div class="float-box float-right"> <div class="area area-two"> Area-two, Area-two. </div> <div class="area area-three"> Area-three, Area-three. </div> </div> </div> <div id="footer"> Footer, Footer, Footer. </div> 

jQuery solution: jQuery解决方案:

 function getHeight() { if ($(window).width() >= 400) { var areaOne = $('.area-one').height(); var areaTwo = $('.area-two').height(); var h = areaTwo - areaOne; $('.area-three').css('minHeight', h); } else { $('.area-three').removeAttr('style'); } } getHeight(); $(window).on('resize', function() { getHeight(); }); 
 #content { box-sizing: border-box; width: 100%; } .area-one { box-sizing: border-box; float: right; width: 30%; background-color: #ffa2a2; padding: 8px; } .area-two { box-sizing: border-box; float: left; width: 70%; background-color: #a4dca4; padding: 8px; } .area-three { box-sizing: border-box; float: right; width: 30%; background-color: #8282ff; padding: 8px; } #footer { clear: both; width: 100%; background-color: #ff8282; } @media only screen and (max-width: 400px) { .area-one, .area-two, .area-three { float: none; width: 100%; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="content"> <div class="area-one"> Area-one, Area-one. </div> <div class="area-two"> Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two, Area-two. </div> <div class="area-three"> Area-three, Area-three. </div> </div> <div id="footer"> Footer, Footer, Footer. </div> 

JSFiddle JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM