Shorthand properties

  • Revision slug: CSS/Shorthand_properties
  • Revision title: Shorthand properties
  • Revision id: 48875
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment 44 words added, 18 words removed

Revision Content

Definition

Shorthand properties are CSS properties that allows the settings the value of several other CSS properties. Using a shorthand property, a Web developer can write more concise and often more readable style sheets and so save time and energy.

The CSS specification defines shorthand properties to group the definition of common properties acting on the same theme. E. g. the CSS {{ cssxref("background") }} property is a shorthand property than is able to define the value of {{ cssxref("background-color") }}, {{ cssxref("background-image") }}, {{ cssxref("background-repeat") }}, and {{ cssxref("background-position") }}. Similarly, the most common font-related properties can be defined using the shorthand {{ cssxref("font") }} or the different margins around a box can be defined using the {{ cssxref("margin") }} shorthand.

Tricky edge cases

Even if they are very convenient to use, there are a few edge cases to keep in mind when using them:

  1. A value which is not specified is set to its initial value. That's sound anecdotal, but it really means that it overrides previously set values. Therefore:
    background-color: red;
    background: url(images/bg.gif) no-repeat top right;
    
    will not set the color of the background to red but to {{ cssxref("background-color") }}'s default, transparent, as the second rule has precedence.
  2. Only the individual properties values can inherit. As missing values are replaced by their initial value, it is impossible to allow inheritance of individual properties by omitting them. The keyword inherit can be apply to a property, but only as a whole, not as a keyword for one value or another. That means that the only way to make some specific value to be inherited is to use the longhand property with the keyword inherit.
  3. Shorthand properties try not to force a specific order for the values of the properties they replace. If this work well when these properties uses values of different types, as the order has no importance, this do not work as easily when several properties can have identical values. Handling of these cases are grouped in several categories:
    1. Shorthands handling properties related to edges of a box, like {{ cssxref("border-style") }}, {{ cssxref("margin") }} or {{ cssxref("padding") }}, always use a consistent 1-to-4-value syntax representing those edges:
      border1.png The 1-value syntax: border-width: 1em — The unique value represents all edges
      border2.png The 2-value syntax: border-width: 1em 2em — The first value represents the vertical, that is top and bottom, edges, the second the horizontal ones, that is the left and right ones.
      border3.png The 3-value syntax: border-width: 1em 2em 3em — The first value represents the top edge, the second, the horizontal, that is left and right, ones and the third value the bottom edge
      border4.png

      The 4-value syntax: border-width: 1em 2em 3em 4em — The four values represents the top, right, bottom and left edge respectively, always in that order, that is clock-wise starting at the top (The initial letter of Top-Right-Bottom-Left matches the order of the consonant of the word trouble: TRBL)

    2. SImilarly, shorthands handling properties related to corners of a box, like {{ cssxref("border-radius") }}, always use a consistent 1-to-4-value syntax representing those corners:

Background Properties

A background with the following properties:

background-color: #000;
background-image: url(images/bg.gif);
background-repeat: no-repeat;
background-position: top right;

Can be shortened to just one declaration:

background: #000 url(images/bg.gif) no-repeat top right;

(The shorthand form is actually the equivalent of the longhand properties above plus background-attachment: scroll and, in CSS3, some additional properties.)

Font Properties

The following declarations:

font-style: italic;
font-weight: bold;
font-size: .8em;
line-height: 1.2;
font-family: Arial, sans-serif;

can be shortened to the following:

font: italic bold .8em/1.2 Arial, sans-serif;

(This shorthand declaration is actually equivalent to the longhand declarations above plus font-variant: normal and font-size-adjust: none (CSS2.0 / css3), font-stretch: normal (css3).)

Border Properties

With borders, the width, color and style can be simplified into one declaration. For example,

border-width: 1px;
border-style: solid;
border-color: #000;

Can be written as

border: 1px solid #000;

Margin/Padding Properties

Shorthand versions of margin and padding values work the same way. The following CSS declarations:

margin-top: 10px;
margin-right: 5px;
margin-bottom: 10px;
margin-left: 5px;

are the same as the following declaration (note that the values are in clockwise order from top: top, right, bottom, then left (TRBL, the consonants in "trouble"))

margin: 10px 5px 10px 5px;

{{ languages( { "pl": "pl/Skr\u00f3cone_deklaracje_CSS" } ) }}

Revision Source

<h2>Definition</h2>
<p><dfn>Shorthand properties</dfn> are CSS properties that allows the settings the value of several other CSS properties. Using a shorthand property, a Web developer can write more concise and often more readable style sheets and so save time and energy.</p>
<p>The CSS specification defines shorthand properties to group the definition of common properties acting on the same theme. E. g. the CSS {{ cssxref("background") }} property is a shorthand property than is able to define the value of {{ cssxref("background-color") }}, {{ cssxref("background-image") }}, {{ cssxref("background-repeat") }}, and {{ cssxref("background-position") }}. Similarly, the most common font-related properties can be defined using the shorthand {{ cssxref("font") }} or the different margins around a box can be defined using the {{ cssxref("margin") }} shorthand.</p>
<h2>Tricky edge cases</h2>
<p>Even if they are very convenient to use, there are a few edge cases to keep in mind when using them:</p>
<ol> <li>A value which is not specified is set to its initial value. That's sound anecdotal, but it really means that it <em>overrides</em> previously set values. Therefore: <pre>background-color: red;
background: url(images/bg.gif) no-repeat top right;
</pre> will not set the color of the background to <code>red</code> but to {{ cssxref("background-color") }}'s default, <code>transparent</code>, as the second rule has precedence.</li> <li>Only the individual properties values can inherit. As missing values are replaced by their initial value, it is impossible to allow inheritance of individual properties by omitting them. The keyword <code>inherit</code> can be apply to a property, but only as a whole, not as a keyword for one value or another. That means that the only way to make some specific value to be inherited is to use the longhand property with the keyword <code>inherit.</code></li> <li>Shorthand properties try not to force a specific order for the values of the properties they replace. If this work well when these properties uses values of different types, as the order has no importance, this do not work as easily when several properties can have identical values. Handling of these cases are grouped in several categories: <ol> <li>Shorthands handling properties related to edges of a box, like {{ cssxref("border-style") }}, {{ cssxref("margin") }} or {{ cssxref("padding") }}, always use a consistent 1-to-4-value syntax representing those edges: <table style="standard-table"> <tbody> <tr> <td><img alt="border1.png" class="internal default" src="/@api/deki/files/6145/=border1.png"></td> <td><em>The 1-value syntax</em>: <code>border-width: 1em</code> — The unique value represents all edges</td> </tr> <tr> <td><img alt="border2.png" class="internal default" src="/@api/deki/files/6146/=border2.png"></td> <td><em>The 2-value syntax</em>: <code>border-width: 1em 2em</code> — The first value represents the vertical, that is top and bottom, edges, the second the horizontal ones, that is the left and right ones.</td> </tr> <tr> <td><img alt="border3.png" class="internal default" src="/@api/deki/files/6147/=border3.png"></td> <td><em>The 3-value syntax</em>: <code>border-width: 1em 2em 3em</code> — The first value represents the top edge, the second, the horizontal, that is left and right, ones and the third value the bottom edge</td> </tr> <tr> <td><img alt="border4.png" class="internal default" src="/@api/deki/files/6148/=border4.png"></td> <td> <p><em>The 4-value syntax</em>: <code>border-width: 1em 2em 3em 4em</code> — The four values represents the top, right, bottom and left edge respectively, always in that order, that is clock-wise starting at the top (The initial letter of Top-Right-Bottom-Left matches the order of the consonant of the word <em>trouble</em>: TRBL)</p> </td> </tr> </tbody> </table> </li> <li>SImilarly, shorthands handling properties related to corners of a box, like {{ cssxref("border-radius") }}, always use a consistent 1-to-4-value syntax representing those corners:</li> </ol> </li>
</ol>
<h2 name="Background_Properties">Background Properties</h2>
<p>A background with the following properties:</p>
<pre>background-color: #000;
background-image: url(images/bg.gif);
background-repeat: no-repeat;
background-position: top right;</pre>
<p>Can be shortened to just one declaration:</p>
<pre>background: #000 url(images/bg.gif) no-repeat top right;</pre>
<p>(The shorthand form is actually the equivalent of the longhand properties above plus <code>background-attachment: scroll</code> and, in CSS3, some additional properties.)</p>
<h2 name="Font_Properties">Font Properties</h2>
<p>The following declarations:</p>
<pre>font-style: italic;
font-weight: bold;
font-size: .8em;
line-height: 1.2;
font-family: Arial, sans-serif;</pre>
<p>can be shortened to the following:</p>
<pre>font: italic bold .8em/1.2 Arial, sans-serif;</pre>
<p>(This shorthand declaration is actually equivalent to the longhand declarations above plus <code>font-variant: normal</code> and <code>font-size-adjust: none</code> (CSS2.0 / css3), <code>font-stretch: normal</code> (css3).)</p>
<h2 name="Border_Properties">Border Properties</h2>
<p>With borders, the width, color and style can be simplified into one declaration. For example,</p>
<pre>border-width: 1px;
border-style: solid;
border-color: #000;</pre>
<p>Can be written as</p>
<pre>border: 1px solid #000;</pre>
<h2 name="Margin.2FPadding_Properties">Margin/Padding Properties</h2>
<p>Shorthand versions of margin and padding values work the same way. The following CSS declarations:</p>
<pre>margin-top: 10px;
margin-right: 5px;
margin-bottom: 10px;
margin-left: 5px;</pre>
<p>are the same as the following declaration (note that the values are in clockwise order from top: top, right, bottom, then left (TRBL, the consonants in "trouble"))</p>
<pre>margin: 10px 5px 10px 5px;</pre>
<p>{{ languages( { "pl": "pl/Skr\u00f3cone_deklaracje_CSS" } ) }}</p>
Revert to this revision