How to Write Shorthand CSS

Today I wanted to talk about what shorthand CSS is and how it can help lower the amount of CSS you the developer has to write. In the whole scheme of things, CSS is here to help us sum up large amounts of styles and attributes into compact rules and classes that we cascade down our site. With shorthand CSS methods, we are compacting down our CSS even further by stating our styles much differently. To better explain shorthand CSS let’s look at a traditional long hand version of CSS. Normally in CSS1 you would declare all four sides to an element for things such as margin or padding like so.





Notice how we have declared all four sides of our element to have different amounts of margin, with short hand CSS we can do the same as above but much faster. Here is the same four unique values for our elements four sides but in shorthand CSS.

margin:5px 15px 20px 10px;

In one single declaration of the margin, we have summed up all four sides of our element and given unique values or rather amounts to each side. Now it’s also to point out that when you use shorthand CSS there are some rules you must fallow in how you are allowed to send values to these styles. When writing shorthand CSS like the demo above you will notice that there is a specific order in which we must give value to each side of our elements. Always think of the above short hand method as this.

margin:top right bottom left;

First value is our elements top value, next is the right value then the bottom value and finally the left value. Whenever you want to write shorthand CSS to declare all four sides of an element you must obey this order over values. Here is a helpful way to remember each side and the order they belong.


Great we can also write the above shorthand style even shorter if we say wanted to have a non unique value for each side of our elements. Let’s say we wanted our element to have a margin of 10 pixels on all four sides. We would simply write our margin style like this.


Because we have left out our “right, bottom and left” values, our one and only value is cascades down over the rest of the elements sides starting from the top. Another way to look at this would be to give our element of a margin of 10 pixels for its top side, but the rest be 15 pixels.

margin:10px 15px;

Again the last known value was 15px therefore it cascades down over the rest of our elements sides.
Shorthand CSS is not limited to just the margin style but a whole array of styles.
Another often used shorthand method is for colors or rather hex numbers. Here is what a full hex color of black would look like.


But did you know that you can write the same hex color shorter?


HTML or rather hex colors are a mixture of red green and blue.

#red green blue

Giving a closer inspection to the hex number you will see that there are three sets of numbers that make up our red green and blue values. In each set of numbers you have a web safe color value and a second value to vary in how much saturation is in each web safe color. So you can think of hex colors as this:

#red, how much red    green, how much green    blue, how much blue

So when we are writing shorthand CSS colors we are basically removing the second value in each pair and only stating the basic red green and blue color values.

So this hex color:


is the same as this hex color:


Writing shorthand colors is only allowed if you are using web safe colors. If I were to choose a non web safe color and try to compress it down I would end up with two different colors in the end.

Non web safe color: #45AFE2

Shorthand non web safe color: #4AE

Notice how the two colors vary ever so slightly, this is because you have basically taken the saturation values out of this non web safe hex color and stated only the red green blue values. Non web safe colors rely heavily on these saturation values and without them the color defaults to a web safe color, but again your desired color will be off.
Ok let’s get more in depth with some other shorthand CSS methods. Another great shorthand CSS style would be for borders. Normally you have this whole array of border styles to work with to obtain the perfect border solution for whatever the need here are a few commonly used border styles.





With shorthand CSS we can sum up all these styles into one single decliration like this.

border:#000 solid 1px;

Wow that’s a lot smaller than having to use all the styles from above to create a simple border right? So with shorthand CSS borders we again have a very specific order in what values are being declaird much like our margin shorthand method. Always think of shorthand borders as this:

border: border-color, border-style, border-width;

Now with our above shorthand border we are assuming that you want all four border sides to be a 1 pixel wide border, however what if you want to have different border sizes on different sides of an element? Well normally you have control of an elements borders and sides with this array of border styles.





Look familier? Like our margin shorthand method we can apply it to our borders by simply adding a second shorthand rule to our first shorthand rule.

border:#000 solid;
border-width:1px 2px 1px 2px;

Unfortunaly we do have to remove our border width from the “border” shorthand rule and create a second “border-width” shorthand rule to give different border widths to each side, but in the end don’t you think these two styles are much better than having to write this.







And that’s all for today folks, by now you should be familiar with at least what shorthand CSS is and how it can help us. If you would like to know absolutely ever shorthand method I suggest you read this and take your CSS compressing to the next level.

Devin R. Olsen

Devin R. Olsen

Devin R. Olsen

Located in Portland Oregon. I like to teach, share and dabble deep into the digital dark arts of web and game development.

More Posts

Follow Me:TwitterFacebookGoogle Plus