Mastering Modern Css Layouts
Mastering Modern Css Layouts
Mastering Modern
CSS Layouts
Everything you need to Master
the Powerful Flexbox &
Grid CSS Layout Models
Ohans Emmanuel
Preface
Like a lot of front-end developers, my face beamed with
smiles as I found out I didn’t have to write a lot of hackish,
rather messed up CSS anymore. There was this thing called
Flexbox and I was determined to learn how it worked.
Did the Flexbox model fool me? Oh yes it did! It looked quite
different from what I was used to and I got very confused many
times. This book however tells that I found a way around my
confusion, but that took lots of hours and on many different
days.
And the CSS Grid model? If you’ve played around with this,
then you already know it takes a complete re-working of how
you’ve viewed CSS layouts. It’s perhaps the more confusing of
the two - at least for me, when I first got started.
Every decent endeavor takes some time but It’s the sole aim of
this book to guide to mastery, so you can get up and running
in less the time it’d normally take and with many practical use
cases for today!
Acknowledgment
It can be a pain in the neck to self publish a book. For that
reason I must appreciate some really amazing people without
whom this book wouldn’t be possible.
This book assumes you know a bit of CSS to get along, but no
previous knowledge of the Flexbox, Grid model or responsive
design is assumed.
Pathway
This book is divided into three parts.
Part 1 is an introduction to the Flexbox and Grid model. Let’s
say it’s a look at these powerful layout models first from a
conceptual perspective. If you have been asking questions
like what’s Flexbox? How do I understand the syntax of the
Grid layout? What can be done with Flexbox and Grids? All
these questions (and more) will be answered in this section of
the book. Part 1 aims to give you a strong understanding of
both the Flexbox and Grid model.
And after knowing all this, you may close the book. You’ll be
able to make your colleagues look clueless, at least when it
comes to using the Flexbox & Grid models - I hope that’s not
such a bad thing
Code conventions
This book comes with a lot of copious examples that show in
great detail the topics covered. The source code in listings or
in text appears in a fixed-width font like this to separate it
from ordinary text.
PART 1 INTRO
PART 2: PRACTICAL
Practical
A ye! You made it to the second art of the show and, like
most intermediary scenes, It is packed full with creative punch
lines and surprises. Perhaps, I’ve saved up the better for this
section!
5
• Using CSS media queries for custom
styles on selected devices
• How certain Flexbox properties are
well suited for dealing with mobile
devices
Here’s what the finished layout looks like. Nothing fancy, like I
said. You may need to zoom in on this image though.
You may not have noticed any responsiveness here but you
may view this in your browser and try resizing to see the
responsiveness in action. We will start off with building the
responsive navigation for the website.
For clarity, I’d like to show here what we are looking to achieve.
On laptops and bigger screens we want the navigation to be
displayed like this:
Before we delve into the styling, let’s have a look at our html
file for the Navigation bar.
<nav class=”fnf-nav”>
<h1 class=”fnf-nav-brand”>Flex & Furious</
h1>
<ul class=”fnf-nav-main”>
<li><a href=”#”>Speed</a></li>
<li><a href=”#”>How</a></li>
<li><a href=”#”>Why</a></li>
<li><a href=”#”>About</a></li>
</ul>
</nav>
Also note the convention I’m using here. The nav element has
a class of “fnf-nav” where fnf stands for Flex & Furious. The
class names for the h1 element and unordered list are easy
to wrap your head around, .fnf-nav-brand and .fnf-nav-
main.
Now that we have our html file in place, it’s a good time to
begin styling the navigation. For now, I’d leave things simple.
No Flexbox styles - just some basic initial styling to get us off
the ground.
ul.fnf-nav-main {
border: 1px solid #da4453;
}
And bravo! We’ve got the navigation bar good to go. The
listing above sets relevant spacing within the nav element,
adds a reddish border to the unordered list and all list items.
The list items which hold the links are then spaced appropriately
too with padding: 0.5em 1em, and finally, when a user has
their cursor over the list items (on hover), the list items take
a transparent background. No colors, just plainly see through
the lists. background-color: transparent.
I know this isn’t particularly exciting yet - just hang on. Flexbox
coming soon!
h1,
h2,
h3,a {
color: #fff;
font-weight: 400;
letter-spacing: .1em;
}
main,
aside {
padding: 1.5em;
}
Since you do have the source files, delete and edit the values
around whatever you do not understand and see how it affects
the page. I’m sure that will answer your questions coupled
with a bit of a Google search - we all use Google! Please do
a Google search, it sure saves me a lot of beating around the
bush.
And here comes the fun part. We may now move on to cater for
larger screens. If it’s now fuzzy what we are trying to achieve,
please see the exercise files or scroll to earlier pages and view
the screenshots earlier discussed.
Okay, that’s not how we actually write it css, but that’s how
the media query thing works. So how do we write this in css ?
Looking at it, you can almost guess what that does. For an
handheld device with a maximum width of 300px ... do this
and that (to be written in the code block - where the “write
style here” comment exists). Any styles within the code block
will only apply to devices that match the expression handheld
and (max-width: 300px)
Breakpoints
Hold up 5 tablets or laptops from different manufacturers and
you’d easily notice they most likely all have varying screen
sizes. I said we’ll be using media queries, and we need some
specification we can bank on to target a large share of all
tablets and laptops. This spec is what we call a breakpoint.