CSS(Cascading Style Sheets) in an asset to our web. Apart from being ubiquitous it's also indispensable for enterprise level web projects. No surprises, web designers and developers find themselves facing questions on it when they appear for an interview. Yeah, it's that important !!! If you've still not seen what CSS does then you go [Here]. Quite frankly, CSS 2.0 has evolved very well with a lot of new properties added to it along with new concepts like Pseudo classes, etc. A majority of 21st generation browsers like IE 7, Firefox 2.0, Chrome 2.0(stable), Safari, Opera 8.0 and above have inherent support of CSS 2.0 and some even support v2.1. The only exception to the rule is IE 6 which I don't believe stands in the league but if you still feel otherwise then you seriously need a dose of Mozilla Firefox !!!
I've seen numerous examples of internal and external CSS being explained over forums and across many websites but rarely does any of them explain the different ways by which you can access a CSS class. So I'll explain it here...
I've seen numerous examples of internal and external CSS being explained over forums and across many websites but rarely does any of them explain the different ways by which you can access a CSS class. So I'll explain it here...
Option 1 : You can define a class named after the type of the HTML [container] element
Result : The style will be applied to all the elements of the same type
Example : The DIV and SPAN tags
CODE Snippet :
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <style type="text/css">
- span
- {
- font-family: Calibri;
- background: Aqua;
- text-align: center;
- }
- </style>
- </head>
- <body>
- <span>Example 1</span>
- <br />
- <span>All SPAN tags will have the same attributes</span>
- </body>
- </html>
Option 2 : You can define a class using an identifier prefixed with a '.' operator. Do not include spaces in the identifier and keep it unique. You can then access it using the 'class' attribute of the element to whom you want to apply this style to.
Result : The style will be applied only to those elements which [reference] this identifier
CODE Snippet :
- CSS :
- .style1
- {
- background: Aqua;
- text-align: center;
- }
- HTML :
- <span class="style1">This is Example 2</span>
Option 3 : You can define a class using an identifier as above but prefix it with a '#' operator. You can then access it using the 'id' attribute of the element to whom you want to apply this style to.
Result : The style will be applied only to those elements which [has] this identifier
CODE Snippet :
- CSS :
- #style1
- {
- background: Aqua;
- text-align: center;
- }
HTML :
- <span id="style1">This is Example 2</span>
Now having learnt the above three methods of accessing a CSS class, its time to apply multiple classes at the same time.
Option 4 : You can define multiple classes with unique identifiers as in Example 2 and then apply the classes to the elements in their 'class' attribute seperated by a space.
CODE Snippet :
- CSS :
- .style1
- {
- background: Aqua;
- text-align: center;
- }
- .style2
- {
- font-family:Calibri;
- }
HTML :
- <span id="style1 style2">This is Example 2</span>
0 comments:
Post a Comment