Unlimited Wordpress themes, plugins, graphics & courses! Unlimited asset downloads! From \$16.50/m

# Quick Tip: Utilizing Normal Document Flow

Length:MediumLanguages:

Layouts can be one of the trickiest things to master when it comes to CSS, though they're undeniably one of the most important. In this Quick Tip we're going to look at Normal Document Flow and how it can assist you with your layouts.

## Introduction

Before I explain what Normal Document Flow means I must explain the different types of html elements, and how they are displayed by default.

We're going to concentrate on the two main types of HTML elements, namely "block-level" and "inline" elements. Since the advent of HTML5 we have more specific element categorization (phrasing, flow, and sectioning) though browsers still effectively lean on the block and inline definitions.

Inline elements include items like anchor tags, images and spans. These elements often define text and data, such as turning the words Click Here into a link. By default inline elements display... well, inline. Meaning you can have multiple elements in one line, up until the width of the containing element is reached, at which point they continue onto the next line.

See this inline element demo in action.

Block-level elements however, such as divs and paragraphs, plus newer HTML5 members like article and section, have very different properties by default. Block-Level elements will normally, but not always, contain inline elements or other block-level elements. Think of them as the "structural" element of the two.

Browsers will (by default) format block-level elements with a line break before and after. In other words, block-level elements will always start on a new line (stacking up like blocks) unless otherwise styled.

## Styling

In all cases we can alter how an element is displayed using the CSS display property:

These display characteristics also dictate what else we can style. For example, we're free to determine the width and height of a block-level element, but not so with inline elements. Padding and margins can be applied to inline elements, but they won't influence the dimensions of the containing element. Take a look at this example to see what I mean:

## The Others

In addition to the common block and inline display attributes, there are a few less often encountered examples. For example, what if you want to display an element inline, but style it like a block-level element? Enter the inline-block value. Inline-block elements preserve properties such as width, height, margin and padding as they're applied to block level elements, whilst maintaining the structural qualities of an inline element.

Inline-Block is a potential alternative to floating elements but certainly isn't without its issues in these circumstances.

Another display value, that isn't as well used is list-item, which does exactly what it says on the tin. Each element with the list-item value applied is displayed vertically, as you'd expect in a list, but they also have bullet points next to them. This is useful for listing data i.e. a list of names, and is logically how list items such as these:

are displayed by default.

Finally, the last display value worth mentioning is 'none'. Can you guess what it does? That's right, any element that has its display property set to "none" simply doesn't show up on the page; it's visually (along with all elements contained within it) removed from the document. The markup for that element is still sent to the browser, but it doesn't generate a box and is hidden from view - screenreaders won't pick it up either. It takes up no space whatsoever, so other elements can take its place.

## So What is Normal Document Flow?

It's how a page is presented when you do nothing to it with regard to structural styling.

With the above in mind, what exactly is Normal Document Flow? It's how a page is presented when you do nothing to it with regard to structural styling. Browsers display content in the order that it's encountered; top stuff first, proceeding content lower down.

When people first start web design, they're often eager to tackle all sorts of fancy tricks, without learning the basics and the fundamentals. If you can understand Normal Document Flow, your web designs will be all the better for it!

## Exercise: The Markup

Let's get started on making a simple photo gallery with very basic CSS. I quickly made an html file to get us started, after all we're mainly concentrating on styling. Copy the code below into your text editor, and I'll explain what's in there.

There's an empty css block at the top. This is where we'll begin styling. Below that is a conditional link to the HTML5 Shiv script which will help Internet Explorer recognize and properly display HTML5 elements. Below that, the obligatory body tag. Inside the body tag is the container for the entire page, and also one for the photo gallery. I've used placekitten to get placeholder images, and Fillerama for filler text. So that's about it.

OK, so now just to demonstrate how Normal Document Flow works take a look at the page you've just made. Just as you'd expect right? The images display in one line, until the width of the parent element, in this case the figure (set to 100% of the viewport, but probably with some margins by default) is filled. And although you can't see it yet, all block level elements, such as the div, the figure and the article, are on their own line.

This is Normal Document Flow in action. Now, just resize the browser for a second. Yep, it's fluid! Well, perhaps not completely fluid as we've come to understand it - the images retain their dimensions irrespective of the viewport. Still, your layouts will always be fluid by default.

## Exercise: The Basic CSS

Just to prove that I'm not pulling your leg about block-level elements, let's add some background colors to them. In your CSS code block at the top of the HTML file, add this:

Now, after adding some background colors, check out the result. It should be apparent as to how block-level elements are displayed.

## Exercise: The Layout CSS

The styling below only contains CSS properties from the box-model (width, margin, padding etc.), however it shows how very little CSS can create solid layouts.

Replace the CSS you already have, for this:

In this CSS we have designated the width of the container to 65%. We could also have used a fixed pixel value, but percentages retain the fluid nature of the document and demonstrate the document flow more effectively. We've also centered the page using margin: 0 auto.

The rest is all about aesthetics. I've added some padding to give everything some breathing space, specified the fonts and font colors, that's about it.

Save that document and view it in your browser.

Whenever I'm working on a project I always consider how it would look in Normal Document Flow. This allows me to think about how I can improve it with as little CSS as possible.

## Conclusion

Using the principles that we've learned today, try building your own examples. You'll find that (structurally) you can achieve quite a lot before getting into the technicalities of floats and positioning.