Shorthand properties - CSS: Cascading Style Sheets 编辑
Shorthand properties are CSS properties that let you set the values of multiple other CSS properties simultaneously. Using a shorthand property, you can write more concise (and often more readable) style sheets, saving time and energy.
The CSS specification defines shorthand properties to group the definition of common properties acting on the same theme. For instance, the CSS background
property is a shorthand property that's able to define the values of background-color
, background-image
, background-repeat
, and background-position
. Similarly, the most common font-related properties can be defined using the shorthand font
, and the different margins around a box can be defined using the 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:
- A value which is not specified is set to its initial value. That sounds anecdotal, but it really means that it overrides previously set values. Therefore:
will not set the color of the background tobackground-color: red; background: url(images/bg.gif) no-repeat left top;
red
but tobackground-color
's default,transparent
, as the second rule has precedence. - 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 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
. - 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:
- Shorthands handling properties related to edges of a box, like
border-style
,margin
orpadding
, always use a consistent 1-to-4-value syntax representing those edges:The 1-value syntax: border-width: 1em
— The unique value represents all edgesThe 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.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 edgeThe 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 word trouble: TRBL) (You can also remember it as the order that the hands would rotate on a clock:1em
starts in the 12 o'clock position, then2em
in the 3 o'clock position, then3em
in the 6 o'clock position, and4em
in the 9 o'clock position). - Similarly, shorthands handling properties related to corners of a box, like
border-radius
, always use a consistent 1-to-4-value syntax representing those corners:The 1-value syntax: border-radius: 1em
— The unique value represents all cornersThe 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.The 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 cornerThe 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.
- Shorthands handling properties related to edges of a box, like
Background properties
A background with the following properties ...
background-color: #000;
background-image: url(images/bg.gif);
background-repeat: no-repeat;
background-position: left top;
... can be shortened to just one declaration:
background: #000 url(images/bg.gif) no-repeat left top;
(The shorthand form is actually the equivalent of the longhand properties above plus background-attachment: scroll
and, in CSS3, some additional properties.)
See background
for more detailed information, including CSS3 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, the following CSS ...
border-width: 1px;
border-style: solid;
border-color: #000;
... can be simplified as:
border: 1px solid #000;
Margin and padding properties
Shorthand versions of margin and padding values work similarly; the margin property allows for shorthand values to be specified using one, two, three, or four values. The following CSS declarations ...
margin-top: 10px;
margin-right: 5px;
margin-bottom: 10px;
margin-left: 5px;
... are the same as the following declaration using the four value shorthand. Note that the values are in clockwise order, beginning at the top: top, right, bottom, then left (TRBL, the consonants in "trouble").
margin: 10px 5px 10px 5px;
Margin shorthand rules for one, two, three and four value declarations are:
- When one value is specified, it applies the same margin to all four sides.
- When two values are specified, the first margin applies to the top and bottom, the second to the left and right.
- When three values are specified, the first margin applies to the top, the second to the left and right, the third to the bottom.
- When four values are specified, the margins apply to the top, right, bottom, and left in that order (clockwise).
The universal shorthand property
CSS provides a universal shorthand property, all
, which applies its value to every property in the document. Its purpose is to change the properties' inheritance model to one of:
CSS provides four special universal property values for controlling inheritance. Every CSS property accepts these values.
inherit
- Sets the property value applied to a selected element to be the same as that of its parent element. Effectively, this "turns on inheritance".
initial
- Sets the property value applied to a selected element to the initial value of that property.
unset
- Resets the property to its natural value, which means that if the property is naturally inherited it acts like
inherit
, otherwise it acts likeinitial
.
Note: There is also a newer value, revert
, which has limited browser support.
Note: See Origin of CSS declarations in Introducing the CSS Cascade for more information on each of these and how they work.
We can look at a list of links and explore how universal values work. The live example below allows you to play with the CSS and see what happens when you make changes. Playing with code really is the best way to get to grips with HTML and CSS.
For example:
- The second list item has the class
my-class-1
applied. This sets the color of the<a>
element nested inside to inherit. If you remove the rule how does it change the color of the link? - Do you understand why the third and fourth links are the color that they are? Check the description of the values above if not.
- Which of the links will change color if you define a new color for the
<a>
element — for examplea { color: red; }
?
See Cascade and inheritance or Introducing the CSS Cascade for more information about how inheritance works in CSS.
See also
- CSS Key Concepts: CSS syntax, at-rule, comments, specificity and inheritance, the box, layout modes and visual formatting models, and margin collapsing, or the initial, computed, resolved, specified, used, and actual values. Definitions of value syntax, shorthand properties and replaced elements.
- Shorthand properties:
all
,animation
,background
,border
,border-block-end
,border-block-start
,border-bottom
,border-color
,border-image
,border-inline-end
,border-inline-start
,border-left
,border-radius
,border-right
,border-style
,border-top
,border-width
,column-rule
,columns
,flex
,flex-flow
,font
,gap
,grid
,grid-area
,grid-column
,grid-row
,grid-template
,list-style
,margin
,mask
,offset
,outline
,overflow
,padding
,place-content
,place-items
,place-self
,scroll-margin
,scroll-padding
,text-decoration
,text-emphasis
,transition
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论