Margins, Padding and Borders

All three of these properties follow the box model as they affect space surrounding an element.

Margin Controls

The Box Model

Margins define the space surrounding an element. In the example above, the margin is the gray area. You can set each margin individually as shown in the example below:

css
  1. .myclass {
  2. margin-top: 20px;
  3. margin-right: 30px;
  4. margin-bottom: 20px;
  5. margin-left: 10px; }

You can also use the margin shorthand property. In the example below, a 20 pixel margin will be added to all four sides:

css
  1. .myclass {
  2. margin: 20px; }

If you use two values, the first value applies to the top and bottom and the second value applies to the right and left sides. An example would look like the following:

css
  1. .myclass {
  2. margin: 20px 10px; }

If you wish to use all four values, you can apply each value separately as follows:

css
  1. .myclass {
  2. margin: 20px 10px 20px 30px; }

Note that there are no commas, only a single space separates each value. You can also use percentages in place of pixels.

Border Controls

Border controls enable you to add a visible border around an area. You can specify the border's style, width, color, and you can set each individual side of an element.

Using the border shorthand properties, the attributes are listed as follows:

  • border-width
  • border-color
  • border-style

A shorthand property rule might look something like this, which applies a 1 pixel solid dark gray border:

css
  1. .myclass {
  2. border: 1px #666666 solid; }

Border-width: The border-width property accepts a length value such as 1px or a relative-size keyword such as thin, medium, or thick. A border-width of 0 will prevent a border from appearing.

Border-style: The border-style attribute refers to the type of border such as solid or dashed. note that not all values are recognized in all browsers. If a browser does not recognize a value, it will usually either be ignored or will display as a solid border. The different values are: none, dotted, dashed, solid, double, groove, ridge, inset, outset.

Border-color: The border-color property accepts a value in either hex or RGB. It will also accept a color name, but can have unexpected results. We prefer using a hex value.

Each border has four separate sides and you can define each side individually with the following properties:

  • border-top
  • border-right
  • border-bottom
  • border-left

It is possible to have a block with each side having a different width, color and style. (Although why would you ever want to?)

css
  1. .mycrazyblock {
  2. border-top: 5px blue dashed;
  3. border-right: 3px #606060 double;
  4. border-bottom: 1px #000000 solid;
  5. border-left: 2px #ff0000 dotted; }

Padding Controls

Padding sets the amount of space inside of a block element. In the box model image above, the padding is shown in blue. You can set each padding individually as shown in the example below:

css
  1. .myclass {
  2. padding-top: 5px;
  3. padding-right: 20px;
  4. padding-bottom: 10px;
  5. padding-left: 15px;
  6. }

Just as with margin properties, the padding properties can also use shorthand properties setting all sides the same, in pairs, or differently as in the examples below:

css
  1. .myclass {
  2. padding: 20px;
  3. }
css
  1. .myclass {
  2. padding: 20px 5%;
  3. }
css
  1. .myclass {
  2. padding: 5px 20px 10px 15px;
  3. }

Float

The float property is most commonly applied to images and divs and accepts the value of right, left, or none. If you have a need to display your images in various ways, you can define a class and apply the class to a single image. In this site, you will see our small icon images are floated to either the left of the right. Here are some examples:

This example has the image floated to the left without an image border. It sits to the left side of the containing block and the text will flow around the image. The coding is as follows:

css
  1. .img-left {
  2. margin: 0 10px 0 0;
  3. float: left;
  4. }

This is the same image, but it is floated to the right. Again the image will sit in its position and the text will flow around it. The coding is as follows:

css
  1. .img-right {
  2. margin: 0 0 0 10px;
  3. float: right;
  4. }