Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do rows need an immediate parent in Bootstrap?

The Bootstrap 3 docs say:

Rows must be placed within a .container for proper alignment and padding.

Does this mean that one of their ancestors should be a container or that their immediate parent should be a container?

Having looked at the examples, I think the former interpretation is correct as containers have fixed widths for specific display sizes:

@media (min-width: 1200px) {
  .container {
    width: 1170px;
  }
  ...
}

And as such they cannot be placed inside other components (e.g. .panel-bodys).

In other words, is the following correct markup in Bootstrap 3?

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">Title</h3>
    </div>
    <div class="panel-body">
        <div class="row">
            <div class="col-xs-6">
                Col 1
            </div>
            <div class="col-xs-6">
                Col 2
            </div>
        </div>
    </div>
</div>  
like image 366
TheFooProgrammer Avatar asked Jan 07 '14 03:01

TheFooProgrammer


People also ask

Can I use row without container in Bootstrap?

Bootstrap 3 it is reasonable to drop the outer container (or at least its css class assignment): This might leave some white space on the right side. Adding "no-gutters" class to the row without a container fixed it for me.

How columns and rows work in Bootstrap?

The grid system of Bootstrap 4 allows you to divide a row into 12 columns of equal width. However, these columns can also be combined to create columns that are wider and positioned differently. The grid can also be responsive and rearrange depending on the screen width or window size.

When should I use rows in Bootstrap?

Use rows to create horizontal groups of columns. Content should be placed within columns, and only columns may be immediate children of rows.

What does row content do in Bootstrap?

In Bootstrap, the "row" class is used mainly to hold columns in it. Bootstrap divides each row into a grid of 12 virtual columns. In the following example, the col-md-6 div will have the width of 6/12 of the "row"s div, meaning 50%.


2 Answers

It means that one of their ancestors should be a .container.

And your code is correct, as the docs mention:

Note that, due to padding and fixed widths, containers are not nestable by default.

Some info on why rows need to be inside .container.

Rows have margin-left: -15px; margin-right: -15px. That's because rows should only contain columns, e.g. col-md-12, and those columns have padding-left: 15px; padding-right: 15px. So that negative margin on the row will mean that effectively columns will line up 'flush' with the edges of the grid.

Because of that negative margin, you need to have the .container because it has padding-left: 15px; padding-right: 15px;. Without that, your rows would go off the page.

Full width designs

Of course, if you do wrap everything in .container then you'll have a fixed width which is not right for everyone. So, if you don't want that, you can go against Bootstrap's rules and place your rows inside a parent that has padding: 0 15px to offset the negative margin on rows (the would cause container to go off the screen and cause a scrollbar).

This demo shows both situations described above.

like image 81
davidpauljunior Avatar answered Oct 01 '22 07:10

davidpauljunior


The .container class is responsible for the padding and margins of its children. Hence, whatever content you put inside the containers inherhits those properties unless overridden. There's nothing unusual going on here.

Take a look at the source for further information:

.container {
  padding-right: 15px;
  padding-left: 15px;
  margin-right: auto;
  margin-left: auto;
}
like image 20
durrrutti Avatar answered Oct 01 '22 08:10

durrrutti