Box Model Shorthand

1, 2, 3, Or 4 Sizes For Margins and Padding (...and Borders)

This also applies to the border property but only when being applied on a per property basis (this is covered later in this lesson)

Padding and Margin Shorthand

Example of using margin shorthand:

   .item {
      margin-top: 15px;
      margin-bottom: 20px;
      margin-left:  5%;
      margin-right:  5%;
    }
   .item {
      margin: 15px 5% 20px;
    }

Centering using margins

The margin value auto is often used to center content horizontally. To accomplish it, the auto value is used as both the left and right side margin value. The section of this web page that you are looking at right now with white background is centered in the same manner as the following example. The css code below is what is being used to center the div:


     div {
     margin: 0 auto;
     }

This div is centered horizontally using auto for its left and right side margin values.



Border Shorthand

Applying Border Properties On A Per Side Basis

Border property values

All three border properties are listed below. They can be specified per side using shorthand. The style property is required otherwise the border will not appear. The values must be in the following order, even if either the width or color are excluded:

  1. Width
    • Percentage ( % ) will not work. If used the border will not appear.
    • The default value is medium.
  2. Style
    • Required, otherwise the border will not be visible - there is no default.
  3. Color
    • If color is not specified, the color property (text color) will be used for the border.

Applying up to three border properties to all sides, or to individual sides, can be done using the three values listed above and the shorthand properties below. (The links go to information about these properties at the W3C).

       border: thin solid #053;  
       border-top: 3px double #030;  
       border: 2px inset #030;
       border-bottom-color: #A5D790;

Applying Border Properties On a Per Property Basis

border-width

border-style

border-color