Make the Bootstrap columns fill 100% of the row width

0

I think it's something simple, but since I'm new to Bootstrap I still have not figured it out.

What I want is for the columns to fill the full width of the row, I put a red border on the sides of it to make it easier to understand.

I know it's the padding of the columns that gives this 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>
    
asked by anonymous 11.07.2017 / 04:30

3 answers

0

What has come to my head right now is to create two classes for your columns. One that will be on the left side, with padding just to the right. And another one 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, where I made my edits: JS Fiddle

    
11.07.2017 / 06:41
0

I think there is no need to make the column fill the row 100%, but as I already mentioned this in the question, let's move on.

What was done:

  • HTML

I've added id to 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 to -15px.

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

Logic behind classes first and last :

When the element is the first of your "line" it should be leaning against the left edge of your parent element (in this case the column).

When the element is the last of your "line" it should be leaning against the right edge of your parent element (in this case the column).

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

  • JQuery

Using Jquery, I created the touch function, which is nothing more than the one responsible for doing the class changes defined earlier in CSS . (by touching the element on the nearest border).

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

Logic behind relocation: Using the offset of jquery, you can get the position exactly where the element is on the page, so we can get the position of the last element padding and subtract by the position of the first element id='D' . As long as they are on the same line, the distance between them will be id='A' . This way I can detect the exact time the line break occurs.

When the line break occurs, the last element loses the class 853.5px and receives the class last , because now it is the first of the bottom line.

And of course there's one element left in the top row, and now it needs to get the first class.

In order for the div's relocation to work, I'm using the events .ready () and #, which 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 this code is in the best way possible, but it fulfills its role in relation to the question asked.

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

Is there a way to do reallocation to an indeterminate number of divs?: I think so, but I was not able to think that way (maybe a little more persistence and I'll get it!) .

Viewing 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><linkhref="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>

Do not forget to resize your browser!

    
11.07.2017 / 18:25
0

Should your snippet's red lines be on the sides? According to container documentation you can change the class container to 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>
    
11.07.2017 / 18:49