HTML/CSS Service

Smarter CSS Shortcuts

Category: CSS, CSS Expert Ideas    |    2,494 views   |   

 

A much-wanted feature in future versions of CSS is to be able to use alternative and smarter shortcuts to shorten CSS code. Some of these are already being implemented in alternative CSS languages, but the new CSS 3 has no sign of this. With greater support, perhaps basic CSS could become smart enough to handle these shortcuts in another 10 or so years, when CSS 4 is introduced.

To see the benefit anyway, let’s look at a few examples in which smarter shortcuts would be beneficial.

When many elements from a certain class or ID share the same properties, things can get a bit repetitious:

  1. #navigation h1#navigation h2#navigation h3{  
  2.     font-familyVerdanaTahomasans-serif;  
  3.     letter-spacing-1px;  
  4. }  

Smarter syntax would tighten things up, saving both time and space in the style sheet:


  1. #navigation [h1,h2,h3]{  
  2.     font-familyVerdanaTahomasans-serif;  
  3.     letter-spacing-1px;  
  4. }  

As most of us know, styling links in CSS can be a huge hassle. Nesting these elements, as shown above, is a great solution, but a smarter syntax for dealing with pseudo-classes would also be good:


  1. a:link, a:active, a:visited  
  2. {  
  3.     fontbold 10pt VerdanaTahomasans-serif;  
  4.     color#444444;  
  5. }  
  6.   
  7. a:hover  
  8. {  
  9.     color:#000;  
  10. }  
  11.   
  12.     #navigation a:link, #navigation a:visited,  
  13.     {  
  14.         fontnormal 10pt VerdanaTahomasans-serif;  
  15.         color#000;  
  16.     }  
  17.   
  18.     #navigation a:active, #navigation a:hover  
  19.     {  
  20.         font-size14pt;  
  21.     }  

Having all these pseudo-classes in the way is rather inefficient. Grouping them would be much smarter:


  1. a[:link, :active, :visited] {  
  2.     fontbold 10pt VerdanaTahomasans-serif;  
  3.     color#444444;  
  4. }  
  5.   
  6. a:hover{ color:#000; }  
  7.   
  8.     #navigation a[:link, :visited] {  
  9.         fontnormal 10pt VerdanaTahomasans-serif;  
  10.         color#000;  
  11.     }  
  12.   
  13.     #navigation a[:active, :hover] {  
  14.         font-size14pt;  
  15.     }  

While the solution in the example above saves only a little space, that extra space can do wonders in a larger style sheet. And most will agree that this is a much easier way to read and understand style sheets.

The general goal for any situation like this is to be able to share elements, pseudo-classes and any other type of extension with o one declared class or ID, rather than repeating the class or ID name repeatedly.

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • digg
  • del.icio.us
  • Stumble it
  • Furl
  • Reddit
  • BlinkList
  • Simpy
  • YahooMyWeb
  • Spurl

Share/Save/Bookmark

1 Star2 Stars (+2)

Tags:

  • No Related Post

2 responses so far!

Leave a Comment

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word