Responsive Column Layouts 124
Typically, to create a column layout, you would need to add the first or last classes to reset the margin space and clear the float. Today I'm going to share a very simple CSS trick to create a responsive column layout using nth-of-type pseudo class. I use this trick to code the WordPress themes at Themify. It doesn't require any first or last class and the number of columns can be adjusted base on the viewport. In other words, it can be toggled from 4-column to 3-column or 2-column, etc.
View Demo Responsive Column/Grid
The Inconvenience of Using The First & Last Classes
Normally we would add a .first or .last class to clear the margin space and float in the grid. Adding the first and last class is very tedious, not to mention that it gets more complicate if you need to make it responsive.
Using nth-of-type
The :nth-of-type(An+B) expression makes it very easy to clear the float and margin without having to add .first or .last class. For example:
.grid4 .col:nth-of-type(4n+1)= target every 4th .col element, starting from the first.grid3 .col:nth-of-type(3n+1)= target every 3rd .col element, starting from the first.grid2 .col:nth-of-type(2n+1)= select every 2th .col element, starting from the first
.grid4 .col:nth-of-type(4n+1),
.grid3 .col:nth-of-type(3n+1),
.grid2 .col:nth-of-type(2n+1) {
margin-left: 0;
clear: left;
}
Making it Responsive With Media Queries
To make it responsive and fluid, use percentage value instead of pixel value.
/* col */
.col {
background: #eee;
float: left;
margin-left: 3.2%;
margin-bottom: 30px;
}
/* grid4 col */
.grid4 .col {
width: 22.6%;
}
/* grid3 col */
.grid3 .col {
width: 31.2%;
}
/* grid2 col */
.grid2 .col {
width: 48.4%;
}
Changing From 4-column to 3-Column
To change the 4-column to 3-column on viewport width that is less than 740px:
- change the .grid4 .col width to 31.2% (one-third width)
- reset the left margin and clear property
- then re-apply the left margin and clear property using nth-of-type(3n+1) to form a 3-column grid
@media screen and (max-width: 740px) {
.grid4 .col {
width: 31.2%;
}
.grid4 .col:nth-of-type(4n+1) {
margin-left: 3.2%;
clear: none;
}
.grid4 .col:nth-of-type(3n+1) {
margin-left: 0;
clear: left;
}
}
Changing 4-column and 3-column to 2-column
To switch the 4-column and 3-column to 2-column on viewport width that is less than 600px: basically use the same trick as above to reset the .col width and float.
@media screen and (max-width: 600px) {
/* change grid4 to 2-column */
.grid4 .col {
width: 48.4%;
}
.grid4 .col:nth-of-type(3n+1) {
margin-left: 3.2%;
clear: none;
}
.grid4 .col:nth-of-type(2n+1) {
margin-left: 0;
clear: left;
}
/* change grid3 to 2-column */
.grid3 .col {
width: 48.4%;
}
.grid3 .col:nth-of-type(3n+1) {
margin-left: 3.2%;
clear: none;
}
.grid3 .col:nth-of-type(2n+1) {
margin-left: 0;
clear: left;
}
}
Making all columns Fullwidth (see demo)
To make all columns to full width on viewport width that is less than 400px: set width to 100% and reset the margin and float.
@media screen and (max-width: 400px) {
.col {
width: 100% !important;
margin-left: 0 !important;
clear: none !important;
}
}
Internet Explorer Issues
Both media queries and nth-of-type are not supported by Internet Explorer 8 or older. You can use selectivizr.js to provide nth-of-type support for IE and respond.js for media queries. Unfortunately, selectivizr.js and respond.js don't work well together (ie. nth-of-type doesn't work within the media queries). This means the only fall back with this responsive grid is that the columns can not be switched from 4-column to 3-column or 2-column.
Thanks for sharing this. I’ve been putting off a project for a while now trying to figure this out in my head. All systems are now go… :D
Anyone would pay lot of cash for the advice provided in the blog, i know i would. Permanently I am learning something or the other through this blog. Any latest gossip is said in this blog, says a much about sources in high places. Loving the ease, how I can pass my suggestions without expressing my identity. Certainly I am coming back to read what else the writer has to show. A plus for examples which can be visited with a banal click. I like how the blog confers the best suggestions on anything. The topic titles makes you want to see more. I like the way the author persuades you step by step. This blog has a mood of its own so to speak. It makes you feel connected to the author in manners more than one. After you read a n article you can’t wait to read another one for the comments provided. Frequently gives you the impression : why I didn’t guess of that. The site is a literary masterpiece. The global look of the blog is agreeable and different. The author does not beat around the bush, he gets upright to the point. There’s a positive feel about the site that i like greatly. This blog posts are the absolute words on this topic. Using befitting titles to the site articles. What you read there today is what you hear from everyone in the upcoming days. You can learn very much from reading the opinions and feedbacks.
A positively delightful example of classic spam. Contains a lovely praising message, long and in-depth, but doesn’t actually address the subject at hand at all. Notice how the ‘author’ would pay money for the content, thinks the site is a literary masterpiece, but doesn’t actually mention they like CSS or column layouts.
I.e. Dude, you’re getting^H^H^H^H^H^H^H^H^H^Hve got a spam problem.
A plus for examples which can be visited with a banal click. I like how the blog confers the best suggestions on anything
Hi, I found a better solution to Your problem.
Try it at: http://flamecity.piasta.pl/columns
Any feedback appreciated :)
Not an ideal solution because it requires left and right margin which causing the left and right empty space.
test
Wow that’s easy. I did it!
That’s really useful.
As for IE8 support, I guess that selectivizr alone is good enough. Because it runs only only on desktop PCs, I think respond.js is overkill.
The tutorial is simple but awesome, as it does not show the usual “n-columns to 1-column on mobile” method, but a much more useful (and usable) column layout reflow based on screen size.
I think a potential drawback could be its use on layouts which contain a sidebar – in that case the column reflow could cause unwanted behaviours. Perhaps a .sidebar class which does not reflow could be a solution.
Wish I had the time to build a framework from this. :)
“Both media queries and nth-of-type are not supported by Internet Explorer 8 or older.”
Dinosaurs…
Thanks
That’s easy. I did it!
We will professionally design the website, smart phone, print any cards, flyers or
posters you may need as marketing material for your business.And the result has been really impressive.
Great info and very simplified information!!
Excelente blog y muy completo este post en especial.
Thanks
That’s easy method. I did it!
test
I am learning something on his blog. Responsive its the way to go, but lot of things that is hard to be understood.
Nice CSS Trick. It allows easy creation of responsive CSS Column Layouts.
Brilliant! Thanks for sharing :)
Works well in Chrome, but its a no go in FF. Can you confirm this just in case I’m doing something wrong.
Great blog post! Hoverer, the font-size:0 tricks don’t work within all user agents when the minimum font has been set.
Excellent post. :)
It’s a good approach, but I prefer to use :first-child and margin-left:0 on first child and on all remaining I set margin-left. In this case you don’t need any classes and number of columns you regulate with parent div width.