| Free HTML Tutorials • CSS • Popups tutorial • Frames Tutorials • Forms Tutorials • Beginners HTML Tutorials • Advanced HTML Tutorials • JavaScripts • Free Web Page Tutorials • Free Downloads |
|
Advanced Section |
|
CASCADING STYLE SHEETS |
|
|
|
|
External Style Sheets
As I said before, the great advantage of CSS is that allows you to set the style of your web site in one place. By using a style sheet you can change various elements on all your pages, without having to go painstakingly over each page separately. Another advantage is that the CSS is loaded with the first document, cached by the browser and referred to subsequently, making pages load faster. First you use a META tag to tell the browser you are using CSS in the document: <META http-equiv="Content-Style-Type" content="text/css">. When you create your style sheet, it will be just a text file saved with a .css extension.You will also need to refer or link to this style sheet with this line also in the HEAD section: <LINK rel="stylesheet" type="text/css" href="extras/style.css"> It doesn't matter what you call it or which folder it is in, as long as the link points to it and it is saved with the .css extension. OK, now we've got that out of the way, let's look at an actual style sheet.
Looks a bit daunting at first glance, but let's take a closer look. First, note the BODY code. What does this mean? It means that when you start your page all you need for the <BODY> tag is <BODY>! Your background colour is pre-set as are font sizes and colours. Next, all your links are taken care of. No need to add fonts, sizes or anything. If you use tables the same applies - the font, etc are matched to the body font. You are not limited to the standard headings (Hx) either; they can be customised to suit. Now take a look at the last five items. Note the dot in front of each. These are Classes. To call them in your page you just add the "class" to an HTML tag like this:
In the example above I have used SPAN and DIV tags which are called "class selectors". You can also use <B> and <I> or <P>. SPAN and DIV don't actually do anything except act as carriers so are useful in this context. One point to note, with <DIV>, which is a block tag, line breaks are automatically inserted (like <P> or <BR> tags), but with <SPAN>, no line breaks are inserted before or after. Note the use of the <H1> tag. Usually this would be much larger but I have given it a rule in the External Style Sheet which says it must be this size and red. In fact it is now much smaller than the H2 & H3 tags. In the example External CSS above, you will also see ul {list-style:square} so all your lists will have a square instead of a black disc. The Cascade For those of you who have been wondering why they are called "Cascading Style Sheets", here is a brief explanation. The cascade refers to the order in which the browser handles the style code. If you have an External Style Sheet specifying that all the <TH> tags will be in blue text, and an Internal Style Sheet saying it should be black, which takes precedence? Style sheets are designed to cascade, which means that they have inbuilt priorities for the rules they follow. In a nutshell, the cascade rules state that the specific will always override the general. So we have this:
This tutorial page is using an Internal Style Sheet, which sets the overall look - the list shown above overrides the style sheet it by using HTML tags to change the colour of the text. Of course, I could have set up a rule that says <OL> tags will be this colour. In that case all <OL> tags would be the colour I specified for them. These examples have barely scratched the surface of what can be done with style sheets. I am currently working on a separate CSS tutorial, but in the meantime if you have any questions or would like more information, just email me using the HELP link. Also check the Links on the Tips page for tutorial references if you would like to learn more. On the next two pages is some important information you need to know to "future proof" your web pages Advanced Index
Help
|