Empty columns in responsive grid? - html

I'm trying to learn the responsive grid and am having difficulty grasping how to place items within the "columns." I'm working with 12 of them, something that looks like the one here: http://www.w3schools.com/css/css_rwd_mediaqueries.asp
For example, I'd like to have one row with the name of the website on the left and the navigation bar on the right side:
<div class="row">
<div class="col-1">empty column acting as margin; do i need a div for this?</div>
<div class="col-2" id="name">
website name
</div>
<div class="col-4">another set of empty columns</div>
<div class="col-4" id="navbar">
Home
About
Page 3
</div>
<div class="col-1">empty column as margin</div>
</div>
How would I be able to achieve this? The divs with "empty columns" in them are clearly wrong, but I'm not sure how to get the proper widths. The link above says that the number of columns in each row should add up to 12; does this mean I need to specify empty columns, too?

Mayor browsers ignore width for empty divs, a simple way to avoid this behavior is adding to empty divs.
<div> </div>

The best solution using grid layout is to utilize its' power and not add empty elements in html (DOM).
In this example we want to have 12 columns in total, same width, where first and last are empty, as well mid four columns:
1 2 4 4 1
Html code:
<div class="grid-example">
<div class="col-2" id="name">
website name
</div>
<div class="col-4" id="navbar">
Home
About
Page 3
</div>
</div>
CSS code:
.grid-example {
...
grid-template-areas: ". nm nm . . . . nvb nvb nvb nvb .";
}
.grid-example #name {
grid-area: nm;
}
.grid-example #navbar {
grid-area: nvb;
}
Here is what this means:
In html there are only useful elements without redundant empty ones.
CSS is master here. Child div's are assigned to grid area with (short) names. Now using property grid-template-areas, #name div is set to spread on two columns, #navbar div on four columns. Dots sets respective columns empty. That's it.
see at CodePen >>
The similar principle could be used for rows, columns doesn't have to be the same, element could be assigned to any cells area etc. Basically grid is very useful to spread any element over any grid area.
Note: .row class has flex layout so not needed here.

The total number of columns should add up to 12 for a row as each column equals 1/12 of 100% (8.333%)
In your example you have the correct number of columns:
col-1 | column count: 1
col-2 | column count: 3
col-4 | column count: 7
col-4 | column count: 11
col-1 | column count: 12
Total Columns = 12
For the columns to display inline the column divs must have additional css to override the default display: block;
Option 1 (display inline block):
[class*="col-"] {
display: inline-block;
margin-right: -0.2em; /* small hack to fix inline block spacing */
}
Option 2 (float):
[class*="col-"] {
float: left;
}
If you go the float method you will need to use a clearfix.
Update
I would recommend Option 1 as you will run into less issues with clearfixes and empty columns. See this codepen

Another css only solution similar to Boris's good answer (https://stackoverflow.com/a/48133942/4236417) but using grid-column rather than grid-template-areas.
.grid-example {
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-column-gap: 1%;
border: 1px solid black;
}
#name {
grid-column: 2 / 5
}
#navbar {
grid-column: 8 / 12
}
<div class="grid-example">
<div id="name">
website name
</div>
<div id="navbar">
Home
About
Page 3
</div>
</div>
Don't forget that css grid columns actually refer to the lines between the columns so grid-column: 8/12 runs from beginning of the 8th column ("Grid Track") until the beginning of the 12th column/Grid Track.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Grid_Layout/Basic_Concepts_of_Grid_Layout#grid_tracks

Related

It is necessary to fill all 12 columns of a row of bootstrap's grid system?

It is necessary to fill all 12 columns of a row?
Example 1 - only 2 columns from a total 12 of the row are declared:
<div class="container">
<div class="row">
<div class="col-sm-2">
...
</div>
</div>
</div>
Example 2 - the unused columns are declared too:
<div class="container">
<div class="row">
<div class="col-sm-2">
...
</div>
<div class="col-sm-10"></div>
</div>
</div>
It is possible for a browser to scale the 2 example in different ways?
I tend to think that a good practice is the Example 2.
Since you're adding a new row it really doesn't matter. If you were using column wrapping where col units in each row may exceed 12 you would need the placeholder col-sm-10.
http://www.codeply.com/go/D1RLpfT8pD
IMO, the first one is preferred since it requires less markup. Also, if you're nesting columns the docs specifically state..
"it is not required that you use all 12 available columns"
No. You can use Bootstrap's offset columns.
If you don't want to use the space to the left of your column, use the offset column classes. If you don't want to use space on the right of your column, set the column to the width that you need it to be. As long as you do this in a .row you should be fine.
In the example below I am not using 4 of the 12 columns. We're using offset to "ignore" two columns on the left and setting the column to 8 instead of 10 to "ignore" two columns on the right. This effectively centers the DIV in the row. No need for extra markup. Try it on a desktop viewport.
#import url( 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css' );
.container {
border: 1px dashed red;
}
.col-md-offset-2 {
border: 1px dashed blue;
}
<div class="container">
<div class="row">
<div class="col-md-offset-2 col-md-8">
I am a 8 column DIV.
</div>
</div>
</div>

Twitter bootstrap grid columns touching eachother?

I am currently working on my own responsive grid with columns. I am currently using 12 columns and I give each column a multiplier of 100/12 and subtract a margin on each column. So I make each column a little bit smaller so I can fit in a margin-left and make gutters this way.
#for $i from 1 through $column-count {
.column-#{$i} {
width: (((100 + $gutter-width) / $column-count) * $i) - $gutter-width;
}
}
This leaves me with something like this:
.column-1 {
width: 7.41667%; }
.column-2 {
width: 15.83333%; }
.column-3 {
width: 24.25%; }
etc.
This way I can set a margin-left for each column and just remove the very first margin-left in each row with the first:child selector
Twitter bootstrap however uses a column grid where all columns touch eachother and they use padding to kind of fake column gutters. However I tried some fooling around with bootstrap and you run into a problem when you actually set like a background for a column. Immediately you can see that the columns will touch. How do people prevent this from happening?
I guess using another element would inside the column would help? This however would get a bit messier than I would like probably.
I am asking this because I am looking for a solution since my grid can only go from default columns to 100% width columns when you downsize it.
grid: http://titan.ravenwebdesign.nl/
I would like to be able to add something like .mobile-column-6 to certain grid columns in a manner that bootstrap has with it's grid tiers of classes.
This would be helpful in for example having 2 columns next to each other when in downsized view. Except I'm currently using rows and every first child of my row has the margin removed. So it would't work like this:
<div class="row">
<div class="column-3 mobile-column-6"></div>
<div class="column-3 mobile-column-6"></div>
<div class="column-3 mobile-column-6"></div>
<div class="column-3 mobile-column-6"></div>
</div><!-- .row -->
Because of my responsive design which says that the first column will have it's margin removed I can't simply turn it into a 2 columns next to eachother design.
Hopefully somebody can help me out here although I understand this problem is extremely hard for me to explain like this.

Bootstrap 3 grid, does it *really* matter how many columns are in a row?

I have a form layout that has bootstrap 3 form groups on it. I want these form groups in a single column on < small, 2 columns on tablet size break and 4 column on larger screens.
I have it apparently working perfectly, however after doing some reading here what I did seems to violate the supposed rule that every column in a row must add up to 12. However every tutorial and docs I could find always use weasel words like "should" or "ideally" when saying it should add up to 12. There doesn't seem to be a clear guidance here.
I defined my groups like this:
<div class="row">
<div class="form-group col-md-6 col-lg-3" ><!--....etc-->
and I currently have 4 of these in each row. This means that the 4 * col-lg-3 adds up to 12 but the 4 * col-md-6 form group divs adds up to 24 not 12.
However this doesn't seem to matter and it works perfectly at each breakpoint.
Should I be concerned? Does it matter in any way? Surely I'm not supposed to do two completely different layouts that duplicate all these controls once each for col-md-6 and col-lg-3 on the same page?
No, there's nothing to mandate that the bootstrap grid must add up to 12 columns.
It's just a preference / stylistic thing.
Less than 12 Columns -> Aligns Left:
If you have less than twelve of the columns filled, by default they will left align, leaving empty space to the right.
The following code:
<div class='row'>
<div class="col-xs-2">Hi</div>
<div class="col-xs-2">Hi</div>
</div>
.row > div {
background: lightgrey;
border: 1px solid grey;
}
Results in this: (Demo in Fiddle)
More than 12 Columns -> Wraps:
If the total adds to more than 12 columns, as long as the columns are within a row class, they will just wrap to additional rows. A good use case would be a photo grid system where you would like to add tons of photos but don't know how many rows you'll have and would still like to define the number of columns.
The following code:
<div class='row'>
<div class="col-xs-6">Hi</div>
<div class="col-xs-6">Hi</div>
<div class="col-xs-6">Hi</div>
<div class="col-xs-6">Hi</div>
</div>
Results in this: (Demo in Fiddle)
Other than that, sometimes it's the case that 12 column layouts look better, but you don't have to use them; it's not a sudoku :)
4 * col-md-6 will create 2 "rows" on a medium screen. Since you are wrapping everything in a .row it will function as expected. If you removed that then things will nest next to each other
e.g.
<div class="row">
<div class="col-sm-2">Hello</div>
</div>
<div class="row">
<div class="col-sm-2">Hello</div>
</div>
will produce 2 rows with 1 column each that are each 16.67% as wide as the parent row and floating left because a row has 100% width. Column widths are specified by col-**-(number/ 12) as a percentage (2/12 = 0.166667)
<div class="col-sm-2">Hello</div>
<div class="col-sm-2">Hello</div>
will produce 1 row with 2 columns that are each 16.667% as wide as the parent object. So you could forgo the .row if you did
<div class="col-sm-12 col-md-6 col-lg-3></div>
<div class="col-sm-12 col-md-6 col-lg-3></div>
<div class="col-sm-12 col-md-6 col-lg-3></div>
<div class="col-sm-12 col-md-6 col-lg-3></div>
This will create: Small Screen ( 4 rows full width columns) Medium Screen( 2 rows 2 columns each 50% width) Large Screen ( 1 row 4 columns each 25% width)
I've seen a lot of website use "col-xs-12 col-md-10 col-md-offset-1" lately. The offset centers the content, yet each row is filled with just 11 cols.
If the next element is a col-1 it must include col-offset-1 as well (Forcing a total of 13, moving the col-1 with offset-1 to the next row, aligning nicely). If the next element is wider, it still needs the offset (To skip the first column).
On the CSS side of it, it all works with percentages. If the total width of the columns is over 100% it has to force the next element on a new line.
U can easily play with this concept by making a html file with a whole lot of divs.
Add the following css to the divs (bootstrap basics)
body > div {
margin: 0;
padding: 0;
float: left;
width: 8.333333% (or 1/12th)
}
Then u can change the size of indivual divs to see what happens. It might be easier to play with round values (eg. 10%/20%), just sticked to bootstrap here.
Hope this gives you an understanding of how the browser handles the bootstrap grid (which you just made a basic version of)
The Bootstrap docs on columnwrapping also give a nice example.

is bootstrap less mixin .makeColumn smaller than a span?

I'm using bootstrap with less and I'm currently trying to make web semantic.
HTML part:
<!-- our new, semanticized HTML -->
<div class="article">
<div class="main-section">...</div>
<div class="aside">...</div>
</div>
Less part:
/* its accompanying Less stylesheet */
.article {
.makeRow(); // Mixin provided by Bootstrap
.main-section {
.makeColumn(10); // Mixin provided by Bootstrap
}
.aside {
.makeColumn(2); // Mixin provided by Bootstrap
}
}
But when I take a look at the rendered html page... my article take less space than a span12
I can also put a .makeColumn(10) and .makeColumn(4) and it will stay on the same line.
It's like it was a 14 grid column and not a 12 grid column.
Am I missing something?
No. If your required number of columns is n, the .makeColumn mixin and the .span (#grid > .core > .span) mixin both calculate width as follows:
(#gridColumnWidth * n) + (#gridGutterWidth * (n-1))
which is the width you'll need to set your element to a width of n columns on the grid.
If n = 6, it calculates all column widths and gutter widths from the left edge of your grid to the right-hand edge of column 6. 6 columns and 5 gutters.
.span only does width, .makeColumn also adds a float: left, and has an optional #offset argument that which isn't important to your current problem, but which allows you to add columns to the left of the element by adding a margin: left.
As #sody says above, wrapping your existing elements in a .container should fix your problem.
And I agree with #kleinfreund on using the html elements where you can:
<div class="container">
<article>
<div class="main-section">Bootstrap rocks
<aside>
By the way...
</aside>
</div>
</article>
</div>
I hope that helps.

CSS columns - more elements than columns

I'm working on a layout with 2 columns. But, I'm having some issue with doing it, since my templates are quite complicated, and I'm unable to have one div per column.
For example, I might have something like:
<div class="column left">
left column - part 1
</div>
<div class="column right">
right column - part 1
</div>
<div class="column right">
right column - part 2
</div>
<div class="column left">
left column - part 2
</div>
And what I'd like to do is create two columns, with the same width and without empty holes (vertically) between them. Normally, it wouldn't be a problem to accomplish with:
<div class="column left">
left column - part 1
left column - part 2
</div>
<div class="column right">
right column - part 1
right column - part 2
</div>
Furthermore, my goal is to have a solution which works in dead browsers, like IE7. But, I'd also like to see solutions which are supported only in newer browsers, because I think there might be some interesting solutions. Of course, there are solutions through JS, e.g. merging elements of all .column.left in one div, and the same thing for .column.right, but CSS solutions would be better.
Is having the divs from different columns in none specific order a requirement? If so, then you can't accomplish it by pure css, because of the need to put some elements above the ones from another column. See Use CSS to reorder DIVs.
If that's not the issue, try writing all left column divs before right column divs in the code and use following css:
.left {
float: left;
width: 100px;
clear: left;
}
.right {
margin-left: 100px;
}
It creates a two-column layout. That method works particularly well when one of the columns if of contant width, but you can achieve whatever you want by changing 100px to for example 50%. Example here: http://jsfiddle.net/uQwUT/.