While creating an HTML page, it is important to understand the difference between the 2 elements categories inline and block to avoid coding errors and impost the CSS for the best.
Those 2 words, ‘block’ and ‘inline’ are explicit enough by themselves..
- block: an element which forms a separate block
- inline: an element which stays inline with the rest of the content
Block element
A block element has the following characteristics:- Forms a block and positions itself vertically underneath with a new empty line above and under
- Takes all the available width based on the measure of its parent element (container)
- Its height changes in rapport to its content
- Can contains other inline and/or block elements (es: a <p> inside a <div>)
- Via css, we can impost a fixed width and/or height
- Example of natural html block element: <div>, <h1>…<h6>, <p>, <ul>, <ol>, <dl>, <table>, <blockquote>, <form>
Inline elements
An inline element has the following characteristics:- Positions itself horizontally inline with the rest of the content of its parent element (container)
- Takes the minimum width and height in rapport to its content
- Can contains ONLY other inline elements (es: an <img/> inside a <a>)
- Via css, we can NOT impost fixed measure
- Example of natural html inline elements: <span>, <a>, <strong>, <em>, <img />, <abbr>, <acronym>
Using the css display:block; and display:inline;, we can change the property of the element from block to inline or from inline to block.
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>
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:
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:
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.
Here’s the code
Here’s how it looks as a normal list
Just adding the class “toolbar”…
Here’s how it looks with the CSS styles applied