CSS shorthand, the shortcut approach.
Coding, Opinion, Web DesignPosted on Aug 20 by AelynAdd comments
I love Cascading Style Sheets, but even more so it’s shorthand properties. I won’t address all of them here but I hope to interest you enough to look into them later for an easier styling experience.
I know that every designer has his/her own way of coding and styling. It’s almost like a fingerprint, unique to its owner. But I also think that as we gain experience, we improve and advance in our skills. When I first started styling, I did the whole line-by-line unnecessary repeats of CSS that cluttered. With time, I discovered and began to use CSS shorthand and comment tags to organize and “tidy” style sheets. Today, that tendency has become a pet peeve.
CSS shorthand allows you to combine and specify different properties to a certain element in the same declaration or line. It validates too, in case you’re wondering.
Consider this short example:
In your CSS, a common way to approach padding or margin would be:
padding-top: 15px;
padding-right: 7px;
padding-bottom: 3px;
padding-left: 5px;With CSS shorthand you can combine them all together, like this:
padding: 15px 7px 3px 5px;
Positioning is important to know. An easy way to remember them (thanks to Skye) is to work your way clockwise. It’s padding: top right bottom left.
And it doesn’t stop there; it can be simplified further if you have the same values for all, top and bottom, or left and right, like below:
padding: 10px 10px 10px 10px; - - - > padding: 10px;
padding: 10px 5px 10px 5px; - - - > padding: 10px 5px;
padding: 0 8px 0 8px; - - - > padding: 0 8px;
Wasn’t that simple? Now, I’m not saying that not using CSS shorthand is wrong, it’s just easier. A clean and tidy style sheet is eye candy, but it’s also a preference. However, if you’re looking for a list of reasons why, here are some:
- It’s quick and efficient, minimizing redundancy while you code or style.
- It helps lessen the overall time it takes to load an external style sheet by keeping it short and simple.
- It’s easier to edit or find something when it’s organized.
- Visitors or curious web developers viewing your style sheet won’t need to decipher through unnecessarily to find something that intrigued them.
- It’s neater.
If you’re interested and want to learn more about where shorthand can be applied, you can take a look at any one of the two demonstrations below:
- Useful CSS Shorthand Properties by Trenton Moss.
- Efficient CSS with Shorthand Properties by Roger Johansson.
Or simply visit the more-than-able and much loved W3Schools.
August 20th, 2007 at 4:47 pm
I use that method when cleaning up my style sheet, because I can never remember top right bottom left. Now that I know that it’s clockwise, that helps a lot. Also, I didn’t know this one:
Thanks for posting!
August 22nd, 2007 at 7:52 pm
When I read this article, I was surprised at how simple things can get. I’m glad you wrote this - I have learned something new! It’s much easier to go with one row of padding besides 3 or 4+ rows!
Thanks for posting, Aelyn! I shall organize my style sheets in the future.
August 22nd, 2007 at 8:35 pm
CSS shorthand is definitely easier and it saves up a ton of space! Though for me, it’s easier to break down some of the properties such as background - but that’s just me - but definitely CSS shorthand is a must for padding, margins, and borders. Wonderful article, Aelyn.
August 23rd, 2007 at 5:39 pm
CSS shorthands do make things a lot easier. Sometimes, if you’re just defining one side, it may be simpler to do it the long way, but it’s a good habit to get into. Though I always mix up the order for some shorthand properties and have to look them up. Heh.
Another good way to save time and code is allowing traits to be inherited.
August 28th, 2007 at 9:39 am
Yay for neatness. I should go check out those sites sometimes and see if I can make my css shorter. Great article!
August 30th, 2007 at 9:22 pm
I noticed you stole my layout today Skye.
Response from Skye: It rocks, though, doesn’t it? haha!
August 30th, 2007 at 11:56 pm
Sorry for the bugs. I’ve fixed the navigation thing in the new version and expanded the height of the tabs but they’re not meant to stretch because (1) un-orderly (2) It’s meant to be a compact links tab not for long links and (3) ignore the last two excuses I tried and I simply failed to code it that way.
Thanks for picking out the bugs!