{"id":85,"date":"2008-04-07T07:00:50","date_gmt":"2008-04-07T12:00:50","guid":{"rendered":"http:\/\/cssnewbie.com\/css-shorthand\/"},"modified":"2008-04-07T07:00:50","modified_gmt":"2008-04-07T12:00:50","slug":"css-shorthand","status":"publish","type":"post","link":"https:\/\/cssdeck.com\/blog\/css-shorthand\/","title":{"rendered":"Writing CSS Shorthand"},"content":{"rendered":"<p><img src='https:\/\/cssdeck.com\/blog\/wp-content\/uploads\/2008\/04\/shorthand-400.jpg' alt='\"Jeri Rowe\" by spcoon.' title=\"'Jeri Rowe' by spcoon. Used under a Creative Commons license.\" \/><\/p>\n<p>Writing Cascading Style Sheets saves you time and bandwidth in the long run by removing all of the presentational elements and attributes from your web pages and moving them into a separate document. But sometimes that CSS document itself can get pretty long as well. So what do you do then?<\/p>\n<p>There are lots of things you can do to help \u2013 <a href=\"\/combating-classitis\/\" title=\"Combating Classitis with Cascades and Sequential Selectors\">embracing the cascading nature of CSS<\/a> helps a great deal, as does <a href=\"\/css-rules-multiplicity\/\" title=\"CSS Rules of Multiplicity\">combining CSS declarations<\/a> using sequential selectors. But another trick that can really help cut down on the size of your CSS is to use CSS shorthand properties whenever possible. There are six shorthand properties for various areas of your CSS: margins, padding, borders, backgrounds, list-styles, and fonts. I\u2019ll go through each of them below.<\/p>\n<p><strong>The margin shorthand property<\/strong> combines the margin-top, margin-right, margin-bottom, and margin-left properties into one single property. So instead of writing this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">div {\n\tmargin-top: 5px;\n\tmargin-right: 8px;\n\tmargin-bottom: 3px;\n\tmargin-left: 4px; }<\/pre>\n<p>You could shorten it all down to this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">div { margin: 5px 8px 3px 4px; }<\/pre>\n<p>It\u2019s important to remember to always put your margins in the shorthand property in the following order: top, right, bottom, and left. Basically, just start at the top and work your way around the element clockwise. And if your top\/bottom and left\/right margins match, you can boil your shorthand down even further:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">div { margin: 5px 8px; }<\/pre>\n<p>The rule above applies a 5 pixel margin to the top and bottom of your div, and an 8 pixel margin to the left and right sides. If all four of your margins match, you could even just write this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">div { margin: 5px; }<\/pre>\n<p><strong>The padding shorthand property <\/strong>works exactly the same way as the margin shorthand. The biggest thing to remember about both of these properties is to start at the top and work your way around clockwise. And if you\u2019re shortening it to two values, the top\/bottom value always goes first, followed by the left\/right value. Further, if you don\u2019t need to specify a value on any one of the sides, you can just specify a zero (0) size with no unit of measurement.<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">div { padding: 30px 0; }<\/pre>\n<p><strong>The border property<\/strong> allows you to combine the border-width, border-style, and border-color properties into one. The width comes first, followed by the style, and then the color. So instead of writing out all this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">div {\n\tborder-width: 3px;\n\tborder-style: solid;\n\tborder-color: #c00; }<\/pre>\n<p>You could boil it down to a single rule, like so: <\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">div { border: 3px solid #c00; }<\/pre>\n<p><strong>The background property<\/strong> is fairly powerful, in that it combines five rules into one: background-color, background-image, background-repeat, background-attachment, and background-position (in that order). So instead of writing this verbose mess of code:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">div {\n\tbackground-color: #fff;\n\tbackground-image: url(..\/images\/bg.gif);\n\tbackground-repeat: repeat-y;\n\tbackground-attachment: fixed;\n\tbackground-position: top center; }<\/pre>\n<p>We could boil all of that down to this little snippet:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">div { background: #fff url(..\/img\/bg.gif) repeat-y fixed top; }<\/pre>\n<p>Also note that I skipped the \u201ccenter\u201d portion of my background-position property: if you specify one background position (i.e. \u201ctop\u201d) but neglect to specify a second position value, \u201ccenter\u201d is the assumed value.<\/p>\n<p><strong>The list-style shorthand property<\/strong> isn\u2019t used all that often, but it can save you a couple of lines of code. It combines the list-style-position property with either of the list-style-type or list-style-image properties \u2013 you could probably specify both, but list-style-image would overwrite the list-style-type property with whatever image you selected. So instead of writing this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">ul {\n\tlist-style-type: square;\n\tlist-style-position: inside; }<\/pre>\n<p>You could write this:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">ul { list-style: square inside; }<\/pre>\n<p>Generally speaking, however, I tend to only use this shorthand when I\u2019m looking to remove styling from the list (as when building a navigation bar):<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">ul { list-style: none; }<\/pre>\n<p><strong>The font shorthand property<\/strong> is probably the most powerful of all the shorthand properties. It combines a grand total of six properties into one: font-style, font-variant, font-weight, font-size, line-height (even though it\u2019s not technically a font property), and font-family. So instead of writing out all six of these rules:<\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">p {\n\tfont-style: italic;\n\tfont-variant: small-caps;\n\tfont-weight: bold;\n\tfont-size: small;\n\tline-height: 1.2em;\n\tfont-family: Helvetica, Arial, sans-serif; }<\/pre>\n<p>I can get by with a single declaration: <\/p>\n<pre lang=\"css\" escaped=\"true\" line=\"1\">p { font: italic small-caps bold small\/1.2em Helvetica, Arial, sans-serif; }<\/pre>\n<p>Of course, most of the time you won\u2019t be specifying all six of those properties at once \u2013 I can\u2019t even imagine how difficult it would be to read italicized, bold-faced small caps! But it is very useful for specifying your font-size, line-height, and font-family all in one place. That way, all of your typeface information sits one convenient location.<\/p>\n<div class=\"wp-socializer wpsr-share-icons \" data-lg-action=\"show\" data-sm-action=\"show\" data-sm-width=\"768\" ><h3>Share and Enjoy !<\/h3><div class=\"wpsr-si-inner\"><div class=\"wpsr-counter wpsrc-sz-32px\" style=\"color:#000\"><span class=\"scount\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook,twitter,linkedin,pinterest,print,pdf\">0<\/span><\/span><small class=\"stext\">Shares<\/small><\/div><div class=\"socializer sr-popup sr-32px sr-circle sr-opacity sr-pad sr-count-1 sr-count-1\"><span class=\"sr-facebook\"><a rel=\"nofollow\" href=\"https:\/\/www.facebook.com\/share.php?u=\" target=\"_blank\"  title=\"Share this on Facebook\"  style=\"color: #ffffff\" ><i class=\"fab fa-facebook-f\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"facebook\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-twitter\"><a rel=\"nofollow\" href=\"https:\/\/twitter.com\/intent\/tweet?text=%20-%20%20\" target=\"_blank\"  title=\"Tweet this !\"  style=\"color: #ffffff\" ><i class=\"fab fa-twitter\"><\/i><\/a><\/span>\n<span class=\"sr-linkedin\"><a rel=\"nofollow\" href=\"https:\/\/www.linkedin.com\/sharing\/share-offsite\/?url=\" target=\"_blank\"  title=\"Add this to LinkedIn\"  style=\"color: #ffffff\" ><i class=\"fab fa-linkedin-in\"><\/i><\/a><\/span>\n<span class=\"sr-pinterest\"><a rel=\"nofollow\" href=\"https:\/\/www.pinterest.com\/pin\/create\/button\/?url=&amp;media=&amp;description=\" target=\"_blank\"  title=\"Submit this to Pinterest\"  style=\"color: #ffffff\" data-pin-custom=\"true\"><i class=\"fab fa-pinterest\"><\/i><span class=\"ctext\"><span data-wpsrs=\"\" data-wpsrs-svcs=\"pinterest\">0<\/span><\/span><\/a><\/span>\n<span class=\"sr-print\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Print this article \"  style=\"color: #ffffff\" ><i class=\"fa fa-print\"><\/i><\/a><\/span>\n<span class=\"sr-pdf\"><a rel=\"nofollow\" href=\"https:\/\/www.printfriendly.com\/print?url=\" target=\"_blank\"  title=\"Convert to PDF\"  style=\"color: #ffffff\" ><i class=\"fa fa-file-pdf\"><\/i><\/a><\/span><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<\/p>\n<p>Writing Cascading Style Sheets saves you time and bandwidth in the long run by removing all of the presentational elements and attributes from your web pages and moving them into a separate document. But sometimes that CSS document itself can [&#8230;]<\/p>\n<p><a class=\"more-link article\" href=\"https:\/\/cssdeck.com\/blog\/css-shorthand\/\" title=\"Click to read 'Writing CSS Shorthand'\">Read Article<\/a><\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[168,173],"tags":[57,151,269,89,120,118],"_links":{"self":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/85"}],"collection":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":0,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"wp:attachment":[{"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cssdeck.com\/blog\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}