I am trying to create a 2 column grid, with literally a 50% with no margins or padding.
How do I achieve this with Bootstrap 3 I tried this but end up with negative
Simple you can use bellow class.
.nopadmar {
padding: 0 !important;
margin: 0 !important;
}
<div class="container-fluid">
<div class="row">
<div class="col-md-6 nopadmar">Your Content<div>
<div class="col-md-6 nopadmar">Your Content<div>
</div>
</div>
The answer given by @yuvilio works well for two columns but, for more than two, this from here might be a better solution. In summary:
.row.no-gutters {
margin-right: 0;
margin-left: 0;
& > [class^="col-"],
& > [class*=" col-"] {
padding-right: 0;
padding-left: 0;
}
}
Another option would be to create your own special CSS class for whenever you want to apply the "gutterless" columns..
HTML
<div class="container">
<div class="row no-gutter">
<div class="col-6 col-sm-6 col-lg-6">Col 1</div>
<div class="col-6 col-sm-6 col-lg-6">Col 2</div>
</div>
</div>
CSS
.no-gutter [class*="-6"] {
padding-left:0;
}
Demo: http://bootply.com/73960
Generalizing on martinedwards and others' ideas, you can glue a bunch of columns together (not just a pair) by adjusting padding of even and odd column children. Adding this definition of a class, .no-gutter
, and placing it on your .row
element
.row.no-gutter > [class*='col-']:nth-child(2n+1) {
padding-right: 0;
}
.row.no-gutter > [class*='col-']:nth-child(2n) {
padding-left: 0;
}
Or in SCSS:
.no-gutter {
> [class*='col-'] {
&:nth-child(2n+1) {
padding-right: 0;
}
&:nth-child(2n) {
padding-left: 0;
}
}
}
The grid system in Bootstrap 3 requires a bit of a lateral shift in your thinking from Bootstrap 2. A column in BS2 (col-*
) is NOT synonymous with a column in BS3 (col-sm-*
, etc), but there is a way to achieve the same result.
Check out this update to your fiddle: http://jsfiddle.net/pjBzY/22/ (code copied below).
First of all, you don't need to specify a col for each screen size if you want 50/50 columns at all sizes. col-sm-6
applies not only to small screens, but also medium and large, meaning class="col-sm-6 col-md-6"
is redundant (the benefit comes in if you want to change the column widths at different size screens, such as col-sm-6 col-md-8
).
As for the margins issue, the negative margins provide a way to align blocks of text in a more flexible way than was possible in BS2. You'll notice in the jsfiddle, the text in the first column aligns visually with the text in the paragraph outside the row
-- except at "xs" window sizes, where the columns aren't applied.
If you need behavior closer to what you had in BS2, where there is padding between each column and there are no visual negative margins, you will need to add an inner-div to each column. See the inner-content
in my jsfiddle. Put something like this in each column, and they will behave the way old col-*
elements did in BS2.
jsfiddle HTML
<div class="container">
<p class="other-content">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse aliquam sed sem nec viverra. Phasellus fringilla metus vitae libero posuere mattis. Integer sit amet tincidunt felis. Maecenas et pharetra leo. Etiam venenatis purus et nibh laoreet blandit.</p>
<div class="row">
<div class="col-sm-6 my-column">
Col 1
<p class="inner-content">Inner content - THIS element is more synonymous with a Bootstrap 2 col-*.</p>
</div>
<div class="col-sm-6 my-column">
Col 2
</div>
</div>
</div>
and the CSS
.row {
border: blue 1px solid;
}
.my-column {
background-color: green;
padding-top: 10px;
padding-bottom: 10px;
}
.my-column:first-child {
background-color: red;
}
.inner-content {
background: #eee;
border: #999;
border-radius: 5px;
padding: 15px;
}
I always add this style to my Bootstrap LESS / SASS:
.row-no-padding {
[class*="col-"] {
padding-left: 0 !important;
padding-right: 0 !important;
}
}
Then in the HTML you can write:
<div class="row row-no-padding">