Cascading Style Sheets CSS - I The Basics
Mar2Written by:
2009/03/02 08:17 AM
Cascading Style Sheets (CSS) is a simple mechanism for adding style. It is a stylesheet language used to describe the presentation (that is, the look and formatting) of a document written in a markup language. Today we are looking at the basics of CSS.
Cascading Style Sheets (CSS) is a simple mechanism for adding style. It is a stylesheet language used to describe the presentation (that is, the look and formatting) of a document written in a markup language. Today we are looking at the basics of CSS.
CSS is designed primarily to enable the separation of document content (written in HTML or a similar mark-up language) from document presentation, including elements such as the colours, fonts, and layout.
There are various ways of linking these style rules to your HTML documents, but the simplest method for starting out is to use HTML's STYLE element. This element is placed in the document HEAD, or by using the Style property of an individual tag, e.g. h1. The style element contains the style rules for the page. But the most common way of using a Style element is by putting it into a external Style sheet. A file with css extension. This is then linked to the HTML page.
Generally each style is made up of a selector, property and value. This is know as a style rule. The selector is normally a html element, eg Body, H1 etc. A property is some property of that element, eg font, color, size. The value specifies the value of that property, e.g., red, 12px.
By using the HTML style element you can define these styles in-line, or within the HTML file. In the header it might look like this.
Note: I had to put spaces in the opening tags for formating issues.
view source
print?
< HEAD>
< TITLE>CSS Example< /TITLE>
< STYLE TYPE="text/css">
H1 { font-size: x-large; color: red }
H2 { font-size: large; color: blue }
< /STYLE>
< /HEAD>
Putting the style in an external style sheet follows the exact same syntax as above, but without the HTML elements.
view source
print?
H1 { font-size:x-large; color: red }
But the style could also be in-line to the tag. For example we might want to change the look of the H1 style. h1 style="font-size: x-large; color: red"
An external style sheet may be linked to an HTML document through HTML's <strong class=" html="">LINK element. External style sheets do not contain any HTML elements such as or . The tag is placed in the document HEAD. The optional TYPE attribute is used to specify a media type--text/css for a Cascading Style Sheet--allowing browsers to ignore style sheet types that they do not support. Configuring the server to send text/css as the Content-type for CSS files is also a good idea.:
view source
print?
<link href="skin.css" rel="stylesheet" type="text/css">
A style sheet may be imported with CSS's @import statement. This statement may be used in a CSS file or inside the STYLE element:
view source
print?
<style type="text/css" media="screen, projection">
<!--
@import url(http://www.integralwebsolutions.co.za/style.css);
@import url(/stylesheets/hunk.css);
dt { background: yellow; color: black }
-->
</style>
The important thing to remember, and something that will confuse many and getting running around in circles, is that style sheets are cascading. The load from the top down and the last style stipulated will be the style used. Also each style can build upon the previous one, but if you overwrite a previous property then the last one is used.
So the process generally is as follows. Your External style sheet is loaded, then any imported style sheets are loaded, then the Embedded style from the element is loaded, and finally in tag in-line styles are loaded. This then will be the final presentation of your page style.
Related Reading:
Cascading Style Sheets (CSS) II - Selectors
Cascading Style Sheets - Selectors III
Cascading Style Sheets (CSS) - III - More Selectors
Cascading Style Sheets – Selectors IV – Pseudo-Classes.
Cascading Style Sheets - Pseudo Elements
blog comments powered by