T-XU.COM      
Home |
  Home>Computer Programming>CSS>
Introduction To Cascading Style Sheets
By Mitchell Harper        [Hits: 8567]



CSS (Cascading Style Sheets) have been around for a while now,and act as a complement to plain old HTML files. Style sheetsallow a developer to separate HTML code from formatting rulesand styles. It seems like many HTML beginners¡¯ under-estimatethe power and flexibility of the style sheet. In this article,I¡¯m going to describe what cascading style sheets are, theirbenefits, and two ways to implement them.

--------------------------------------- Cascading whats?---------------------------------------

Cascading Style Sheets¡­that¡¯s what! They¡¯re what paint is tocanvas, what topping is to ice cream¡­ they complement HTML andallow us to define the style (look and feel) for our entire sitein just one file!

Cascading style sheets were introduced to the web developmentworld way back in 1996. They get their name from the fact thateach different style declaration can be ¡°cascaded¡± under the oneabove it, forming a parent-child relationship between thestyles.

They were quickly standardized, and both Internet Explorer andNetscape built their latest browser releases to match the CSSstandard (or, to match it as closely as they could).

So, you¡¯re still asking what a style sheet exactly is? A stylesheet is a free-flowing document that can either be referencedby, or included into a HTML document. Style sheets use blocks offormatted code to define styles for existing HTML elements, ornew styles, called classes.

Style sheets can be used to change the height of some text, tochange the background color of a page, to set the default bordercolor of a table¡­the list goes on and on. Put simply though,style sheets are used to set the formatting, color scheme andstyle of an HTML page.

Style sheets should be used instead of the standard , , and tags because:

- One style sheet can be referenced from many pages, meaningthat each file is kept to a minimum size and only requires onlyextra line to load the external style sheet file

- If you ever need to change any part of your sites look/feel,it can be done quickly and only needs to be done in one place:the style sheet.

- With cascading style sheets, there are many, many pageattributes that simply cannot be set without them: individualtags can have different background colors, borders, indents,shadows, etc.

Style sheets can either be inline (included as part of a HTMLdocument), or, referenced externally (Contained in a separatefile and referenced from the HTML document). Inline style sheetsare contained wholly within a HTML document and will only changethe look and layout of that HTML file.

Open your favorite text editor and enter the following code.Save the file as stylesheet.html and open it in your browser:

Cascading Style Sheet Example < itle><style> h1 { color: #636594; font-family: Verdana; size: 18pt; }</style> </head> <body> <h1>This is one big H1 tag!</h1> </body></html> <br><br>When you fire up your browser, you should see the text "This isone big H1 tag!" in a large, blue Verdana font face.<br><br>Let¡¯s step through the style code step by step. Firstly, we havea pretty standard HTML header. The page starts with the <html>tag followed by the <head> tag. Next, we use a standard <title>tag to set the title of the page we are working with. <br><br>Notice, though, that before the <head> tag is closed, we haveour <style> tag, its contents, and then the closing </style> tag.<br><br><style> h1 { color: #636594; font-family: Verdana; size: 18pt; }</style> <br><br>When you add the style sheet code inline (as part of the HTMLdocument), it must be bound by <style> and </style> tagsrespectively. Our example is working with the <h1> tag. We arechanging three attributes of the <h1>¡¯s style: the text color(color), the font that any <h1> tags on the page will bedisplayed in (font-family), and lastly, the size of the font(size). <br><br>The code between the { and } are known as the attributes. Oursample code has three. Try changing the hexadecimal value of thecolor attribute to #A00808 and then save and refresh the page.You should see the same text, just coloured red instead of blue.<br><br>--------------------------------------- An example of anexternal style sheet ---------------------------------------<br><br>External style sheets are similar to internal style sheets,however, they are stripped of the <style> and </style> tags, andneed to be referenced from another HTML file to be used. <br><br>Create a new file called ¡°mystyle.css¡± and enter the followingcode into it:<br><br>h1 { color: #a00808; font-family: Verdana; size: 18pt } <br><br>Next, create a HTML file and name it external.html. Enter thefollowing code into external.html:<br><br><html> <head> <title> External Style Sheet Reference Example <itle> <link rel="stylesheet" type="text/css" href="mystyle.css"></head> <body> <h1>This is one big H1 tag!</h1> </body> </html> <br><br>As mentioned above, you can see that the actual code inmystyle.css is exactly the same as it was in the inline example.In our HTML file, we simply place a <link> tag in the <head>section of our page. The rel=¡±stylesheet¡± attribute tells thebrowser that the link to the external file is a style sheet. Thetype=¡±text/css¡± attribute tells the browser that mystyle.css isa text file containing css (cascading style sheet) declarations.Lastly, the href=¡±mystyle.css¡± attribute tells the browser thatthe actual file we want to load is mystyle.css. <br><br>--------------------------------------- Conclusion---------------------------------------<br><br>Well, there you have it, a quick look at style sheets and how toimplement both an inline and external version. Checkout thelinks below if you¡¯ve never worked with cascading style sheetsbefore. You will be surprised at some of the things you can dowith them! <br><br>- http://www.devarticles.com/art/1/7 -http://hotwired.lycos.com/webmonkey/98/15/index0a.html -http://www.webreview.com/style/index.shtml -http://jigsaw.w3.org/css-validator/ <br> <script src="/plus/count.php?aid=52915&mid=0" language="javascript"></script> <center></center> </td> <td style="vertical-align:top"> <script type="text/javascript"><!-- google_ad_client = "pub-9203237434407766"; google_ad_width = 160; google_ad_height = 600; google_ad_format = "160x600_as"; google_ad_type = "text_image"; //2007-07-10: txu_articles_bottom google_ad_channel = "9900206766"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_text = "000000"; google_color_url = "008000"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </td> </tr> </table></td> </tr> </table></td> </tr> </table> </td> <td width="224" align="right" valign="top"> <table width="98%" border="0" cellpadding="0" cellspacing="0" class="tbspan"> <tr> <td class="guidet"> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="gtb"> <tr> <td height="29" background="/templets/img/31bg3.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="32%" align="right"><img src="/templets/img/i.gif" width="10" height="13"></td> <td width="2%"> </td> <td width="66%" class="guidetitle"><strong>Top Articles</strong></td> </tr> </table></td> </tr> </table></td> </tr> <tr> <td height="50" valign="top" class="guideb"> *<a href="/css/css-the-basics-id-s-and-classes--correct-aid52913.htm">CSS: The Basics - ID's and Cla</a><br/> *<a href="/css/css-the-basics-id-s-and-classes-aid52914.htm">CSS: The Basics - ID's and Cla</a><br/> *<a href="/css/the-power-of-css-aid52886.htm">The Power of CSS</a><br/> *<a href="/css/the-30-minute-css-tutorial--aid152948.htm"> The 30 minute CSS tutorial.</a><br/> *<a href="/css/css-browser-detection-the-complete-guide-aid52891.htm">CSS Browser Detection - The co</a><br/> *<a href="/css/simon-says-aid154890.htm">Simon Says</a><br/> *<a href="/css/using-css-with-tables-aid52881.htm">Using CSS with Tables</a><br/> *<a href="/css/cascading-stylesheets-5-reasons-to-use-css-aid52889.htm">Cascading Stylesheets: 5 Reaso</a><br/> *<a href="/css/7-reasons-why-using-css-is-a-must-aid52896.htm">7 Reasons Why Using CSS is a M</a><br/> *<a href="/css/teach-yourself-css-the-easy-way-aid52902.htm">Teach yourself CSS the easy wa</a><br/> *<a href="/css/starting-cascading-style-sheets-aid52899.htm">Starting Cascading Style Sheet</a><br/> *<a href="/css/css-maximum-benefits-aid52912.htm">CSS - Maximum benefits</a><br/> </td> </tr> </table> <table width="98%" border="0" cellpadding="0" cellspacing="0" class="tbspan"> <tr> <td class="guidet"> <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" class="gtb"> <tr> <td height="29" background="/templets/img/31bg3.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="32%" align="right"><img src="/templets/img/i.gif" width="10" height="13"></td> <td width="2%"> </td> <td width="66%" class="guidetitle"><strong>Related Articles</strong></td> </tr> </table></td> </tr> </table></td> </tr> <tr> <td height="50" valign="top" class="guideb"> *<a href="/css/css-the-basics-id-s-and-classes-aid52914.htm">CSS: The Basics - ID's and Cla</a><br/> *<a href="/css/css-the-basics-id-s-and-classes--correct-aid52913.htm">CSS: The Basics - ID's and Cla</a><br/> *<a href="/css/css-maximum-benefits-aid52912.htm">CSS - Maximum benefits</a><br/> *<a href="/css/using-ccs-to-eliminate-tables-aid52911.htm">Using CCS to Eliminate Tables</a><br/> *<a href="/css/css-print-media-tutorial-aid52906.htm">CSS Print Media Tutorial</a><br/> *<a href="/css/teach-yourself-css-the-easy-way-aid52902.htm">Teach yourself CSS the easy wa</a><br/> *<a href="/css/starting-cascading-style-sheets-aid52899.htm">Starting Cascading Style Sheet</a><br/> *<a href="/css/seo-benefits-of-css-aid52897.htm">SEO Benefits Of CSS</a><br/> *<a href="/css/7-reasons-why-using-css-is-a-must-aid52896.htm">7 Reasons Why Using CSS is a M</a><br/> *<a href="/css/font-organizers-review-part-i-aid52895.htm">Font Organizers Review, Part I</a><br/> *<a href="/css/css-in-flash-the-return-of-crisp-and-legible-fonts--aid52893.htm">CSS in Flash the return of cri</a><br/> *<a href="/css/css-browser-detection-the-complete-guide-aid52891.htm">CSS Browser Detection - The co</a><br/> </td> </tr> </table></td> </tr> </table> <div> <br/><br/> <b>Prev:</b> <a href='/css/css-the-basics-id-s-and-classes-aid52914.htm'>CSS: The Basics - ID's and Classes</a>   <b>Next: </b><a href='/plus/view.php?aid=52916'>A Stop On the Road of Life</a> <br/><br/> <script type="text/javascript"><!-- google_ad_client = "pub-9203237434407766"; google_ad_width = 728; google_ad_height = 15; google_ad_format = "728x15_0ads_al_s"; //2007-07-10: txu-articles_nav_bottom google_ad_channel = "9256344755"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_text = "000000"; google_color_url = "008000"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <br/><br/> </div> <table width="900" border="0" align="center" cellpadding="0" cellspacing="0" class="tbspan" style="margin-top:3px"> <tr> <td height="51" background="/templets/img/31bg5.gif" valign="top"><img src='/templets/img/31c5.gif' width='4' height='49'></td> <td background="/templets/img/31bg5.gif" align="center"> <a href='/'>Home</a> | <a href="/plus/sitemap.html">Site Map</a> | <a href='#' onClick="javascript:window.external.AddFavorite('http://localhost','T-XU Articles');">Bookmark this site</a> | <a href="/plus/rssmap.html">T-XU RSS</a> <hr style="height: 1px"/> Copyright 2007 <a href='http://www.t-xu.com' target='_blank'>T-XU.com</a> - All Rights Reserved Worldwide. </td> <td background="/templets/img/31bg5.gif" align="right" valign="top"><img src='/templets/img/31c6.gif' width='4' height='49'></td> </tr> </table> <table width="900" border="0" align="center" cellpadding="0" cellspacing="0" class="tbspan"> <tr><td height="6"></td></tr> </table> </center> </body> </html>