Equal Height Columns Flexbox Layout with CSS and Javascript

Equal Height Columns Flexbox

In this tutorial, we learn How to Create Equal Height Columns flexbox or Divs using pure CSS and JavaScript? therefore, we create equal height layout columns or div with using HTML, CSS, and JavaScript.

I hope you will see on multiple websites two, three or more blocks equal height columns flexbox. these columns are parallel columns. But you will be facing the problem if a divs or columns have more text as the comparison of other divs or columns. then more text divs height increase bit more.

If you are thinking, you will be put the same height value for all columns, at the time it will work, but you will face the problem in future, when we want to add more content or image on divs then we need to change the values of height, I think which is more difficult to customize again and again.

so, today’s we guide you to learn how to create equal height columns flexbox element using CSS and Javascript. I have used the divs inside the list to create the columns, there will be no effect comes on Javascript function because you can use the javascript function in all HTML elements like list, table, span, div, etc.

In HTML section I have used the Image and Content both, you can remove and add content or image according to your requirement. in this program the all columns works with dynamically functioned because of columns have not any exact values of height. for Heading used <h1> tag and for paragraph used <P> tag.

Explanation of source code for Equal Height Columns Flexbox with CSS & JavaScript.

Therefore, we discussed the source code which I have used to create the program if you see in the HTML section I have created a list using HTML <ul> and <li> tags. I have added the div inside the list for each list.

you can see I have created three (3) divs or columns. I have added border-top on paragraph and also added border-top on div to styling. and after div closed tag I have used span class for styling. if you see there are three columns that are why I used 33% width to each column.

The important thing in this program I did not put height in CSS, because height will be managed with Javascript. if div content increased then div height will adjust automatically with JS.

I have used two classes <div class="ContentBlock ParellelHeight"> for every column’s div. here, ParellelHeight class managed by Javascript and ContentBlock Class managed by CSS.

For fetching the columns, I have used Js document.getElementsByClassName (info) method. In JS function for equal height, I run a JS loop 0 to the max height of elements.

loop works for (i = 0; i < elements.length; i++) I hope you will be understood easily Javascript code when you getting the Code. then the height will be adjusted for elements according to the maximum value. For creating equal height columns flexbox program, you have to create three files, as given below.

First Step: Create HTML File as name “index.html” | Change file name as you want

<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>Equal Heights Columns flexbox </title>
   <link rel="stylesheet" href="css/stylesheet.css">
   <link href="https://fonts.googleapis.com/css?family=Lato&display=swap" rel="stylesheet">
</head>
 
<body>
  <ul>
  <li>
    <div class="ContentBlock ParellelHeight">
      <h1>HTML Tutorial</h1>      
      <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered
         alteration in some form, by injected humour, or randomised words which don't look even slightly
         believable.</p>
    </div>
    <div class="span-bottom"></div>
  </li>
  <li>    
    <div class="ContentBlock ParellelHeight">
      <h1>PHP Tutorial</h1>         
      <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered
       alteration in some form, by injected humour, or randomised words which don't look even slightly 
       believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't 
       anything embarrassing hidden in the middle of text.</p>
    </div>
    <div class="span-bottom"></div>
  </li>
  <li>
    <div class="ContentBlock ParellelHeight">
      <h1>Bootstrap Tutorial</h1>        
      <p> It is a long established fact that a reader will be distracted by the readable content of a 
         page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less 
         normal distribution of letters, as opposed to using 'Content here, content here', making it 
         look like readable English. Many desktop publishing packages and web page editors now use Lorem
         Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites 
         still in their infancy</p>
    </div>
    <div class="span-bottom"></div>
    
  </li>
</ul>
    <script  src="JS/JavaScript_Function.js"></script>
 
</body>
</html>

Second Step: Create CSS File as name “stylesheet.css” | Change file name as you want

body {
  background: #212121;
  color: #212121;
  overflow-y: scroll;
  font-family: 'Lato', sans-serif;
  text-align: center;
}
 
p {
  font-size: 14px;
  margin: 0;
  color: #333;
  padding: 15px;
  line-height: 22px;
  margin-top: 40px;
  border-top: 5px solid #2ed01173; 
  font-style: italic;
  text-align: justify;
}
ul {
  width: 100%;
  float: left;
  box-sizing: border-box;
  padding: 0 5%;
}
ul li {
  list-style-type: none;
  width: 33.3333%;
  height: auto;
  padding-left: 10px;
  float: left;
  box-sizing: border-box;
}

ul li:first-child {
  padding-left: 0;
}
ul li .ContentBlock {
  padding: 15px;
  opacity: 0;
  background: #fefefe;
  transition: opacity 0.4s ease;
  border-top: 34px solid #2ed011;
  }
ul li .ContentBlock.show {
  opacity: 1;
}
.span-bottom{
  background: #2ed011;
  height: 15px;
}

Third Step: Create JS File as name “JavaScript_Function.js” | Change file name as you want

(function () {
  ParellelHeight(false);
})();
 
window.onresize = function(){
  ParellelHeight(true);
}
 
function ParellelHeight(resize) {
  var elements = document.getElementsByClassName("ParellelHeight"),
      allHeights = [],
      i = 0;
  if(resize === true){
    for(i = 0; i < elements.length; i++){
      elements[i].style.height = 'auto';
    }
  }
  for(i = 0; i < elements.length; i++){
    var elementHeight = elements[i].clientHeight;
    allHeights.push(elementHeight);
  }
  for(i = 0; i < elements.length; i++){
    elements[i].style.height = Math.max.apply( Math, allHeights) + 'px';
    if(resize === false){
      elements[i].className = elements[i].className + " show";
    }
  }
}

Therefore, see the code segment to create the Equal Height Columns Flexbox. you will be created successfully. you have to face any trouble then comment me down below. I will try to resolve the problem as soon as possible. thanks for viewing these post.