Skip to content

Technique C18:Using CSS margin and padding rules instead of spacer images for layout design

About this Technique

This technique is Advisory for 1.1.1: Non-text Content.



This technique relates to 1.1.1: Non-text Content (Advisory).


Web designers sometimes use spacer images (usually 1x1 pixel, transparent GIFs) for better control over layout, for example in tables or to indent a paragraph. However, Cascading Style Sheets (CSS) allow sufficient control over layout to replace spacer images. The CSS properties for margins and padding can be used on their own or in combination to control the layout. The margin properties ('margin-top', 'margin-right', 'margin-bottom', 'margin-left', and the shorthand 'margin') can be used on any element that is displayed as a block; they add space at the outside of an element. The padding properties ('padding-top', 'padding-right', 'padding-bottom', 'padding-left', and the shorthand 'padding') can be used on any element; they add space inside the element.


Example 1: The CSS

The following example consists of two parts: the CSS code, which specifies a margin on all sides of the table, and padding for the table cells; and the HTML code for the table, which does not contain spacer images and is not nested inside another table.

table { margin: .5em; border-collapse: collapse; } 
td, th { padding: .4em; border: 1px solid #000; }
  <caption>Books in the category 'Web development'</caption>
      <td>How to Think Straight About Web Standards</td>
      <td>Andrew Stanovich</td>
      <td>1 April 2007</td>

Other sources

No endorsement implied.

Back to Top