How to make equal 5 columns in bootstrap 4?

Viewed 68

I have to create 5 columns in a row but sometime it may be less than 5 columns in a row, how can i manage this?

My Code:-

.col {
  border: solid;
  margin: 2px;/* this can be added without breaking the row */
}
.col:before {
content:attr(class);/* show class used */
color:crimson
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
      <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    
<div class="w-100"></div>

    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>

<div class="w-100"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
</div>
</div>

For example:- Right now How can i manage last two boxes, i want all the boxes should equal like first row boxes width...

Thank You!

1 Answers

If I'm understanding you correctly, you want all columns to always be the same width as the columns in the first two rows. If that is correct, you could just override the max-width property on Bootstrap's col class.

Something like this (using your unchanged HTML):

.col {
  border: solid;
  margin: 2px;
  /* this can be added without breaking the row */
  max-width: 18% !important;  /* added. note, if you remove the margin above, you could set this to 20% */
}

.col:before {
  content: attr(class);
  /* show class used */
  color: crimson
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div class="row">
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>

    <div class="w-100"></div>

    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>
    <div class="col"></div>

    <div class="w-100"></div>

    <div class="col"></div>
    <div class="col"></div>
  </div>
</div>

You may also wish to set a constant width for all columns using the width property. But I may be misunderstanding what you're trying to do.

Related