The Beginner’s Guide to CSS Shorthand

Download Now: An Intro to HTML & CSS for Marketers Guide
Anna Fitzgerald
Anna Fitzgerald

Updated:

Published:

St. Jan. Vs. Dr. These are just a few examples of common abbreviations that you might use when writing a blog post, postcard, or text. Abbreviations can help you save space and time and avoid repeating long words and phrases.

Website owner learning CSS shorthand

Shorthand like this also exists in CSS. It can save developers time and space and make the codebase look cleaner by avoiding redundancies. Not all CSS properties can be written in shorthand, but some important ones can.

In this post, we’ll define what CSS shorthand is and then look at how to use the different shorthand properties.

Download Now: 25 Free HTML & CSS Hacks

In addition to the CSS border property, the margin, padding, background, font, animation, transition, and flex properties all have a shorthand. Let’s take a closer look at these shorthand properties below.

CSS Margin Shorthand

The margin property is shorthand for the following individual properties:

  • margin-top
  • margin-right
  • margin-bottom
  • margin-left

So I could write the following declarations in longhand:

margin-top: 20px; margin-right: 30px; margin-bottom: 70px; margin-left: 80px;

Or in one declaration using the shorthand margin property:

margin: 20px 30px 70px 80px;

The order of the values matter. In the example above, they start at the top and move clockwise. In addition to the order, the number of values specified matters.

  • If only one value is defined, then it applies to all sides of the element.
  • If two values are defined, then the first value represents the top and bottom margins and the second represents the right and left margins.
  • If three values are defined, the first value represents the top margin, the second represents the left and right, and the fourth represents the bottom margin.
  • If four values are defined, they represent the top, right, bottom, and left, respectively.

Let’s look at the example below.

See the Pen CSS Margin shorthand by Christina Perricone (@hubspot) on CodePen.

Padding Shorthand

The padding property is shorthand for the following individual properties:

  • padding-top
  • padding-right
  • padding-bottom
  • padding-left

So I could write the following declarations in longhand:

padding-top: 10px; padding-right: 20px; padding-bottom: 20px; padding-left: 40px;

Or in one declaration using the shorthand background property:

padding: 10px 20px 40px;

The rules about the order and number of values for the margin property apply to the padding property as well.

Here’s the example below.

See the Pen CSS Padding shorthand by Christina Perricone (@hubspot) on CodePen.

 

CSS Background Shorthand

The background property is shorthand for the following individual properties:

  • background-color
  • background-image
  • background-repeat
  • background-attachment
  • background-position

The background-color and background-image properties set the background color and image of an element. You can use the background-repeat property to control how the image is repeated, or if it is at all, and the background-attachment property to set whether the image is fixed or scrolls. The background-position property sets where the background image is positioned on the page.

So I could write the following declarations in longhand:

background-color: #CCCCCC; background-image: url(https://cdn.pixabay.com/photo/2017/03/15/15/36/outline-2146544_960_720.png); background-repeat: no-repeat; background-attachment: fixed; background-position: right top;

Or in one declaration using the shorthand background property:

background: #CCCCCC url(https://cdn.pixabay.com/photo/2017/03/15/15/36/outline-2146544_960_720.png) no-repeat fixed right top;

When using the shorthand property, you have to put the values in the order above: color, image, repeat, attachment, position. You don’t have to define all these properties though. If a value is missing, the shorthand will still be applied properly as long as the order of the values is correct. If you want to use another background property, like the background-size property, you must define it separately.

Look at the example below.

See the Pen CSS Background shorthand by Christina Perricone (@hubspot) on CodePen.

CSS Border Shorthand

The border property is shorthand for the following individual border properties:

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

So I could write the following declarations in longhand:

border-width: 10px; border-style: solid; border-color: #AA88FF;

Or in one declaration using the shorthand background property:

border: 10px solid #AA88FF;

A value for the border-style property must be defined or the border won’t render, but values for the width and color are optional. Without these values, the line will be rendered as black and about 3px wide. You can also specify these three border properties for just one side of the element by using border-top, border-right, border-bottom, or border-left.

Let’s look at the example below.

See the Pen CSS Border shorthand by Christina Perricone (@hubspot) on CodePen.

CSS Font Shorthand

The font property is shorthand for the following individual properties:

  • font-style
  • font-weight
  • font-variant
  • font-size / line-height
  • font-family

The font-style property specifies the style for a text; it can be normal, italic, oblique. The font-weight specifies the weight for a text and can have any of the following values: normal, bold, bolder, lighter, or 100-900. With the font-variant property, you can set the text to display in a small-caps font so that all lowercase letters will be converted to uppercase letters but appear in a smaller font size.

You can use the font-size property to set the size of text with a keyword, length value, or percentage. Or you can set the line-height property instead. Finally, using the font-family property, you can define multiple fonts in case the browser doesn’t support your first choice. It’s recommended you specify the name of the family font you want first, and end with a generic family name like “sans-serif” or “cursive.”

I could write the following declarations in longhand:

font-style: oblique; font-weight: 400; font-variant: small-caps; font-size: 24px; font-family: Times, serif;

Or in one declaration using the shorthand background property:

font: oblique 400 small-caps 24px Times, serif;

Values for the font-size and font-family properties must be specified, or the shorthand may not render in some browsers. If no values are specified for font-weight, font-style, or font-variant, then they’ll default to normal.

You should declare the values of font-size (and/or line-height) and font-family in the order above. Otherwise, the order doesn’t matter.

Let’s look at the example below.

See the Pen CSS font shorthand by Christina Perricone (@hubspot) on CodePen.

CSS Animation Shorthand

The animation property is shorthand for the following individual properties:

  • animation-duration
  • animation-name
  • animation-delay
  • animation-direction
  • animation-fill-mode
  • animation-iteration-count
  • animation-play-state
  • animation-timing-function

Values for the animation-duration property, which specifies the length of time of the animation sequence, and the animation-name property, which specifies the type of animation, are required. If the other properties aren’t specified, then they’ll be set to their default values. Here’s  a brief look at what each property does.

Using the animation-delay property, you can specify when the animation starts, using milliseconds or seconds, and positive or negative values. The animation-direction property defines the direction of the animation. Using the animation-fill-mode function, you can have the animation styles applied before or after the animation plays. The animation-iteration-count property specifies the number of times that the animation will play. With the animation-play-state property, you can pause and resume the animation sequence. Finally, the animation-timing-function sets the pace of the animation.

To learn more about these properties and their possible values, check out The Ultimate Guide to CSS Animation.

I could write the following declarations in longhand:

animation-duration: 5s; animation-name: example; animation-delay: 2s; animation-direction: alternate; animation-fill-mode: normal; animation-iteration-count: infinite; animation-play-state: running; animation-timing-function: ease-out;

Or in one declaration using the shorthand background property. Note: the animation-fill-mode and animation-play-state properties are set to their default values so I’ll leave them out of the shorthand below:

animation: 5s example 2s alternate infinite ease-out;

Let’s look at the example below.

See the Pen CSS animation shorthand by Christina Perricone (@hubspot) on CodePen.

CSS Transition Shorthand

The transition property is shorthand for the following individual properties:

  • transition-property
  • transition-duration
  • transition-timing-function
  • transition-delay

These properties are similar to the animation properties described above. The transition-delay property specifies when the transition effect begins. The transition-duration property specifies the length of time the transition effect takes to complete. The transition-property specifies the name of the CSS property the transition effect is for. So, for example, if this property is set to “width,” then the transition effect will take place when a user hovers over the element and its width begins to change. Finally, the transition-timing-function sets the pace of the transition effect.

If you’re wondering when to use the CSS animation or transition property, then check out The Main Difference Between CSS Animations & Transitions.

I could write the following declarations in longhand:

transition-property: background, color; transition-duration: 1s; transition-timing-function: ease-out; transition-delay: 60ms;

Or in one declaration using the shorthand background property:

transition: background-color 1s ease-out 60ms;

Let’s look at the example below.

See the Pen CSS Transition shorthand by Christina Perricone (@hubspot) on CodePen.

CSS Flex Shorthand

The flex property is shorthand for the following individual properties:

  • flex-grow
  • flex-shrink
  • flex-basis

With the flex-grow property, you can specify how much the flex item will grow relative to the other flex items or to the available space in the flex container. Or you can specify the flex item to shrink with the flex-shrink property. The flex-basis property sets the initial size of the flex item, which can be a percent or length unit.

I could write the following declarations in longhand:

flex-grow: 1; flex-shrink: 1; flex-basis: 1em;

Or in one declaration using the shorthand background property:

flex: 1 1 1em;

The flex property may be specified using one, two, or three values. The rules about the number of values and their order are a little trickier than the rules for the margin or padding property.

  • If there’s one value, it can be a number or keyword to set the flex-grow property. The value for the flex-shrink property is assumed to be 1, and the value for the flex-basis property is assumed to be 0.
  • If there’s two values, the first must be a number to set the flex-grow property. The second must be either a number to set the flex-shrink property or a width value for the flex-basis property. Two-value syntax
  • If there’s three values, then the first must be a number for the flex-grow property, the second a number for the flex-shrink property, and the third a value for the flex-basis property.

Let’s look at an example below.

See the Pen CSS Flex shorthand by Christina Perricone (@hubspot) on CodePen.

 

Coding in Shorthand

Using CSS shorthand can help you save time and space and make your codebase look cleaner. While there are rules to remember about the order and number of values you define, you can master them with a little practice.

New Call-to-action

 

Related Articles

Learn more about HTML and CSS and how to use them to improve your website.

    CMS Hub is flexible for marketers, powerful for developers, and gives customers a personalized, secure experience

    START FREE OR GET A DEMO