Responsive layout using CSS Grid
CSS Grid was designed to make it easier to lay out components on a page using standard CSS. It divides the page into a grid composed of rows and columns, like a table, which is similar to Bootstrap’s row- and col- classes. The added bonus is we don't have to download or reference an external library as it is built-in to CSS 3.
As an example, let’s create an HTML page but we are not going to compile.
Let us create views\cssgrid.html:
The line
grid-template-columns: 150px 150px 150px;
means we are making three columns, with each column 150 pixels wide.
The line
grid-template-rows: 200px 200px 200px;
means we are making three rows, with each row having a height of 200 pixels.
This way, we defined a three-by-three grid, or three rows with three columns.
Do not compile. Simply open it in File Explorer to open it in the browser, then resize the browser and make it small:
Then expand the browser:
You see that when you resize the browser, the blocks remain the same sizes.
Now let’s change the CSS to make the center column expandable.
Refresh the browser. This time, when you expand or contract the browser, the middle column resizes automatically.
Now let’s make the middle row expandable too.
And resize the browser.
Now both the center column and the middle row are expandable.
We are just skimming the surface of CSS Grid here. There are so many variations in the use of CSS grid, but for now let us settle for this rudimentary knowledge. At least we are armed with the knowledge to develop the fixed navbar and the sticky footer without Bootstrap.
Last updated