Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make divs on same row the same height - Dynamic Content

I am working on an application that generates dynamic content and displays them on floating divs. Each div takes 49% width of the page. The problem I'm running into is that the height of the divs vary depending on the content.

What I'm looking to do is make the divs on the same row the same height. Any suggestions?

.item {
    background: #c4c4c4;
    width: 49%;
    border: 1px solid black;
    float: left;
}
<div id="container">
    <div class="item">
        Test
    </div>
    <div class="item">
        Hello.
        Sample <br>
        Content <br>
    </div>
    <div class="item">
        Test<br>
        Sample Content
    </div>
    <div class="item">
        Test
    </div>
</div>
like image 471
Sohrab Hejazi Avatar asked Sep 23 '15 19:09

Sohrab Hejazi


People also ask

How do I make a div the same height in a row?

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 I keep 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 dynamically change the height of a div?

The content height of a div can dynamically set or change using height(), innerHeight(), and outerHeight() methods depending upon the user requirement.


1 Answers

Using CSS3 flexbox to make the #container adapt flexible box layout.

Default value of flex-wrap is nowrap so it aligns in a single row. Use flex-wrap: wrap to create multiple rows based on item's width.

Current browser support for Flexbox is pretty good: Can I use Flexbox?

#container {
  display: flex;
  flex-wrap: wrap; /* Wrap after the items fill the row */
  
  /* Safari specific rules */
  display: -webkit-flex;
  -webkit-flex-wrap: wrap;
}
.item {
  background: #c4c4c4;
  border: 1px solid black;
  width: 49%;
}
<div id="container">
  <div class="item">
    Test
  </div>
  <div class="item">
    Hello. Sample
    <br>Content
    <br>
  </div>
  <div class="item">
    Test
    <br>Sample Content
  </div>
  <div class="item">
    Test
  </div>
</div>
like image 197
m4n0 Avatar answered Oct 01 '22 13:10

m4n0