Equal height divs kept together side-by-side always - css

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;
}

Related

Make flex items expand to full width of a row, but all stay the same size in another [duplicate]

This question already has answers here:
Targeting flex items on the last or specific row
(10 answers)
Closed 3 years ago.
So basically, what I have here is a contact page, that shows each contact's info inside a card. It's basically flex container with flex items. I want cards to expand to full width of a first row (5 cards in a row on desktop). I have managed to achieve that but what I want to have is to have all the cards be the same width. For example if user has 7 contacts, first 5 cards will be displayed in a first row nicely, but second two will expand to take the full width of the second row. Is it possible to make those cards the same width as others?
<div class="d-flex flex wrap">
<div class="contact-card">
<div>
<div class="contact-card">
<div>
<div class="contact-card">
<div>
<div class="contact-card">
<div>
<div class="contact-card">
<div>
</div>
.contact-card {
flex: 1 1 0;
}
It looks like a straight forward Bootstrap layout. I copied CSS from Bootstrap CSS.
You can adjust width (and consequently number of columns) in a row by setting flex:0 0 16.666667% and max-width: 16.666667%; to custom values.
.row {
display: flex;
flex-wrap: wrap;
}
.col-2 {
width: 100%;
flex: 0 0 16.666667%;
max-width: 16.666667%;
}
/*DEMO*/
*,*::before,*::after{box-sizing:border-box}
.col-2{height:80px;border:1px red solid}
<div class="row">
<div class="col-2"></div>
<div class="col-2"></div>
<div class="col-2"></div>
<div class="col-2"></div>
<div class="col-2"></div>
<div class="col-2"></div>
<div class="col-2"></div>
<div class="col-2"></div>
</div>

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;
}
}

Bootstrap: Floated divs cause parent elements to collapse. Best way to structure nested columns?

I'm really struggling with Bootstrap regarding the way it handles divs.
It seems to me, that all column classed divs using Bootstrap CSS are floated. For example:
<div class="col-md-3">
property
</div>
This above div has the style: float: left; as defined in Bootstrap CSS.
Now everyone knows about the issue with floating, it causes the parent div to not expand to the height because it doesn't 'see' floated elements as pushing out the container.
I feel like this is a serious flaw in Bootstrap. I have some classes to add margin to divs, for example:
.full-buffer{margin:20px 0}
If I want to use this class on a div that wraps a number of Bootstrap columns, it doesn't work. Because the div has no height. If I wanted to add a background colour, it won't show up. For example:
.background-coloured-div{background-color:#0F0;}
<div class="background-coloured-div">
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
</div>
The above won't show any background colour, because all of the divs inside are floated, so it'll collapse to 0px in height.
JSFiddle without bootstrap
JSFiddle with bootstrap - fixes one problem and causes another
So, what's the proper way to use Bootstrap columns? If everything is floated, how can you add proper margins, background colours etc to parent divs? Isn't this a massive flaw of the whole system?
Bootstrap has a row class that contains the floats using:
.row:before {
display: table;
content: " ";
}
.background-coloured-div {
background-color: #0F0;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class="background-coloured-div row">
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
</div>
You'll want to wrap those cols in a .row div to contain the floats.
Don't forget to also contain that row in a .container or .container-fluid to counter balance the -15px margin applied to .row.
<div class="container-fluid">
<div class="background-coloured-div row">
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
</div>
</div>
http://getbootstrap.com/css/#grid-example-fluid
If for whatever reason you don't want to use a .row, you could add the following to your css to contain the floats of that divs children, but I recommend using Bootstraps solution:
background-coloured-div:after {
content:'';
display:table;
clear:both;
}
Rows must be placed within a .container (fixed-width) or
.container-fluid (full-width) for proper alignment and padding.
Use rows to create horizontal groups of columns.
Content should be placed within columns, and only columns may be
immediate children of rows.
In your case, the code should be:
<div class="container-fluid">
<div class="row background-coloured-div">
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
<div class="col-md-3">
property
</div>
</div>
</div>

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

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.

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