Im fairly new to bootstrap and the concept of responsive design.
I was reading over the bootstrap docs and following some tutorials on w3schools. All mentions col-
must = 12 in a single row
class. They also mention you can combine different col
classes and sizes example <div class="col-md-3 col-xs-6">
What I am not getting is when should you break the viewport with the </row>
class when you combine different col sizes
?
Consider the following, where I want a mobile device to display 2 rows and 2 columns and on desktop a single column with 4 rows
<div class="container">
<div class="row">
<div class="col-md-3 col-xs-6">
</div>
<div class="col-md-3 col-xs-6">
</div>
<div class="col-md-3 col-xs-6">
</div>
<div class="col-md-3 col-xs-6">
</div>
</div><!--/row -->
</div><!--/container -->
Considering all columns inside rows
must = 12, the row class would need to be closed on different points for mobile and desktop...?
How would I tackle the above problem, hope the question makes sense.
Thank you
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%.
Rows must always be placed inside of a container. Rows span the width of the container. They have a negative 15 pixel margin at the end, essentially removing the 15 pixel margin set by its container. This is because the columns each have a 15 pixel margin of their own that replaces the container's margin.
float-left and float-right to pull the taller column to the right.
Your code is correct and doesn't need more .row
s. The W3schools tutorial is misleading, and it's wrong to say ".col-*-*
should always add up to 12 for each row".
It's ok to have more (or less) than 12 column units in a Bootstrap .row
. It's known as column wrapping, and will simply make the extra columns wrap to the next line...
From the Bootstrap docs:
"If more than 12 columns are placed within a single row, each group of extra columns will, as one unit, wrap onto a new line"
That's why there are examples in the Bootstrap docs that demonstrate using more than 12 columns in a single .row
. When using column wrapping you do need to be aware of responsive resets (known as "clearfix") if the columns vary in height.
There are many responsive scenarios (like your example) where it's necessary to have column units exceeding 12 in a single .row element. It's a common misconception that column units must be 12 or less in a single .row.
Similar Questions..
Bootstrap what will happen if I put more than 12 columns in a row?
Bootstrap 3 - row can I have columns add up to more then 12?
Just change the "col-md-3" class to "col-md-12" in all divs to show 4 rows and single column on desktop and two rows and two columns on mobile.
<div class="container">
<div class="row">
<div class="col-md-12 col-xs-6">
</div>
<div class="col-md-12 col-xs-6">
</div>
<div class="col-md-12 col-xs-6">
</div>
<div class="col-md-12 col-xs-6">
</div>
</div><!--/row -->
</div><!--/container -->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With