CSS Inline Elements
Inline elements are distinct from block level elements. Whereas block-level elements modify the flow of any text around them, inline elements are designed to flow with the text. Also, inline elements do not have a line break before and after. Properties of inline elements:
- they flow with text.
- they have line-height.
- the element height and width is dependent on the content and will not collapse to the borders of the parent element.
So what does all this mean?! It can be displayed in the following way:
Green Line - the content area
Red Line - the border. The distance between border and content area is the padding
Blue Line - the element edge. The distance between border and element edge is the margin
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.
The 3 ways that HTML elements can be displayed
All HTML elements are naturally displayed in one of the following ways:
Takes up the full width available, with a new line before and after (display:block;)
Takes up only as much width as it needs, and does not force new lines (display:inline;)
Some tags are not visible (display:none;)
<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:
Your general-purpose box
<h1> ... <h6>
<ul>, <ol>, <dl>
Lists (unordered, ordered and definition)
<li>, <dt>, <dd>
List items, definition list terms, and definition list definitions
Like an indented paragraph, meant for quoting passages of text
Indicates a block of preformatted code
An input form
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:
Your all-purpose inline element
Anchor, used for links (and also to mark specific targets on a page for direct linking)
Used to make your content strong, displayed as bold in most browsers, replaces the narrower (bold) tag
Adds emphasis, but less strong than . Usually displayed as italic text, and replaces the old (italic) tag
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.
Form input fields like and
Indicates an abbr. (hover to see how it works)
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.
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
<li><a href="#">About us</a></li>
<li><a href="#">Contact us</a></li>
Here's how it looks as a normal list
Just adding the class "toolbar"...