Responsive Bootstrap - changing number of divs per row from desktop to mobile - css

New to responsive bootstrap here. I have 2 div's per row and 2 rows showing on my desktop. I used display: inline-block (see below link):
https://jsfiddle.net/9ya7kb67/
HTML:
<div class="parent">
<div class="child"> content </div>
<div class="child"> content </div>
<div class="child"> content </div>
<div class="child"> content </div>
</div>
CSS:
.parent {
width: 200px;
}
.child {
display: inline-block;
width: 80px;
}
However, I'd like to keep this layout in desktop but change it to 1 div per row (for 4 rows) on mobile using responsive bootstrap. How can I do this?

Check the bootstrap documentation for its grid system here: http://getbootstrap.com/css/#grid-example-mixed-complete
There are 4 different types of classes col-sm col-xs col-md col-lg each one fitted for different screen sizes and you can combine them to make dynamic grids for your site.
<div class="row">
<div class="col-xs-12 col-md-6">Test col</div>
<div class="col-xs-12 col-md-6">Test col</div>
<div class="col-xs-12 col-md-6">Test col</div>
<div class="col-xs-12 col-md-6">Test col</div>
</div>
I didn't exactly understand what your end goal is, but the code above is something similar to what you want. On small screens col-xs-12 will fill the entire row, on medium to large screen you will have two col-md-6 on each row. Make sure to read the bootstrap documentation to learn more about it.

Related

Bootstrap v3, Reverse Stacking Order of Full-Width Columns

Is it possible to reverse the order of full width columns in Bootstrap v3? For example...
-xs (both A and B are col-xs-12)
[ A ]
[ B ]
-sm (both A and B are col-sm-12)
[ B ]
[ A ]
I know that I could use hide/show techniques, but I want to avoid duplicate content because both A and B have a lot of dynamically generated content, and duplicating that content will slow the page down quite a bit. Thanks.
Use Bootstraps .col-md-push-* and .col-md-pull-* classes.
More on this, here: http://getbootstrap.com/css/#grid-column-ordering
Example:
<div class="container">
<div class="row">
<div class="col-md-9 col-md-push-3">.col-md-9 .col-md-push-3</div>
<div class="col-md-3 col-md-pull-9">.col-md-3 .col-md-pull-9</div>
</div>
</div>
CODEPEN DEMO
For full width column example using push/pull helper classes please see this JS bin:
<div class="row">
<div class="col-xs-12 col-sm-6 col-sm-push-6">
<p>test2</p>
</div>
<div class="col-xs-12 col-sm-6 col-sm-pull-6">
<p>test1</p>
</div>
</div>
http://jsbin.com/gazipa/2/edit?html,css,output
You can also use CSS transform to change it's ordering at a viewport breakpoint:
#media (max-width: 767px) {
.row.reorder-xs {
/*bring your own prefixes*/
transform: rotate(180deg);
direction: rtl; /* Fix the horizontal alignment */
}
.row.reorder-xs > [class*="col-"] {
/*bring your own prefixes*/
transform: rotate(-180deg);
direction: ltr; /* Fix the horizontal alignment */
}
}
http://jsbin.com/gazipa/3/edit?html,css,output
2018 Update Bootstrap 4
In Bootstrap 4, you can change the order of full-width (12 unit) columns using the flexbox ordering classes. For example,
<div class="container">
<div class="row">
<div class="col-12 order-sm-1 order-2">1</div>
<div class="col-sm-12 order-1">2 (first on xs)</div>
</div>
</div>
https://www.codeply.com/go/VUjKsM3cUD
For those on Bootstrap 3, just add a flexbox wrapper. Then you can add flex-direction with column reverse. The code would look something like this (assume mobile first)...
Markup:
<div class="row">
<div class="flex-switch">
<div class="col-xs-12 col-sm-6">
This is a column
</div>
<div class="col-xs-12 col-sm-6">
This is a column
</div>
</div>
</div>
CSS:
.flex-switch {
display: flex;
flex-direction: column-reverse;
#include sm-min {
flex-direction: row;
}
}

Equal height divs kept together side-by-side always

Using Bootstrap 3.x and creating a common 3 column layout. In desktop view I want all three divs in the same row and the same height. When shrunk down to the smallest width I want the first two divs to always remain next to each other but the third drop below. Always, the first two divs should be the same height. If the second div is shorter than the first, the third div ends up underneath the second, to the right of the first.
<div class="row">
<div class="col-sm-12 col-md-9">
<!-- keep these two divs together side by side and the same height -->
<div class="col-xs-6 col-sm-6 col-md-6">
this is panel one
</div>
<div class="col-xs-6 col-sm-6 col-md-6">
this is panel two
</div>
</div>
<div class="col-sm-12 col-md-3">
<!-- this div should be beside the other two on large screens and drop below on xsmall screens -->
side bar ad
</div>
</div>
Here is another way to visualize the issue. The first two need to be the same height: http://www.bootply.com/29cNrJrEwT
Connie DeCinko, Hi there.
To have the divs flow like you ask here you would do that this way.
Here is the Fiddle.
You said that the first two blocks should have the same height so you would not have a issue with the 3rd block flowing below.
But, if say the second block is to be shorter than the first block then wrap the first 2 block in a row and also wrap the 3rd block in a row and add to the row for the 3rd block col-xs-12 so when on small screens it takes up the full width.
<div class="container">
<div class="row text-center">
<div class="col-xs-6 col-sm-4 block1"><h2>Block 1</h2></div>
<div class="col-xs-6 col-sm-4 block2"><h2>Block 2</h2></div>
<div class="col-xs-6 col-sm-4 block3"><h2>Block 3</h2></div>
</div>
</div>
If you do actually want to have the second block shorter.
Then you could do it this way.
Here is the Fiddle.
Note this is not using Flex ... Flex does not have full browser support.
<div class="container">
<div class="row-fluid col-xs-12 col-sm-8 text-center clear">
<div class="col-xs-6 col-sm-6 block4"><h2>Block 1</h2></div>
<div class="col-xs-6 col-sm-6 block5"><h2>Block 2</h2></div>
</div>
<div class="row-fluid col-xs-12 col-sm-4 text-center clear">
<div class="col-xs-6 col-sm-12 block6 "><h2>Block 3</h2></div>
</div>
</div>
Flexbox was created for this. Literally.
http://jsfiddle.net/rudiedirkx/6ka86vfx/
Define a flex container with display: flex
Tell its children to flex: 1
Except the bigger children, they're flex: 3 in this case
Children can be flex containers too
It's children have flex: 1 again, to make them equal size
Your class names aren't the most descriptive, but the CSS is otherwise very simple:
.row { /* flex container */
display: flex;
}
.row > div { /* all its children */
flex: 1;
}
.row > div:first-child { /* one of its children is bigger (3:1) and is a flex container itself */
display: flex;
flex: 3;
}
.row > div:first-child > div { /* all its children are equal size */
flex: 1;
}

How to create two rows in a bootstrap without scrolling using one with pixel and another with percentage?

How to create two rows in a bootstrap without scrolling using one with pixel and another with percentage ?
<div class="container-fluid">
<div class="row main_header">
sadf
</div>
<div class="row main_second">
<div class="col-lg-2 main_left">
test
</div>
<div class="col-lg-10 main_right">
test
</div>
</div>
</div>
How to create two rows in a bootstrap without scrolling using one with pixel and another with percentage ?
.main_header{
height: 80px;
background-color: #606060;
color: #fff
}
.main_left{
height: 100%;
background-color: #D8D8D8;
} .main_right{
height: 100%;
}
.main_second{
height: 100%;
}
I think you don't need to use any additional CSS to achieve what you want as Bootstrap already have a very established Grid System. Please see my following example in the following fiddle!
All what I have used is the following lines with Twitter Bootstrap V3.3.4:
<div class="row">
<div class="row">
<div class="col-sm-4" style="background-color:red;">.col-sm-4</div>
</div>
<div class="row">
<div class="col-sm-4" style="background-color:lavender;">.col-sm-4</div>
</div>
</div>
And you can add any additional divs within those two divs.
Learn about the Grid System very well to use this via these kind of tutorials.
And it's better to look at the following StackOverflow answer, I guess.

Responsive grid, equal height

How would i go about achieving this layout, in a responsive scenario (ie 25% width), but each block have the same height:
_ _ _ _
|_|_|_|_|
|_|___|_|
|_|_|_|_|
Notice the middle block occupies (50% of the width), that middle block contains text, but the square images are images (that are square, giving its size).
Should i be using display: table mixed with display: table-cell and display: table-row? Or is there nifty css trick.
Flexbox is really cool but we are not yet at 100% for support, so I do not yet use it. I would probably check out something like www.shelvesgrid.org. Then have a width: 100% on the images within the boxes and if they are all perfectly square, it should work.
Your structure will look like this:
<div class="row">
<div class="column-3"></div>
<div class="column-3"></div>
<div class="column-3"></div>
<div class="column-3"></div>
</div>
<div class="row">
<div class="column-3"></div>
<div class="column-6"></div>
<div class="column-3"></div>
</div>
<div class="row">
<div class="column-3"></div>
<div class="column-3"></div>
<div class="column-3"></div>
<div class="column-3"></div>
</div>
An your CSS is just img {width: 100%}

Twitter Bootstrap - why row class has margin-left: -20px?

I don't understand why row class has margin-left: -20px (so it grows after parents border like on the image). I think nobody needs this behavior. Or am I doing something wrong?
<div class="container">
<div id="top-container" class="row">
<div class="span8">
<h1>App</h1>
</div>
<div class="span4">
</div>
</div>
</div>
You can use row-fluid instead of row, then your span4 and span8 won't have margin-left.
<div class="container">
<div id="top-container" class="row-fluid">
<div class="span8">
<h1>App</h1>
</div>
<div class="span4">
</div>
</div>
</div>
For people trying to find this out for Bootstrap 3:
Grids and full-width layouts Folks looking to create fully fluid
layouts (meaning your site stretches the entire width of the viewport)
must wrap their grid content in a containing element with padding: 0
15px; to offset the margin: 0 -15px; used on .rows.
Source (search for Grids and full-width layouts)

Resources