Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equal height elements with CSS

Tags:

html

css

I read about several different solutions to simulate equal-height columns or elements, but none of them really captured my attention because they were using hacks, incredibly complex HTML layouts or not widely supported attributes.

Here's the example Fiddle.

My goal would be to make sure all the elements have the same height or, at least, the maximum height of the siblings in the row.

A row is composed by 3 elements (in this case, no row wrapper exists but I may consider to add such container element).

Is there a solution that:

  1. doesn't require JS
  2. doesn't use display: table
  3. doesn't use wide padding/margin with negative values
  4. doesn't require exponential uses of divs and float
like image 684
Simone Carletti Avatar asked May 25 '12 12:05

Simone Carletti


People also ask

How do I equal the height in CSS?

@vsync: If you want to make them the same height, make them the same height. For most layouts the "illusion" is sufficient and acceptable. Html/Css is a limited markup language so there is only so much it can do before you have to rely on another tool.

How do I make two columns the same height in CSS?

Answer: Use the CSS3 flexbox With CSS3 flex layout model you can very easily create the equal height columns or <div> elements that are aligned side by side. Just apply the display property with the value flex on the container element and the flex property with the value 1 on child elements.

How do you make two divs the same height?

The two or more different div of same height can be put side-by-side using CSS. Use CSS property to set the height and width of div and use display property to place div in side-by-side format. The used display property are listed below: display:table; This property is used for elements (div) which behaves like table.

How do I keep two side by side DIV elements the same height?

Basically what you do is make both divs/columns very tall by adding a padding-bottom: 100% and then "trick the browser" into thinking they aren't that tall using margin-bottom: -100% .


3 Answers

With the restrictions you made: No, there isn't.

edit: Because you didn't mention this: You could use tables for this. (welcome back to the 90s)

like image 193
Christoph Avatar answered Oct 04 '22 16:10

Christoph


You can use CSS3 flex property for this. Write like this:

CSS

.parent{
    display:-moz-box;
    display:-webkit-box;
    display:box;
    -moz-box-orient: horizontal;
    -webkit-box-orient: horizontal;
    box-orient: horizontal;
    width:100%;
    min-height:200px;
}

.parent div{
    background:red;
    -moz-box-flex: 1;
    -webkit-box-flex: 1;
    box-flex: 1;
    border:2px solid green;
}

HTML

<div class="parent">
    <div class="child">1</div>
    <div class="child">2</div>
    <div class="child">3</div>
</div>

Check this http://jsfiddle.net/VkPmg/2/

like image 25
sandeep Avatar answered Oct 04 '22 15:10

sandeep


If you have a fix width layout, you can fake it by background-image. Imagine a tile image background that simulate the borders, you will just have to repeat-y in the main container.

For example if you have a 400px container with 3 100px box floating (like your fiddle), you will have to make a 1x400 image repeating in the main container. Just add in this image at the good x position 1 pixel look like the border color you used. And repeat it !

With this technique, your box will not have the same height (in reality) but the display will look like the boxe have it because the higher box will push the container and the background will appear.

like image 45
Sebastien Axinte Avatar answered Oct 04 '22 15:10

Sebastien Axinte