Reputation: 28148
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.
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?
Upvotes: 0
Views: 1369
Reputation: 2268
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>
Upvotes: 1
Reputation: 2200
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;
}
Upvotes: 2
Reputation: 115108
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>
Upvotes: 2