HTML/CSS Service

How to use Conditional CSS?

Category: CSS    |    1,721 views   |   

 

Conditional options in CSS have a variety of benefits but can also bring the same deficiencies as CSS variables in that they alter the state of CSS as it is. Many of the cons have to do with efficiency, confusion and, in some cases, added HTTP requests (because it deals with a type of server-side programming language).

Nonetheless, let’s look into more of the benefits of conditional CSS statements. Just keep in mind the cons that come with them, and always be thinking of new ideas to overcome them. So far, the tools and solutions that give developers and designers conditional statements in CSS have come to be relatively well accepted. This is because CSS conditionals seem to solve bigger problems in CSS, and the lack of efficiency seems minor by comparison.

Let’s not forget the original CSS conditional. The problem with the traditional CSS conditional for IE, however, is that it has no else or else if. Although the use may be limited, an if/else statement for CSS could allow designers and developers to specify styles for other types of conditions: browsers, for example.

One popular tool for calling styles according to browser type is Conditional-CSS.com. While many other solutions are out there for conditionals as well as this particular problem, this tool can do most of the work automatically, with minimal confusion.


Conditional-CSS allows the user to customize a Web script in their choice of language (PHP, C# or C), and it compiles the code along with an uploaded style sheet to create CSS conditionals based on browser type. With this tool, those nasty IE bugs become easier to handle, and other browser bugs and unsupported features can be handled more gracefully. Using this method, you no longer has to create a separate style sheet for IE (or any browser for that matter), instead using old-fashioned CSS conditionals.

For a closer look at how one can use the syntax of this tool, here is the sample code from the Conditional-CSS website:


  1. /* Conditional-CSS example */  
  2. a.button_active, a.button_unactive {  
  3. display: inline-block;  
  4. [if lte Gecko 1.8] display: -moz-inline-stack;  
  5. [if lte Konq 3.1] float: left;  
  6. height: 30px;  
  7. [if IE 5.0] margin-top: -1px;  
  8.   
  9. text-decoration: none;  
  10. outline: none;  
  11. [if IE] text-decoration: expression(hideFocus=‘true’);  
  12. }  

As you can see, the code is easy to read, write and handle. This is a smart solution to a common problem in Web development. However, as with CSS variables, there is the risk that developers will use this method to overpower CSS’ basic functionality.

 

Customized Native CSS Conditionals

What we discussed above is a great reason why developers would use conditional statements in CSS. But many of us would like even more control. For example, instead of being limited to browser type, many of us who work with fluid layouts would like to be able to create conditionals based on browser width:

  1. if($browser_window >= 600px && $browser_window < 1280px){  
  2.     p{width: 600px;}  
  3. }  
  4.   
  5. else if($browser_window < 600px){  
  6.     p{width: 90%;}  
  7. }  
  8.   
  9. else{  
  10.     p{width: 800px;}  
  11. }  

Similar to what we saw with Conditional-CSS, the solution is to attach a more advanced language, whether PHP, Ruby, JavaScript or another of your choice.

Dynamic Resolution Dependent Layouts is a great example of how to use an if/else statement for this problem. In addition, we can use the same idea to create customized conditionals in CSS using JavaScript.

The idea here is to use separate style sheets altogether, and then use a JavaScript (or a script in your preferred language) to call the correct style sheet. There are no conditional statements in the CSS at all; rather, the website just simulates them. And a default option is available if JavaScript is turned off. Also, using multiple style sheets will have no negative consequences because the JavaScript will be calling only one in the end, based on the conditional statement. This means there will be only one HTTP request, so it will not slow down the Web page.

Here is the first part of the example code:

  1. <link rel=“stylesheet” type=“text/css” href=“css/default.css”  
  2.     title=“default”/>  
  3.   
  4. <link rel=“alternate stylesheet” type=“text/css” href=“css/thin.css”  
  5.     title=“thin”/>  
  6.   
  7. <link rel=“alternate stylesheet” type=“text/css” href=“css/wide.css”  
  8.     title=“wide”/>  
  9.   
  10. <link rel=“alternate stylesheet” type=“text/css” href=“css/wider.css”  
  11.     title=“wider”/>  

By specifying the title attribute, we can use this in our JavaScript file:


  1. function dynamicLayout(){  
  2.     var browserWidth = getBrowserWidth();  
  3.   
  4.     //Load Thin CSS Rules  
  5.     if (browserWidth < 750){  
  6.         changeLayout(“thin”);  
  7.     }  
  8.     //Load Wide CSS Rules  
  9.     if ((browserWidth >= 750) && (browserWidth <= 950)){  
  10.         changeLayout(“wide”);  
  11.     }  
  12.     //Load Wider CSS Rules  
  13.     if (browserWidth > 950){  
  14.         changeLayout(“wider”);  
  15.     }  
  16. }  
  17.     
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

Tags:

  • No Related Post

0 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