Dynamically I create a set of boxes with slightly different height and want to appear 2, 3 or 4 of them (depending on the screen size) at the same row. I tried the following markup with bootstrap:
<div class="container-fluid">
<div class="row-fluid">
<div class="col-xs-6">
Two<br>Lines
</div>
<div class="col-xs-6">
Two<br>Lines
</div>
<div class="col-xs-6" style="background-color: red">
Three<br>Lines<br>Jup
</div>
<div class="col-xs-6">
Two<br>Lines
</div>
<div class="col-xs-6">
Two<br>Lines
</div>
<div class="col-xs-6">
Two<br>Lines
</div>
</div>
</div>
My problem is the space below my third column.
A B
C D
C E <- Should wrap to next row, because of C
F G
Can you tell how to achieve this? I recognized the advice to use a clearfix but I guess this attempt will cause problems and ugly code when using a different count of columns.
Thanks for your answers.
The problem
The problem is that all bootstrap columns try to float left.
From MDN on floats:
when an element is floated it is taken out of the normal flow of the document. It is shifted to the left or right until it touches the edge of its containing box or another floated element.
So when you have uneven heighted elements, the correct behavior is to stack them to the side.
Luckily, there are a lot of way to correct this to the anticipated behavior:
Using CSS Clear property
From MDN on Clear:
The clear CSS property specifies whether an element can be next to floating elements that precede it or must be moved down (cleared) below them
For this exact structure, you can apply clear to every other row using the nth-child selector:
.col-xs-6:nth-child(odd) {
clear: both;
}
Note: The nth-child selector won't work in IE8
Demo in jsFiddle / Stack Snippets:
.col-xs-6:nth-child(odd) {
clear: left;
}
.col-xs-6 {
border: 1px solid grey;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6 label-warning">
Three<br/>
Lines<br/>
Jump
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
</div>
Using .clearfix
The Bootstrap way to do this is to use the clearfix class which applies the following styles:
.clearfix:before,
.clearfix:after {
content: " ";
display: table;
}
.clearfix:after {
clear: both;
}
Note: You can use selectively target different screen sizes by combining this with the responsive utility classes (i.e. .visible-*-*, .hidden-*)
Demo in jsFiddle / Stack Snippets:
.col-xs-6 {
border: 1px solid grey;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="clearfix"></div>
<div class="col-xs-6 label-warning">
Three<br/>
Lines<br/>
Jump
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="clearfix"></div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
</div>
Using .row
You can also just wrap every pair of columns into a new row. This is the least reusable, but if every set of items forms a logical group, it does create semantic markup.
Demo in jsFiddle / Stack Snippets:
.col-xs-6 {
border: 1px solid grey;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
<div class="row">
<div class="col-xs-6 label-warning">
Three<br/>
Lines<br/>
Jump
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
<div class="row">
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
</div>
Fixing the height
Depending on your use case, you might just benefit from making everything the same height. This would work well if you had similar content in every column and wanted a consistent looking grid.
.col-xs-6 {
height: 7rem;
}
Demo in jsFiddle / Stack Snippets:
.col-xs-6 {
height: 7rem;
}
.col-xs-6 {
border: 1px solid grey;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6 label-warning">
Three<br/>
Lines<br/>
Jump
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
<div class="col-xs-6">
Two<br/>
Lines
</div>
</div>
</div>
Related
I have a Bootstrap page on which I'm trying to stack different boxes.
Imgur - Image of boxes (sorry, not enough rep to upload images directly)
The green boxes are the ones currently in position, and the red ones are the ones I am having issues with. I'm using the following code (simplified) to get the green boxes:-
<div class="container">
<div class="row">
<div class="col-xs-3" style="height:100px; background-color:green;">
</div>
<div class="col-xs-3" style="height:100px; background-color:green;">
</div>
<div class="col-xs-6" style="height:200px; background-color:green;">
</div>
</div>
</div>
I'm basically trying to create another two <div class="col-xs-3" style="height:100px; background-color:green;"> that will go underneath the current two, while also keeping the large box to the right.
I thought it would be an easy fix with a new row, or using float:left / right, but none of that seems to be working out.
One way to do it would be to make two 50% width columns and then divide the column on the right into two rows with two more columns in each:
.green-lrg {
background-color:green;
height:215px;
}
.green-sml {
background-color:green;
height:100px;
}
.red-sml {
background-color:red;
height:100px;
}
.col-xs-6 .row {
padding-top:15px;
}
.col-xs-6 .row:first-child {
padding-top:0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row">
<!-- LEFT COLUMN -->
<div class="col-xs-6">
<div class="green-lrg"></div>
</div>
<!-- RIGHT COLUMN -->
<div class="col-xs-6">
<div class="row">
<div class="col-xs-6">
<div class="green-sml"></div>
</div>
<div class="col-xs-6">
<div class="green-sml"></div>
</div>
</div>
<div class="row">
<div class="col-xs-6">
<div class="red-sml"></div>
</div>
<div class="col-xs-6">
<div class="red-sml"></div>
</div>
</div>
</div>
</div>
</div>
Here is simple example.
HTML
<div class="container">
<div class="row">
<div class="col-xs-6 box box-big">
</div>
<div class="col-xs-6">
<div class="row">
<div class="col-xs-6 box box-small">
</div>
<div class="col-xs-6 box box-small">
</div>
<div class="col-xs-6 box box-small">
</div>
<div class="col-xs-6 box box-small">
</div>
</div>
</div>
</div>
</div>
and CSS:
.box {
background-color:green;
border:5px solid white;
}
.box-big {
height:200px;
}
.box-small {
height:100px;
}
I am trying to adapt a layout on Bootstrap that I got from the designer. Two columns in the content has a small spacing, that I couldn't achieve with direct column-spacing (as it gives too much gap). Thus, I tried wrapping everything into a row and adding sub-columns inside my main columns. Here in the code, it's more clear:
<div class="container">
<div class="col-md-12 banner"></div>
<div class="row">
<div class="col-md-8">
<div class="col-md-12 leftSide">
</div>
<div class="col-md-12 leftSide">
</div>
</div>
<div class="col-md-4 rightSide">
<div class="col-md-12">
</div>
</div>
</div>
</div>
Now the spacing seems good but as I wrapped it in a row (I think), the right panel overflowed more then the banner.
It can be seen more clear on the fiddle: http://www.bootply.com/gMBrLvaK5C
The problem is the 'rightSide' panel.
How can I keep that spacing between 'leftSide' and 'rightSide', and fix the overflowing of the right column (because the spacing gets too much if I try achieving spacing with columns)? Or what is the best way to achieve that?
this is my personal solution, instead of add a class on the columns, create a div inside of a column, then you can place a div.banner and div.block like my example:
http://www.bootply.com/PEIiDnp9VD
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="banner"></div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div class="block"></div>
</div>
<div class="col-md-4">
<div class="block"></div>
</div>
</div>
</div>
if you want less space between the columns you can simply override Bootstrap col-md-* class by adding a class on the columns changing the padding left and right.
First of all, you need to follow proper wrapping of rows and columns. If you want consistency you need to make sure all columns are wrapped into a row.
<div class="row">
<div class="col-md-8 leftSide">
<div class="row">
<div class="col-md-12">
</div>
</div>
<div class="row">
<div class="col-md-12">
</div>
</div>
</div>
<div class="col-md-4 rightSide">
<div class="row">
<div class="col-md-12">
</div>
</div>
</div>
</div>
Then apply custom margins and styling to elements inside of the columns, not columns themselves.
http://www.bootply.com/g6eLHRd1tH
I am using Bootstrap 3 and I am having issues to make small gap between two divs. I have tried offset but still its not working here is my code
.box1 {
background-color:black;
width:150px;
height:150px;
}
<div class="container">
<div class="row">
<div class="col-md-1 col-md-offset-0">
<div class="box1">
</div>
</div>
<div class="col-md-1 col-md-offset-1">
<div class="box1">
</div>
</div>
</div>
</div>
The problem is if set second div col-md-offset-0 it gets too close first div and if I set to 1 or above it sets very big gap. I want very small gap
Edited:
Try adding margin with a custom class-name on the col divs.
like so
<div class="container">
<div class="row">
<!-- custom-spacer is a custom class-name. to add extra margin. -->
<div class="col-md-1 col-md-offset-0 custom-spacer">
<div class="box1">
</div>
</div>
<div class="col-md-1 col-md-offset-1">
</div>
</div>
</div>
<style>
.box1 {
background-color:black;
width:150px;
height:150px;
}
.custom-spacer {
margin-right: 40px; /* Increase or decrease the margin as required. */
}
</style>
There could be one problem with this approach, because you are increasing the gutter size, you will not be able to create 12 cols within the same row.
I am really struggling with twitter bootstrap. I have the following spans, which add up to 12, yet the thrid item appears on a separate line.
<div class="container">
<div class="row">
<div class="span6 offset6">
<div class="span2">Contact</div>
<div class="span2">About Us</div>
<div class="span2">Blog</div>
</div>
</div>
</div>
Here is what I get...
Your typo is span 6, it should be span6
<div class="span 6 offset6">
Should be:
<div class="span6 offset6">
Not doing so will place a small div to the left of your three span3's, thus moving them a bit sideways.
Updated: You will need to place the div's in their seperate row or row-fluid div's to align them properly.
<div class="row">
<div class="span6 offset6"></div>
</div>
<div class="row">
<div class="span3"></div>
<div class="span3"></div>
<div class="span3"></div>
</div>
I am a newbie, I have following structure
<div class="row bottom">
<div class="col-md-4">
<div class="row">
<img src="images/logo-main-bottom.png" />
</div>
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-10">
</div>
</div>
</div>
</div>
How can I add/apply a CSS on all 'row' divs that comes under 'row bottom'?
a css rule for that would look like
.row.bottom .row{
//style
}
Take any one class row or bottom
.row{
//add required css
}