Make Bootstrap columns fill 100% of the Row width

Asked

Viewed 1,798 times

0

I think it’s something simple, but since I’m a beginner in Bootstrap I haven’t figured out how to do it.

What I want is for the columns to fill the full width of Row, I put a red border on the sides of it to facilitate understanding.

I know it’s the padding of the columns that gives that spacing, but I want to fill the Row without removing the space between the columns.

Jsfiddle

.box {
  height: 100px;
  margin-bottom: 20px;
  border: 1px solid rgba(0,0,0,0.2);
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row" style="margin-top:40px;margin-bottom:40px;border-right: 1px solid red;border-left: 1px solid red;">
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box"></div>
     </div>
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box"></div>
    </div>
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box"></div>
    </div>
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box"></div>
    </div>
   </div>
  </div>

  • Gabriel, what is the need of this occupation 100%? If you take the edges off, you can’t tell if the col is occupying 100% or 90%.. It makes no difference. If we make the column 'stick' in Row, every time the media break happens it will be strange and misaligned.

  • Gabriel, feedback would be interesting!

3 answers

0

What came to my mind at the moment is to create two classes for your columns. One that will be on the left side, with padding only on the right. And another that will always be on the right, with padding only on the left. With this, they would occupy 100% of the frame and remain separate.

<div class="container">
<div class="row" style="margin-top:40px;margin-bottom:40px;border-right: 1px solid red;border-left: 1px solid red;">
  <div class="col-xl-3 col-md-4 col-sm-6 col-left">
    <div class="box"></div>
 </div>
<div class="col-xl-3 col-md-4 col-sm-6 col-right">
  <div class="box"></div>
</div>
<div class="col-xl-3 col-md-4 col-sm-6 col-left">
  <div class="box"></div>
</div>
<div class="col-xl-3 col-md-4 col-sm-6 col-right">
  <div class="box"></div>
</div>

    .box {
  height: 100px;
  margin-bottom: 20px;
  border: 1px solid rgba(0,0,0,0.2);
  padding: (0,0,0,0)
}

.col-left {
  padding-left: 0px;
 }

 .col-right {
  padding-right: 0px;
 }

Follow JS Fiddle below, in which I made my edits: JS Fiddle

  • Pedro, this does not solve the problem, the middle columns have no more space between them.

  • Yes, it doesn’t. I had misunderstood the question!

0

I believe that there is no need to make the column fill the Row 100%, but as I have already commented on this in the question, let’s move on.

What has been done:

  • HTML

I added id for each div of class='box' (A,B,C,D), so that they can independently undergo javascript manipulation.

  • CSS

Added class first which reduces the margin-left of the element in -15px.

Added class last which reduces the margin-right of the element in -15px.

Logic behind the classes first and last:

When the element is the first of his "line" he must stand against the left edge of his parent element (in this case the column).

When the element is the last of his "line" he must stand against the right edge of his parent element (in this case the column).

But why -15px? : 15px is the padding default column defined by bootstrap. Then we define which child element will have -15px of margin to cancel the action of padding.

  • Jquery

Using Jquery, I created the function pull over, which is nothing more than the person responsible for the class exchanges defined previously in CSS. (touching the element to the nearest edge).

When there is a line break or a change of media, the function must detect and relocate the div’s correctly.

Logic behind the relocation: Using the offset jquery, it is possible to take the exact position the element is in on the page, so we can take the position of the last element id='D' and subtract by the position of the first element id='A'. As long as they’re on the same line, the distance between them will be 853.5px. This way I can detect the exact time when the line break occurs.

When line breaking occurs, the last element loses class last and gets the class first, why now he’s the first of the bass line.

And of course there’s one element left on the top line, and now he needs to receive the class last.

For div’s relocation to work, I’m using events .ready() and .resize(), that together are able to detect when the page has been fully loaded and every time it is being resized.

  • My sincerity

I do not believe that this code is in the best possible way, but it fulfills its role in relation to the question asked.

Unfortunately this code only works when we are talking about the 4 div’s created (A,B,C,D).

There is a way to reallocate an undetermined number of Divs?: I believe so, but I was not able to think that way (maybe a little more persistence and I arrive there!).

The view of this code is also available here.

$(document).ready(encostar);
$(window).on('resize',encostar);

function encostar(){
 
  var vet = [];
  
  $('.box').each(function(){
    
    var offset = $(this).offset();
    
   // $(this).text(offset.left+'px');
    
    vet[$(this).attr('id')] = offset.left;
    
    var out = '';
    for (var i in vet) {
        out += i + ": " + vet[i] + "\n";
    }
        
    if(vet['D'] - vet['A'] < 853.5)
      {       

        $('#D').addClass('first');
        $('#D').removeClass('last');
        
        if($('#C').hasClass('first'))
          {
            
          }
        else
          {
            $('#C').addClass('last');
          }    
        
        if(vet['B'] === vet['D'])
          {
              $('#D').removeClass('first');
              $('#D').addClass('last');
              $('#B').removeClass('first');
              $('#B').addClass('last');
          }
        
        if(vet['A'] === vet['C']-15)
          {
            $('#C').addClass('first');
            $('#C').removeClass('last');
            
            if($(window).width() < 575)
            {
               $('#A').addClass('last');
               $('#C').addClass('last');
               $('#B').addClass('first');
               $('#B').addClass('last');
               $('#D').addClass('first');
               $('#D').addClass('last');
            }
            else
              {
               $('#A').removeClass('last');
               $('#C').removeClass('last');
               $('#B').removeClass('first');
               $('#D').removeClass('first');
              }
            
          }
        
      }
    else
      {   
   
        $('#D').removeClass('first');
        $('#D').addClass('last');
        $('#C').removeClass('last'); 
        $('#C').removeClass('first');  
        $('#B').removeClass('last'); 
      }
    
 
    
  });
 
}
.box {
  height: 100px;
  margin-bottom: 20px;
  border: 1px solid rgba(0,0,0,0.2);
  text-align: center;
}


.first{
  
    margin-left: -15px !important;
  
}


.last{
  
    margin-right: -15px !important;
  
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row" style="margin-top:40px;margin-bottom:40px;border-right: 1px solid red;border-left: 1px solid red;">
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box first" id='A'></div>
     </div>
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box" id='B'></div>
    </div>
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box" id='C'></div>
    </div>
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box last" id='D'></div>
    </div>
   </div>
  </div>

Don’t forget to resize your browser!

  • Sorry it took me so long to give you feedback. Thanks for the code, but it would have to be a dynamic, no-limit speaker solution.

  • Okay, it’s always good to clarify in question too!

0

Should your snippet’s red lines be leaning against the sides? According to container documentation you can change the class container for container-fluid:

.box {
  height: 100px;
  margin-bottom: 20px;
  border: 1px solid rgba(0, 0, 0, 0.2);
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />

<div class="container-fluid">
  <div class="row" style="margin-top:40px;margin-bottom:40px;border-right: 1px solid red;border-left: 1px solid red;">
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box"></div>
    </div>
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box"></div>
    </div>
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box"></div>
    </div>
    <div class="col-xl-3 col-md-4 col-sm-6">
      <div class="box"></div>
    </div>
  </div>
</div>

  • Renan are not the red lines that should touch, are the div’s box, which should touch the red line.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.