Introduction
This is the concluding part in a three part blog on Cascading Style Sheets, or CSS. In the first part, we saw what CSS is and where and why it is used. In the second part, we saw what Semantic Markup is, how it has been built into CSS and what enhancements it brings for Web pages.
In this part, we shall see how style rules are defined and what kind of issues may arise in displaying the same Web page across different browsers.
Defining style rules
- Selectors
Every CSS style definition has three parts: a selector, a property and a value. The selector is normally the HTML element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon (:), and surrounded by curly braces as shown in the code sample below. Selectors can be grouped so that the same style rule can be applied to multiple elements. This selector needs to be separated by a comma to form a group.
h1, h2, h3, h4, h5, h6, p
{
color: green
}
- The class selector
Using a class selector, you can define different style rules for multiple elements. The class selector is defined as a selector name prefixed by a dot (.). You can apply multiple classes to elements which give you the flexibility to define more rules, displaying the page accordingly, as shown next.
.contentarea
{
width: 200px;
}
.content
{
margin: 0;
padding: 0;
color: #cccccc;
}
Applying the above classes,
<p class=”contentarea content”> Some content here … </p> <p class=”content”> Some content here … </p>
- The id selector
You can define style rules to HTML elements with the id selector, which is defined as a hash (#). In the example below, the style rule will be applied to the element which has the id ‘someid’. With this, you can define different style rules for multiple elements. You can apply multiple classes to elements giving you the flexibility to define more rules and accordingly displaying the page, as shown below.
#someid
{
font-weight:bold;
}
Applying the above classes,
<p id=”someid”> Some content here … </p>
Note: As a rule, you can have only one element in a page with one id. Having multiple elements with the same id is not recommended and your scripts might not give you the desired output.
- Adding comments in CSS
To explain what you are doing in the style rules, you can insert comments in CSS, using the /* and */ tags.
#someid
{
/* The following rule will make the font bold on screen!!! */
font-weight:bold;
}
Next, we shall see some cross-browser issues that we need to handle during the coding of our Web page.
Cross-browser issues
The designer of a Web page can never know what browser the user may use to browse his Web page. This is aggravated by the fact that users now have several lighter and faster browsers to browse the Web. This makes your task as a Web developer more difficult as unfortunately all these browsers follow standards in their own way.
Nonetheless, as the developer, you have a few tools to make sure that your Web page loads correctly in the majority of browsers. One of them is testing your page on as many browsers as possible, at least on Microsoft Internet Explorer versions 6 and 7, and Firefox versions 2.x and 3.x.
You can also use tools such as conditional CSS linking, improving the compatibility of your page for the browser on which it will be viewed. To link CSS files conditionally in your Web page, you can use the following conditional statements in your code for Internet Explorer versions 6 and 7:
<!–[if IE 6]> <link src=“ieg_6.css” type=“text/css” /> <! [endif]– >
<!–[if IE 7]> <link src=“ieg_7.css” type=“text/css” /> <! [endif]– >
This will do the following while loading your page: If the user is using Microsoft Internet Explorer 6, then styles from ieg_6.css will be applied to the page while the other CSS file will be ignored.
On the other hand if the user is browsing using Internet Explorer 7, styles will be applied from ieg_7.css. This approach provides you the flexibility to maintain separate CSS files specific to Internet Explorer 6 and 7. We can have a common file in which all the common rules can be specified and in these specific files, we can override the rules for these specific browsers. The obvious advantage of using this method is improved code management since in case of a problem, we need not go through the entire hierarchy of code files for the site. We can straight away look into the specific file and apply the necessary fixes.
Further study and references
A lot of material on CSS is available on the Web; some of the links are mentioned below.
- Yahoo Developer Network: http://developer.yahoo.com/performance/rules.html
- CSS Filters from the now-defunct dithered.com: http://www.communis.co.uk/dithered/css_filters/index.html
- CSS Hack category on the CSS-Discuss Wiki: http://css-discuss.incutio.com/?page=CssHack
- W3Schools: http://w3schools.com










