There are a number of grid frameworks for use around the web. Common features to look for in the grid system are:
I chose to use CSS Smart Grid for the Bear Necessities class demo. The syntax is fairly intuitive and the grid can be nested. It's responsive and it doesn't rely on a CSS preprocessor (although it can work with one). One major downside is that only has a 12 column implementation.
The Smart Grid code is hosted on GitHub (like most web frameworks and helpers). Go to the GitHub page linked from the Smart Grid home page. We only need the base Smart Grid CSS file for our sites since we're not using any of the advanced features.
smart-grid.css
filesmart-grid.css
Put smart-grid.css file in your CSS folder with the rest of your CSS files.
Use the link tag in your HTML to link to the style sheet. You should link to the grid system after your reset/normalize, but before your own styles.
Smart Grid is responsive, but it allows for a set max width. To do this, wrap your page inside a container
class. You should use a div
tag for this.
<body>
<div class="container">
<p>This is where my content goes.</p>
</div>
</body>
The Smart Grid framework contains the following CSS which will center your layout and set a max-width:
.container {
max-width: 940px;
padding: 0 1.06383%;
margin: 0 auto
}
You can overwrite the max-width property in your own stylesheet to whatever you want. For example, to set the max-width to 1180px, you would use this in your stylesheet:
.container {
max-width: 1180px;
}
Smart Grid uses a system of classes to create rows an grids. To tell an element to fit into a column, add the class columns
to it. By default, an element will take up one column.
<body>
<div class="container">
<p class="columns">One column paragraph.</p>
<p class="columns">One column paragraph.</p>
<p class="columns">One column paragraph.</p>
</div>
</body>
There are twelve total columns. You can add classes with the numbers one-twelve to specify how many columns an element should take up. Note that the numbers should be spelled out.
<body>
<div class="container">
<p class="six columns">Six column paragraph.</p>
<p class="four columns">Four column paragraph.</p>
<p class="two columns">Two column paragraph.</p>
</div>
</body>
To incorporate more than one row of columns, you must use a class of row. This ensures that each row is "cleared" and that they don't bump into each other. This is usually down with a separate element.
<body> <div class="container">
<div class="row">
<p class="one columns">One column paragraph.</p> <p class="one columns">One column paragraph.</p> <p class="one columns">One column paragraph.</p>
</div> <div class="row">
<p class="six columns">Six column paragraph.</p> <p class="four columns">Four column paragraph.</p> <p class="two columns">Two column paragraph.</p>
</div>
</div> </body>
In more complex layouts, it usually makes sense to add the main rows and columns in their own elements (using the div
tag). This will simplify your CSS. We're using div
tags because these classes have no semantic value.
<body>
<div class="container">
<div class="row">
<div class="twelve columns">
<header>
...header stuff...
</header>
</div>
</div>
<div class="row">
<div class="eight columns">
<article>
...main content stuff...
</article>
</div>
<div class="four columns">
<aside>
...aside content stuff...
</aside>
</div>
</div>
<div class="row">
<div class="twelve columns">
<footer>
...footer content stuff...
</aside>
</div>
</div>
</div>
</body>
Smart Grid also contains functionality for shorthand classes for common use cases: one-fourth, three-fourths, one-third, two-thirds, and one-half.
<body> <div class="container"> <div class="row">
<div class="two-thirds columns">
<article> ...main content stuff... </article> </div>
<div class="one-third columns">
<aside> ...aside content stuff... </aside> </div> </div> </div> </body>
You can create blank columns or offset columns by using offset-xxxx classes. The possible classes are: offset-one, offset-two, offset-three, offset-four, offset-five, offset-six, offset-seven, offset-eight, offset-nine, offset-ten, and offset-eleven.
<body> <div class="container"> <div class="row">
<div class="offset-two five columns">
<article> ...main content stuff... </article> </div>
<div class="offset-three two columns">
<aside> ...aside content stuff... </aside> </div> </div> </div> </body>