Web Design Articles

Inline elements and padding


Russ Weakley
19-Nov-06

Have you ever tried to add padding to <li> elements that have been set to "display: inline"? Did you find that the padding seemed to be rendering in an unusual way?

In the example below, the <li> elements have been set to "display: inline", and then 1em of padding has applied to all sides. Unfortunately, the padding on the top and bottom of the <li> elements seems to have been ignored causing the elements to overlap each other.

List item one List item two List item three List item four List item five List item six List item seven List item eight List item nine List item ten List item eleven List item twelve
To understand why this is happening, we need to look at the different ways that block level and inline elements treat properties such as width, height, padding and margins.

Block level elements

The W3C's CSS2 spec defines block level elements as elements of the source document that are formatted visually as blocks.

In other words, block level elements are normally displayed as blocks with line breaks before and afterwards. Examples of block level elements include paragraphs, headings, divs, and blockquotes.

Inline elements

The W3C's CSS2 spec defines inline elements as elements of the source document that do not form new blocks of content; the content is distributed in lines.

So, inline content is displayed with no line breaks before and afterwards. Examples of inline elements include emphasised text, strong text and links.

Dimension - a key difference between block and inline elements
If you try to add dimension to an inline element, some properties will be applied, some properties will be partially applied and others will not be applied at all. The most noticable properties are width, height, margin and padding.

Inline elements and width

The W3C's CSS2 spec states that for Inline, non-replaced elements, the 'width' property does not apply.

In the example below, a width of 200px has been applied to the inline <a> element. As you can see, it has no affect on the surrounding content:

Lorem ipsum dolor sit amet consect etuer adipi scing elit sed diam nonummy nibh euismod tinunt ut laoreet dolore magna aliquam erat volut. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Inline elements and height

The W3C's CSS2 spec states that for Inline, non-replaced elements, the 'height' property doesn't apply, but the height of the box is given by the 'line-height' property.

In the example below, a height of 50px has been applied to the inline <a> element. As you can see, it has no affect on the surrounding content:

Lorem ipsum dolor sit amet consect etuer adipi scing elit sed diam nonummy nibh euismod tinunt ut laoreet dolore magna aliquam erat volut. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Inline elements and padding

While padding can be applied to all sides of an inline element, only left and right padding will have an effect on surrounding content.

In the example below, 50px of padding has been applied to all sides of the <a> element. As you can see, it has an affect on the content on each side, but not on content above or below:

Lorem ipsum dolor sit amet consect etuer adipi scing elit sed diam nonummy nibh euismod tinunt ut laoreet dolore magna aliquam erat volut. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Inline elements and margins

Margins operate in the same way as padding on inline elements. In the example below, 50px of margin has been applied to all sides of the <a> element. While the left and right edges are effected, the content above and below are not:

Lorem ipsum dolor sit amet consect etuer adipi scing elit sed diam nonummy nibh euismod tinunt ut laoreet dolore magna aliquam erat volut. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Changing an element's "display" property from inline to block
It is possible to change the display property of an inline element to "block". This will give it a block level appearance without changing it's actual structure.

For example, the <a> element below has been set to "display: block". As soon as this occurs, properties like width, height, margin and padding are applied is if it were a block level element.

Lorem ipsum dolor sit amet consect etuer adipi scing elit sed diam nonummy nibh euismod tinunt ut laoreet dolore magna aliquam erat volut. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.

Changing an element's "display" property from block level to inline
You can also change block level elements so that they display inline. If an <li> element is set to "display: inline", width, height, padding and margin will immediately operate as they do for any other inline element.

This is what caused the <li> element to ignore top and bottom padding in our original example.

Overcoming padding issues for <li> elements set to "inline"
There are many ways to overcome the padding issue shown above. Here is one:

Step 1

Set the <li> element to "display: block". Padding is now applied to all sides of the element, but the elements sit in a vertical stack.

List item one
List item two
List item three
List item four
List item five
List item six
List item seven
List item eight
List item nine
List item ten
List item eleven
List item twelve

Step 2

Set the <li> element to "float: left" and give it a width - in this case 8em has been used. This will allow the list items to sit beside each other. If there is not enough room for all list items to sit next to each other, those that do not fit will move down and sit below.

List item one List item two List item three List item four List item five List item six List item seven List item eight List item nine List item ten List item eleven List item twelve










why-css-is-good-for-google
using-relatve-font-sizes
random-content-rotation
web-design-xhtml-1-1
web-design-resources
accessibility-intro
web-design-xhtml-2-1
why-internet-marketing
xhtml-latin-1-character-references
google-updates
google-dance
css-positioning-properties
fancy-paragraphs
bob-regan-macromedia-accessibility
web-design-technologies
wrox-beginning-php-4-chapter-3-1
julie-howell-rnib-accessibility
handy-hints-web-design
mod_accessibility
pagerank-1
search-engine-crawling
Definition lists - misused or misunderstood.html
Accessible Data Tables
Developing sites for users with Cognitive disabilities and learning difficulties
An Accessibility Frontier Cognitive disabilities and learning difficulties
Inline elements and padding
Basic webstandards Workshop
Internet Explorer and column collapse
Building a page template in CSS - a step by step tutorial
Remote control CSS
Colored boxes - one method of building full CSS layouts
Replicating a Tree table
Creating a graph using percentage background images
Simple, accessible external links
Simple, accessible more links
Styling abbreviations and acronyms
 Web standards checklist
Floated items inside containers
Liquid layouts the easy way
Two columns with color
CSS Centering - fun for all!
Body padding and margin
List inheritance and Descendant Selectors
Taming the Taming lists model
Headings as images - The Lindsay method
Ideal line length for content
Validating Australian Museum Online
Styling the hr element
Styling and font family names that contain whitespace
Sample CSS Page Layouts
how_to_find_good_freelancer
using_colors_on_website
annoying-website-design
 
cross_browser_compatibility
banner-design-success-techniques
 
craig-tanner-freelance
ecommerce-website-design
 
ppc
separating-content-from-presentation
 
alternative
web_page_optimization
 
personalization
communicating_needs_web_designer
 
buzz
design_it_yourself_or_hire_pro
 
social_bookmarking2
w3c_validation.php
social_bookmarking
web_design_guidelines
 
digg
photo_optimization
 
buzz2
website_templates
 
viral_video
web_design_versus_web_development
 
controversy
graphic_formats
 
 
good_website_navigation_is_important