Do nested Bootstrap 3 rows need to be within a col-sm-* or can they also be nested within a col-md-, col-xs-, etc?
The documentation says col-sm-* but it doesn't appear to be explicit about whether nesting within other col sizes is forbidden.
http://getbootstrap.com/css/#grid-nesting
The col-sm-*you're referring to is just an example. It should really be col-*-* (xs, sm, md, lg).
Have a look just further down where the describe pushing and pulling with .col-md-push-* and .col-md-pull-*. Once again it's just an example, any column can be pushed and pulled.
Furthermore, a nested row doesn't necessarily need to be directly within the parent column. The important thing to remember is that every row is directly followed by a column, but nested rows can be wrapped with any element.
Examples:
<div class="row">
<div class="col-xs-12">OK</div>
</div>
<div class="row">
<div class="foo">
<div class="col-xs-12">NOT OK</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="row">
<div class="col-xs-6">OK</div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="foo">
<div class="row">
<div class="col-xs-6">OK</div>
</div>
</div>
</div>
</div>
You can use all class in the same time. On this base bootstrap decide which one is the best for screen.
You can nasted class inside all of types - it's depend what you need.
In TWBS3 the .col-xx-x classes only set width in terms of percentage, e.g. .col-sm-4 { width: 25% }, .col-md-4 { width: 25% } and .col-md-4 { width: 25% }.
Yes all the same, so you only need to define one.
But then why have all the variants? Two reasons:
It's because you may want to have the columns render different widths at different resolutions, e.g. .col-sm-4 .col-md-3 will render 1/4 width in small screen, but as 1/3 in medium, large and x-large screens.
The column will render down to it's 'mobile first' variant once the screen resolution falls below the chosen width. i.e. .col-sm-4 will render ΒΌ width in small, medium, lg, xl screens, but render full width at xs screens. Similarly, using only .col-md-4 with render 1/4 in md, lg & xl screens, but full width in sm & xs. Therefore, a class set like .col-sm-4 .col-md-4 is redundant.
So, with that knowledge - and in direct answer to your question: No, you only need one declaration, but be mindful of the size you select, as, when in smaller than your choice, the column will render full width (width: 100%)
Related
I don't know how to make this kind of col 3 and 6 size.
Middle column has no padding, but it is not enough.
I was trying to make different sizes of col.
#media (min-width:992px){
.col-md-6 { width: 52,5641%;}
.col-md-3 { width: 23,7179%;}
}
but no success.
With Bootstrap you dont need to add media queries or your own width, just use the BS grid system (you can read more here) and let it handle all the tough work. Based on your picture a 3 column layout would use something like this:
<div class="row">
<div class="col-md-3">.col-md-3</div>
<div class="col-md-6">.col-md-6</div>
<div class="col-md-3">.col-md-3</div>
</div>
Make sure you columns total 12 like above (3+6+3) If you need extra padding in between columns just add a nested DIV and apply the spacing you want to those.
<div class="row">
<div class="col-md-3">
<div class="myclass">
this will have extra padding
</div>
</div>
<div class="col-md-6">.col-md-6</div>
<div class="col-md-3">.col-md-3</div>
</div>
.myclass {
padding: 20px;
}
Updated
Based on your comment if you want column 6 to be slightly larger than it is you will either need to expand that column and "shrink" the outer 2 columns to something like this:
<div class="row">
<div class="col-md-2">.col-md-2</div>
<div class="col-md-8">.col-md-8</div>
<div class="col-md-2">.col-md-2</div>
</div>
If that's not what you are going for then you can create your own table within bootstrap.
<div class="row">
<div class="custom-col">My custom left side</div>
<div class="custom-main">my main column</div>
<div class="custom-col">My custom right side</div>
</div>
Sizing each of the column as you need.
Maybe Bootstrap is not the best option for your problem. It works if only you can divide the screen in 12 equal parts. Rewrite this rule could break other stuff.
What about using flexboxes or other CSS framework more flexible?
For the smallest xs size, even if I have the columns adding up to 12, they don't work as expected if screen width decreases beyond a certain limit. For instance:-
<div class="container">
<div class="row">
<div class="col-xs-2">
</div>
<div class="col-xs-1 col-xs-offset-8>
</div>
<div class="col-xs-1>
</div>
</div>
I would expect the if I decrease the size the last column will get stacked up to the end of the screen. However, this is not the case. As the screen decreases beyond a certain limit, the last column wraps up toward the beginning of the screen. I have thought about writing css code to give the container min-width. However, I thought that bootstrap might have a better way to handle this
This is a known issue: https://github.com/twbs/bootstrap/issues/13221
At screen widths <360px, the .col-xs-1 columns start to wrap because..
"while the column is set to width: 8.333333%;, the
column is going to be at least 30px wide because of the 15px padding
on either side. Therefore, there's a mismatch and the browser just
stacks the columns" - #mdo
The problem can be avoided by not using col-xs-1 on very small screens. You should also consider if the screen will realistically be resized less than 360px. In most cases it is not.
Note: In Bootstrap 4, col-xs-1 is now col-1.
Related: Bootstrap grid breaks in smallest size
It works well for me. But your code lacks a closing div and few quotation marks.
<div class="container">
<div class="row">
<div class="col-xs-2">
</div>
<div class="col-xs-1 col-xs-offset-8">
</div>
<div class="col-xs-1">
</div>
</div>
</div>
I have the following html:
<div class="row">
<div class="col-xs-10 col-xs-offset-1 col-sm-8">
...
</div>
</div>
The problem is that I get a 1 column offset even when the view port is larger than 768px (the xs size). Any ideas why am I getting this offset?
Because that's what you told it to do. As the Bootstrap docs say:
Grid classes apply to devices with screen widths greater than or equal
to the breakpoint sizes, and override grid classes targeted at smaller
devices.
So you can override your xs offset with a sm one:
<div class="col-xs-10 col-xs-offset-1 col-sm-8 col-sm-offset-0">
I have a grid layout for md+ displays which looks like (they are all in one row div):
A(col-md-8) | B(col-md-4)
C(col-md-8) | D(col-md-4)
For small and extra small displays I'd like to have:
B(col-sm-6) | D(col-sm-6)
A(col-sm-12)
C(col-sm-12)
Is it possible to do something like this with bootstrap grid system?
You can use nesting along with push pull like this..
<div class="container">
<div class="row">
<div class="col-md-4 col-md-push-8 col-xs-12">
<div class="row">
<div class="col-xs-6 col-md-12"> B </div>
<div class="col-xs-6 col-md-12"> D </div>
</div>
</div>
<div class="col-md-8 col-md-pull-4 col-xs-12">
<div class="row">
<div class="col-xs-12"> A </div>
<div class="col-xs-12"> C </div>
</div>
</div>
</div>
</div>
Demo: http://bootply.com/BS5Vuz0XEt
You can do it with a little be of redundant code.
<div class="row"">
<div class="hidden-xs hidden-sm col-md-8">COL A (MD and LG only)</div>
<div class="col-xs-6 col-md-4">COL B</div>
<div class="hidden-md hidden-lg col-xs-12">COL A (XS and SM only)</div>
<div class="col-xs-12 col-md-8">COL C</div>
<div class="col-cs-6 col-md-4">COL D</div>
</div>
You can get the B/D ordering the way you want without anything tricky, but that gets you:
A
B D
C
In order to get A where you like on XS/SM devices, you need to have that column twice and mark it hidden for certain sizes. Also note that I am only using -XS and -MD for the column widths. XS covers SM when no separate SM is specified, just like MD covers LG as well. The responsive utilities hidden-xx and visible-xx, however, need to be specified for each size.
Hope that helps. I know it isn't optimal.
Grid Classes
The Bootstrap grid system has four classes:
xs (for phones)
sm (for tablets)
md (for laptops)
lg (for desktops)
The classes above can be combined to create more dynamic and flexible layouts.
Tip: Each class scales up, so if you wish to set the same widths for xs and sm, you only need to specify xs.
Grid System Rules
Some Bootstrap grid system rules:
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
Predefined classes like .row and .col-sm-4 are available for quickly making grid layouts
Columns create gutters (gaps between column content) via padding. That padding is offset in rows for the first and last column via negative margin on .rows
Grid columns are created by specifying the number of 12 available columns you wish to span. For example, three equal columns would use three .col-sm-4.
Examples
Stacked-to-horizontal
Small devices
Medium devices
Large devices
(Source)
I want to create little panels/dashboard for my interface. In my case I want to have two panels like so
+-------------------------------+ +-------------------------------+
| | | |
| | | |
+-------------------------------+ +-------------------------------+
Generally it is easy with Bootstrap 3.
<div class="row">
<div class="col-md-5">
</div>
<div class="col-md-5 pull-right">
</div>
</div>
The problem is, the gap of col-md-2, as it is the case here, is way too big. I cannot use a col-md-1 gap, because then both sides do not have an equal size.
I also tried to add padding right and left, but that had not effect, too. What can I do here?
You could add a class which modifies the width of col-md-6. The width of this class is set to 50%. A smaller gap is achieved by reducing the width like so:
.dashboard-panel-6 {
width: 45%;
}
Add this to your div elements. This way the width rule of col-md-6 gets overriden.
<div class="row">
<div class="col-md-6 dashboard-panel-6">...</div>
<div class="col-md-6 dashboard-panel-6">...</div>
</div>
You can use another div inside and give padding to that.
<div class="row">
<div class="col-md-6">
<div class="inner-div">
</div>
</div>
<div class="col-md-6 pull-right">
<div class="inner-div">
</div>
</div>
</div>
.inner-div{
padding: 5px;
}
I posted this here already but it is still relevant the original question.
I have had similar issues with space between columns. The root problem is that columns in bootstrap 3 and 4 use padding instead of margin. So background colors for two adjacent columns touch each other.
I found a solution that fit our problem and will most likely work for most people trying to space columns and maintain the same gutter widths as the rest of the grid system.
This was the end result we were going for
Having the gap with a drop shadow between columns was problematic. We did not want extra space between columns. We just wanted the gutters to be "transparent" so the background color of the site would appear between two white columns.
this is the markup for the two columns
<div class="row">
<div class="col-sm-7">
<div class="raised-block">
<h3>Facebook</h3>
</div>
</div>
<div class="col-sm-5">
<div class="raised-block">
<h3>Tweets</h3>
</div>
</div>
</div>
CSS
.raised-block {
background-color: #fff;
margin-bottom: 10px;
margin-left: 0;
margin-right: -0.625rem; // for us 0.625rem == 10px
padding-left: 0.625rem;
padding-right: 0.625rem;
}
#media (max-width: 33.9em){ // this is for our mobile layout where columns stack
.raised-block {
margin-left: -0.625rem;
}
}
.row [class^="col-"]:first-child>.raised-block {
// this is so the first column has no margin so it will not be "indented"
margin-left: -0.625rem;
}
This approach does require an inner div with negative margins just like the "row" class bootstrap uses. And this div, we called it "raised-block", must be the direct sibling of a column
This way you still get proper padding inside your columns. I have seen solutions that appear to work by creating space, but unfortunately the columns they create have extra padding on either side of the row so it ends up making the row thinner that the grid layout was designed for. If you look at the image for the desired look, this would mean the two columns together would be smaller than the one larger one on top which breaks the natural structure of the grid.
The major drawback to this approach is that it requires extra markup wrapping the content of each columns. For us this works because only specific columns needed space between them to achieve the desired look.
Hope this helps
Here's another possibility:
Live view
Edit view
You will see that it uses 2 col-md-6, each with a nested col-md-11, and you position the nested row in the second div to the right.
The suggestion from Ken has clean HTML which I like. If your left and right panels use elements with widths defined by Bootstrap though (eg wells or form elements) the column padding could cause hassles and break the layout. This nested approach might be easier in this situation.
HTML
<div class="container">
<div class="row">
<div class="col-md-6">
<div class="row">
<div class="col-md-11">nested row col-md-11</div>
</div><!-- end nested row -->
</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-11 col-md-offset-1">nested row col-md-11</div>
</div><!-- end nested row -->
</div>
</div>
</div>
Good luck!