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 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>
This is one big H1 tag!
When you fire up your browser, you should see the text "This isone big H1 tag!" in a large, blue Verdana font face.
Let¡¯s step through the style code step by step. Firstly, we havea pretty standard HTML header. The page starts with the tag followed by the tag. Next, we use a standard tag to set the title of the page we are working with.
Notice, though, that before the tag is closed, we haveour tag.
When you add the style sheet code inline (as part of the HTMLdocument), it must be bound by tagsrespectively. Our example is working with the
tag. We arechanging three attributes of the
¡¯s style: the text color(color), the font that any
tags on the page will bedisplayed in (font-family), and lastly, the size of the font(size).
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.
--------------------------------------- An example of anexternal style sheet ---------------------------------------
External style sheets are similar to internal style sheets,however, they are stripped of the tags, andneed to be referenced from another HTML file to be used.
Create a new file called ¡°mystyle.css¡± and enter the followingcode into it:
Next, create a HTML file and name it external.html. Enter thefollowing code into external.html:
External Style Sheet Reference Example
This is one big H1 tag!
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 tag in the 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.
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!