CSS - Web Page Layout
laying out the content of a web page
- Overview
- Container Elements
- Calculating Widths
- Block vs. Inline Elements
- A Word About Divs and Spans
- Position
- Grid
- Flexbox
- Conclusions
Overview
The layout of a web page is controlled by a few simple CSS concepts and rules. In order to get it right, we need to understand a few preliminary concepts.
Containers
The HTML
To center the contents of a web page and to restrict the width of the page, we wrap the entire contents of the paage within a container or wrapper element.
...
<body>
<div class="container">
<!-- put the contents of your page within this element -->
</div>
</body>
...
Restrict the page width
Now, we can easily restrict the page width to whatever width we like by adjusting this container’s width.
.container {
width: 800px; /* you choose the width */
}
Center the contents
We can also easily center the entire page within the browser’s viewport using margins.
.container {
width: 800px; /* container width must be smaller than the browser viewport width */
/* equal margins on the left and right side of the containere */
margin-left: auto;
margin-right: auto;
}
Warning: avoid setting heights
The height of the container, or any element for that matter, will always automatically adjust to fit the height of the contents you put within it.
If you were to set the height of an element to a fixed size, yet place more content nested within that element than can fit in that size, your layout would become corrupted.
Calculating Widths
The question
The width of an element can be a bit counter-intuitive to calculate.
Take this HTML:
<article class="boxy">...</article>
And this CSS:
.boxy {
width: 400px;
border: 1px solid red;
padding: 20px;
margin: 10px;
background-color: pink;
}
What’s the width of the article
element?
An answer
The actual width of an element includes:
- the width of the element!
- any border applied to either side of it
- any padding applied to either side of it
actual width = width +
left border + right border +
left padding + right padding
Another answer
The space consumed by an element also includes:
- any margin applied to either side of it
actual width = width +
left border + right border +
left padding + right padding +
left margin + right margin
Block vs. Inline Elements
Concept
Layout behavior of HTML elements is largely determined by the value of their CSS display
property. Let’s focus first on 3 of these:
…
display: block;
display: inline;
display: none;
…
Wwe will discuss two more important display
values, grid
and flex
, later.
Common block elements
By default, the display
property is set to block
for many elements, including:
header
footer
main
aside
article
section
h1
-h6
p
ul
andol
li
div
- … and more
Block element behavior
The display: block
setting causes the elements to have the following behavior:
- consume as much width as they can within their containing element
- refuse to sit side-by-side with other elements
Common inline elements
By default, the display
property is set to inline
for many text-related elements and images, including:
a
strong
em
img
span
- … and more
Inline element behavior
The inline
value will cause the elements to have the following behavior:
- consume only as much width as they need to surround the contents nested within them.
- happily sit side-by-side with other elements.
Effects
The block/inline difference means that…
- we cannot set the widths of inline elements
- we also cannot set the left or right padding of inline elements
- we must use
block
elements to build the structure or skeleton of our web pages - we will have to do extra work to get block elements to sit side-by-side
- inline elements will be used only to style bits of text and images.
A Word About Divs and Spans
Concept
There are two elements we will frequently use that have no semantic meaning:
div
- ablock
element with no semantic meaningspan
- aninline
element with no semantic meaning
These are useful when you want to surround some content that you want to apply a style to, but you don’t want to adjust that content’s semantic meaning.
- So much for the much-touted separation of concerns!
Position
Positioning using the CSS position property
It is sometimes tempting to use the CSS position property to position elements on the page… it sounds like this is what the property is intended for.
The CSS position
property can take three values:
static
- keep the element at its default position on the pagerelative
- allows us to move the element from its static position by a particular number of pixels in thetop
orleft
directionsabsolute
- allows us to absolutely position an element relative to the top-left corner of the pagefixed
- allows us to have an element stuck in the browser viewport at a specific position relative to the top-left corner of the viewport.
Do not use these for general web page layout.
These position
values are useful in some specific advanced contexts, but should never be used to layout the positions of all the main parts of a web page.
- They make a site difficult to design and maintain.
You have now been warned. Don’t use them.
CSS Grid
The skeleton of web layout
The CSS Grid system is typically used to create the main skeletal structure of web pages. This involves the following:
- put a container element around the content you want to lay out
- set the container element to have a
display
value ofgrid
- set the container element to have a
grid-template-columns
value that defines the number of columns in the grid - set the container element to have a
grid-template-areas
value that defines the areas of the grid and their relative widths
Example - Cheerio Layout
Imagine the following classic web page layout with a header, footer, main content, left navigation bar and “right-rail” sidebar:
Example - Cheerio Layout (HTML)
The body
section of the HTML for such a page might contain code like this:
<div class="container">
<header>Header</header>
<nav>Left nav</nav>
<main>Main content</main>
<aside>Right rail</aside>
<footer>Footer</footer>
</div>
Example - Cheerio Layout (CSS)
The critical parts of the CSS code that would produce such a layout would be:
/* give grid system nicknames for each of the areas we want to lay out */
header { grid-area: header; }
nav { grid-area: left-nav; }
main { grid-area: main-content; }
aside { grid-area: right-rail; }
footer { grid-area: footer; }
.container {
display: grid; /* turn on the grid system */
grid-template-columns: 1fr 2fr 1fr; /* relative widths of columns */
/* now specify which nicknamed elements to put into which grid positions */
grid-template-areas:
"header header header"
"left-nav main-content right-rail"
"footer footer footer";
}
CSS Flexbox
Flexible layouts of components
While CSS Grid is most commonly used to create the outer structures of a web page, CSS Flexbox is often useful for laying out the inner content pieces that go within each skeletal area.
To use CSS Flex, we typically:
- put a container element around the content you want to lay out
- set the container element to have a
display
value offlex
to turn on CSS Flex for that element. - set the container’s
flex-direction
property to be eitherrow
orcolumn
, depending on whether you want to line up the inner elements horizontally or vertically. - set the container’s
gap
property to be the desired spacing between the inner elements. - determine how the inner elements are justified within the container by setting the container’s
justify-content
property.
Example - Content row
For example, consider an area containing three elements that sit side-by-side, where one element is wider than the others, and all elements are separated from each other by a gap.
- We show boxes for simplicity only, but these could be any type of real content elements:
h1
,p
,img
,ul
,div
, etc.
Example - Content row (HTML)
The HTML code for such a content area might be:
<div class="row">
<div class="box1">box 1</div>
<div class="box2">box 2</div>
<div class="box3">box 3</div>
</div>
- Note the inner elements are shown as
div
for simplicity. These could be any type of element in practice.
Example - Content row (CSS)
The critical parts of the CSS code that would produce such a layout would be:
.row {
display: flex;
flex-direction: row; /* options are row | row-reverse | column | column-reverse; */
gap: 1rem; /* gap between items, set to 1 times the font size of the root element (the html element), which by default is 16px in most browsers */
}
/* the following settings allow the three boxes to grow, where box 2 will grow up to 10 times as wide as box 1 and 2 */
.box1, .box3 { flex-grow: 1; }
.box2 { flex-grow: 10; }
Conclusions
At this point you have a solid understanding of the key concepts behind web page layout. You must try it yourself to really understand how to apply it.
- Thank you. Bye.