Block vs Inline display style in CSS
HTML elements can be displayed either in block or inline style.
The difference between these is one of the most basic things you need to know in order to use CSS effectively.
New 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.
New 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.
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>
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
<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>
Here's how it looks as a normal list
Just adding the class "toolbar"...
background-color:#eee;
border:1px solid;
border-color:#f3f3f3 #bbb #bbb #f3f3f3;
margin:0;
padding:.5em;
<ul class="toolbar">
<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>
Here's how it looks with the CSS styles applied
See our production worked example ebook for more in-depth CSS tricks!
New 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.
Scratchmedia specialises in designing new Web2.0 user interfaces. We also provide expert reviews and consulting services.
Questions about Web Design & Production?
Share your questions, thoughts, and ideas with the community over at Scratch Web Design Forums »- Recommended
- NEW Ebook "Save the Pixel - the Art of Simple Web Design"
- vBulletin Skins
- Compare web hosts - speed, uptime, support etc...
- Professional Web Hosting $4.95
- Quality Ruby on Rails hosting - UK, recommended
- Custom logo design services
- Promote your business online for FREE
- Advertise on this site
- Chat about web design our brand new discussion forum!
If you like Web Design from Scratch, you'll love our new ebook.
