How I Use CSS Grid and Flexbox To Create A One
How I Use CSS Grid and Flexbox To Create A One
While we can use either CSS Grid and Flexbox for a webpage layout, CSS Grid tends to be
used for page layouts while Flexbox tends to be used for aligning UI elements. Let’s create
a webpage to see how this pans out in real code. I’ll be using a common design as below:
Step 1 — Plan before coding
The trick to coding layouts is to plan beforehand.
I’ll identify the parent and child elements first based on the
layout method. Below are my workings based on the design.
Create a guide
After determining the number of columns and rows, I now
have a guide that I can follow when coding. In it, I’ll also
mark out the grid lines and note down the parent and child
elements.
Step 2 — Create the HTML file
For a simple layout, I’ll type out all the html before I work
on the CSS.
Set-up
I remove the default margin and padding
of html and body elements, and add a height of 100%.
Create the grid
Next, I specify the width of grid rows and the height of grid
columns.
It just means that it starts on the first vertical line, and ends
on the third vertical line.
Mobile view
Lastly, I work on the mobile view.
Grid
• display: grid
Flexbox
• display: flex
• flex-direction
• flex-grow
Resources
• Basic Concepts of Flexbox by MDN
• Flexbox by MDN
• CSS Grid Layout by MDN
• My Codepen with the codes I used in this article