Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the bootstrap .row class have a default margin-left of -30px?

When I do boostrap I have to use the class "row" ...

When you look at my test design:

enter image description here

Why I am forced with a margin-left of -30px?

With this html I would expect 3 rows sharing each column 33% of the whole available width:

<div class="container">     <div class="row">         <div style="background-color: pink;" class="span4">             This is a label         </div>         <div style="background-color: violet;" class="span4">             This is a textbox         </div>         <div style="background-color: slateblue;" class="span4">             This is a button         </div>     </div>      <div class="row">         <div style="background-color: orange;" class="span4">             This is a label         </div>         <div style="background-color: orangered;" class="span4">             This is a textbox         </div>         <div style="background-color: yellow;" class="span4">             This is a button         </div>     </div>      <div class="row">         <div style="background-color: seagreen;" class="span4">             This is a label         </div>         <div style="background-color: green;" class="span4">             This is a textbox         </div>         <div style="background-color: lightgreen;" class="span4">             This is a button         </div>     </div>      </div> 

The gray area with the buttons is from this code:

<div style="background-color: gray;">     <div class="pager">         <div class="pull-left">             <a href="#" class="btn" data-bind="css: { disabled: !hasPrevious() }, click: previous">previous</a>             <a href="#" class="btn" data-bind="css: { disabled: !hasNext() },     click: next">next</a>         </div>         <div class="pull-right">             <span data-bind="text: stepNumber()" />             <span>/</span>             <span data-bind="text: stepsLength" />         </div>     </div>     <hr />      <!-- ko compose: { model: activeStep,         transition: 'entrance' } -->     <!-- /ko --> </div> 
  1. Why does the whole 3-column design fall together when I remove the -30px margin-left?

  2. How should I change my code to really get a 3 column layout each column having the same width. This is what span4 should do.

like image 610
Elisabeth Avatar asked May 22 '13 19:05

Elisabeth


People also ask

What is default margin in Bootstrap?

css - Why does the bootstrap . row class have a default margin-left of -30px? - Stack Overflow.

Why do Bootstrap rows have negative margin?

Rows have a negative left/right margin of -15px. The Container padding of 15px is used to counteract the negative margins of the Row. This is to keep content evenly aligned on the edges of the layout. If you don't put a Row in a Container, the Row will overflow it's container, causing an undesirable horizontal scroll.

How do I set the margin-left in Bootstrap?

l - sets margin-left or padding-left. r - sets margin-right or padding-right. x - sets both padding-left and padding-right or margin-left and margin-right. y - sets both padding-top and padding-bottom or margin-top and margin-bottom.

How do I add a margin to a Bootstrap row?

If you need to separate rows in bootstrap, you can simply use . form-group . This adds 15px margin to the bottom of row.


1 Answers

question 1:

the spans all have a margin-left of 30px to create some space between the single blocks. this space should only appear between the single spans and not at the beginning (or end) of the row. to accomplish this, there are several possibilitys - for example:

  • create a negative margin with the space-with on the row (this is what bootstrap does)
  • use the :first-child selector to remove margin-left on the first span in a row
  • [to be continued]

i can only assume the bootstrap uses the first option because it's more compatible with older browsers (most likely IE 7 and below).

a little example: lets say your spans have a width of 100, a space of 10 and there are 3 in a row.

  • your total row-width in this case should be 320 (100 + 10 + 100 + 10 + 100 = 320)
  • a single span has a width of 110 (100 width + 10 magin-left)
    • simply adding those up would give you a width of 330 and an ugly space of 10 at the beginning (10 + 100 + 10 + 100 + 10 + 100 = 330)
    • "subtract" 10 with one of the listed methods (-10 + 10 + 100 + 10 + 100 + 10 + 100 = 320)
      • hooray, we've created great things with the power of math

question 2 if you use span4s, you already have 3 columns of the same width. you don't need to change anything.

like image 85
oezi Avatar answered Oct 18 '22 03:10

oezi