The 3 ways that HTML elements can be displayed

All HTML elements are naturally displayed in one of the following ways:

Block
Takes up the full width available, with a new line before and after (display:block;)
Inline
Takes up only as much width as it needs, and does not force new lines (display:inline;)
Not displayed
Some tags, like <meta /> and <style> are not visible (display:none;)

Block example

<p> tags and <div> tags are naturally displayed block-style.

(I say "naturally" because you can override the display style by setting the CSS display property e.g. display:inline;.)

A block-display element will span the full width of the space available to it, and so will start on a new line in the flow of HTML. The flow will continue on a new line after the block-display element.

Here I’ve started a paragraph and now I’m going to insert a <div>

new div inside my paragraph

and then continue the text here…

 

See how the <div> jumped in and took over the full width of the space?

Common HTML elements that are naturally block-display include:

<div>
Your general-purpose box
<h1> … <h6>
All headings
<p>
Paragraph
<ul>, <ol>, <dl>
Lists (unordered, ordered and definition)
<li>, <dt>, <dd>
List items, definition list terms, and definition list definitions
<table>
Tables
<blockquote>
Like an indented paragraph, meant for quoting passages of text
<pre>
Indicates a block of preformatted code
<form>
An input form

Inline example

Inline-display elements don’t break the flow. They just fit in with the flow of the document.

So here I’ve got a paragraph going on, and I’m going to add a <span> tag that has a yellow background and italic text. See how it just fits right in with the flow of the text?

More elements are naturally inline-style, including:

<span>
Your all-purpose inline element
<a>
Anchor, used for links (and also to mark specific targets on a page for direct linking)
<strong>
Used to make your content strong, displayed as bold in most browsers, replaces the narrower <b> (bold) tag
<em>
Adds emphasis, but less strong than <strong>. Usually displayed as italic text, and replaces the old <i> (italic) tag
<img />
Image
<br>
The line-break is an odd case, as it’s an inline element that forces a new line. However, as the text carries on on the next line, it’s not a block-level element.
<input>
Form input fields like
and
<abbr>
Indicates an abbr. (hover to see how it works)
<acronym>
Working much like the abbreviation, but used for things like this TLA.

You change the display property of any elements

Although each HTML element has its natural display style, you can over-ride these in CSS.

This can be very useful when you want your page to look a particular way while using semantically-correct HTML.

Examples

Say you want to provide a list of items, but you don’t want a big bulleted list. You just want to say that you’re going to the store to buy:

  • some fizzy drinks,
  • a chainsaw,
  • and some nylon stockings.

 

Or maybe you want a nice toolbar, which is stricly a list (of links) and so should be marked up as a <ul>.

Here’s the code

 
<ul>

<li><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">FAQs</a></li>
<li><a href="#">Contact us</a></li>

</ul>

 

Here’s how it looks as a normal list

Just adding the class "toolbar"…

 
<style type="text/css">

.toolbar li {

display:inline;
background-color:#eee;
border:1px solid;
border-color:#f3f3f3 #bbb #bbb #f3f3f3;
margin:0;
padding:.5em;
zoom: 1;

}

</style>

<ul class="toolbar">

<li><a href="#">Home</a></li>
<li><a href="#">About us</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">FAQs</a></li>
<li><a href="#">Contact us</a></li>

</ul>

 

Here’s how it looks with the CSS styles applied

See our production worked example ebook for more in-depth CSS tricks!

My Tips for Learning Better CSS

If you want to build clean and robust web pages using HTML & CSS, I strongly recommend Build Your Own Web Site The Right Way Using HTML & CSS. It's great value at under $20.

For a more comprehensive CSS manual, you can't beat Eric Meyer's Definitive Guide.

If you're wanting a guide to using CSS to help you create more eye-catching and beautiful designs, this may be the one for you: Zen of CSS Design

New eBookNew eBook - Web page production with xHTML and CSS #1

Experience the thought process of a professional web producer as he guides you through the web page production process, from photoshop design to working HTML template.

The book tells you how to approach web production, beginning with semantic HTML, guiding you through how to slice up a photoshop document, and finally how to use CSS for presentation.

Buy Now for £9.00 GBP

New eBookNew eBook - Guide to Semantic HTML

Ben Hunt adds another great eBook to the collection with his "Guide to Semantic HTML". The book gives you advice and tips on how, and why to use semantic HTML.

Included is a comprehensive list of HTML tags, each with their semantically appropriate uses, along with a worked example taking you through the process of how to build a website using semantic HTML.

Get “Guide to Semantic HTML” now for £5.00

Tags:

Make Better Web Pages!

How to make your web site sell - use my secrets

Find out How

Do you love our approach to crafting simple & effective web sites that just work for people?

We'd love to hear about your web strategy.

Contact one of our team today!

6 Comments

  1. Renyo Borges says:

    actually there are more ways to display some elements, you can checkout on w3c docs…

  2. Jason says:

    I like this quick listing of block and inline elements.

    I think you should change round the and example so the example is actual valid code.

    We don’t want more people creating invalid code. Theres plenty of it out there already and the next generation should be taught correctly form the outset.

    Sorry for being a stickler but it’s important yes.

  3. Jason says:

    *edit
    “I think you should change round the <div> and <p> example so it is actual valid code.

  4. Sam Thunderbolt says:

    Thanks!! That helped a lot! Very clearly explained and exactlty what I wanted to know :) .

  5. I like this quick listing of block and inline elements.

    I think you should change round the and example so the example is actual valid code.

    Thank you..

  6. [...] idea until today (thank you Alvaro!). If you’d like to learn more about the display element, go here. There’s a great description and several examples to help [...]

Leave a comment

Articles + tutorials in HTML / CSS

Complete List of HTML/xHTML Tags, With Guide to Proper Semantic Use
My Complete List of HTML/xHTML Tags, With Guide to their Proper Semantic Use
Semantic HTML Handbook – Benefits of Writing Semantic HTML
Free article on Semantic HTML. Why you should learn Semantic HTML. Benefits for SEO and code reuse explained.
A Few Tips and Tricks to Write Better Semantic HTML
Tips and tricks for writing better semantic HTML or xHTML from a professional web producer
Building a web page with HTML & CSS for complete beginners
Learn what HTML is and how to build a website from scratch. A guide to creating a web page using HTML and CSS for people with no prior knowledge
Keeping your content in order of priority with flexible CSS layouts.
How to keep your most important content at the top of your page, no matter what kind of column layout you wish to achieve.
Web page production with xHTML and CSS (ebook)
Introduction to Semantic HTML
A Basic Guide to writing semantically correct semantic HTML or xHTML markup
Anatomy of HTML/xHTML tags
HTML basics tutorial: Learn HTML tags, covering the most common attributes
Introduction to Cascading Style Sheets (CSS)
Beginner's introduction to Cascading Style Sheets (CSS), learn CSS.
Making a Tabular list in HTML
Create an appealing tabular list using HTML, CSS and JavaScript
How HTML, CSS and JavaScript work together in web pages
Best practice for using HTML, Cascading Style Sheets, and JavaScript together to make web pages.
HTML Tables – when and how to use tables in HTML
When to use tables in HTML, and how to do it properly
Block vs Inline display style in CSS
An article explaining the differences between block and inline display property in CSS. Gives examples of CSS Block vs Inline CSS and how they are applied in CSS/HTML files and pages.
Datasheet-style form using HTML and CSS
Make a datasheet-style web form using HTML, CSS and JavaScript
Inheritance and Cascading Styles in CSS Explained
Introduction to CSS inheritance and how styles apply in CSS through inheritance and cascading. Read this guide detailed guide on using CSS inheritance.
HTML Lists: unordered, ordered and definition lists
My guide to HTML lists explains the 3 main types of lists web browsers support and how to implement them in HTML / xHTML
HTML, how to code HTML/xHTML markup
Free HTML tutorials: Learn top-quality web page production skills using HTML/xHTML markup
Using CSS (Cascading Style Sheets)
Free tutorial on learning CSS for web design and development
Introduction to HTML – basics of HTML
Introduction to basic HTML tags and the structure of HTML documents.
© Scratchmedia Limited, 2006-2010
Floor 3, 111 Buckingham Palace Road, London, SW1W 0WQ, UK
+44 (0)207 1600 989