Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting element height in responsive layout?

Generally, what markup and CSS will create an element with a percentage-based height in a responsive design? How to build a 2 Column (Fixed - Fluid) Layout with Twitter Bootstrap? shows how to create two 100% height columns, but this breaks for heights < 100%.

Specifically, I have a sidebar div with a list of items, which may be short (empty) or long (overflow: auto). But because no parent elements have a fixed height, height: 20%; does not work. How can I give the sidebar a fluid height while maintaining a responsive design?

like image 260
knite Avatar asked Feb 24 '12 14:02

knite


People also ask

How do I set responsive height?

For the height of a div to be responsive, it must be inside a parent element with a defined height to derive it's relative height from. If you set the height of the container holding the image and text box on the right, you can subsequently set the heights of its two children to be something like 75% and 25%.

How do I Auto adjust height in CSS?

If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly. If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow.


1 Answers

Usually having a fluid padding-bottom on the parent container solves this problem.

More information can be found here : http://www.alistapart.com/articles/creating-intrinsic-ratios-for-video/

update: on js bin http://jsbin.com/obesuk/1/

markup :

<div class="obj-wrapper">  <div class="content">content</div> </div> 

css:

  .obj-wrapper {     position:relative;     width:50%;     padding-bottom:40%;     height:0;     overflow:hidden;   }   .content {     position:absolute;     width:100%;     height:100%;     background:red;   } 
like image 87
Varinder Avatar answered Oct 26 '22 03:10

Varinder