Movatterモバイル変換


[0]ホーム

URL:


  1. Веб-технологии для разработчиков
  2. CSS: каскадные таблицы стилей
  3. Guides
  4. Cascading and inheritance
  5. Краткая форма записи свойств

This page was translated from English by the community.Learn more and join the MDN Web Docs community.

View in EnglishAlways switch to English

Краткая форма записи свойств

Сокращённые свойства - это такие CSS-свойства, которые позволяют одновременно устанавливать значения нескольких других свойств. Используя сокращённое свойство, вы можете писать более сжатые (и часто более читаемые) таблицы стилей, экономя время и энергию.

The CSS specification defines shorthand properties to group the definition of common properties acting on the same theme. For instance, the CSSbackground property is a shorthand property that's able to define the values ofbackground-color,background-image,background-repeat, andbackground-position. Similarly, the most common font-related properties can be defined using the shorthandfont, and the different margins around a box can be defined using themargin 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 sounds anecdotal, but it really means that itoverrides previously set values. Therefore:

    css
    background-color: red;background: url(images/bg.gif) no-repeat left top;

    will not set the color of the background tored but tobackground-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 keywordinherit can be applied 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 keywordinherit.

  3. Shorthand properties try not to force a specific order for the values of the properties they replace. This works well when these properties use values of different types, as the order has no importance, but this does 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, likeborder-style,margin orpadding, always use a consistent 1-to-4-value syntax representing those edges:

      border1.pngThe 1-value syntax:border-width: 1em — The unique value represents all edges
      border2.pngThe 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.pngThe 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.pngThe 4-value syntax:border-width: 1em 2em 3em 4em — The four values represent the top, right, bottom and left edges 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 wordtrouble: TRBL)
    2. Similarly, shorthands handling properties related to corners of a box, likeborder-radius, always use a consistent 1-to-4-value syntax representing those corners:

      corner1.pngThe 1-value syntax:border-radius: 1em — The unique value represents all corners
      corner2.pngThe 2-value syntax:border-radius: 1em 2em — The first value represents the top left and bottom right corner, the second the top right and bottom left ones.
      corner3.pngThe 3-value syntax:border-radius: 1em 2em 3em — The first value represents the top left corner, the second the top right and bottom left ones, and the third value the bottom right corner
      corner4.pngThe 4-value syntax:border-radius: 1em 2em 3em 4em — The four values represent the top left, top right, bottom right and bottom left corners respectively, always in that order, that is clock-wise starting at the top left.

Background properties

A background with the following properties ...

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

... can be shortened to just one declaration:

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

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

Font properties

The following declarations ...

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

... can be shortened to the following:

css
font:  italic bold 0.8em/1.2 Arial,  sans-serif;

This shorthand declaration is actually equivalent to the longhand declarations above plusfont-variant: normal andfont-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, the following CSS ...

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

... can be simplified as:

css
border: 1px solid #000;

Margin and padding properties

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

css
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, beginning at the top: top, right, bottom, then left (TRBL, the consonants in "trouble").

css
margin: 10px 5px 10px 5px;

Смотрите также

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp