CSS SyntaxSyntaxThe CSS syntax is made up of three parts: a selector, a property and a value:
The selector is normally the HTML element/tag you wish to define, the property is the attribute you wish to change, and each property can take a value. The property and value are separated by a colon and surrounded by curly braces:
If the value is multiple words, put quotes around the value:
Note: If you wish to specify more than one property, you must separate each property with a semi-colon. The example below shows how to define a center aligned paragraph, with a red text color:
To make the style definitions more readable, you can describe one property on each line, like this:
GroupingYou can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. Each header element will be green:
The class SelectorWith the class selector you can define different styles for the same type of HTML element. Say that you would like to have two types of paragraphs in your document: one right-aligned paragraph, and one center-aligned paragraph. Here is how you can do it with styles:
You have to use the class attribute in your HTML document:
Note: Only one class attribute can be specified per HTML element! The example below is wrong:
You can also omit the tag name in the selector to define a style that will be used by all HTML elements that have a certain class. In the example below, all HTML elements with class="center" will be center-aligned:
In the code below both the h1 element and the p element have class="center". This means that both elements will follow the rules in the ".center" selector:
The id SelectorThe id selector is different from the class selector! While a class selector may apply to SEVERAL elements on a page, an id selector always applies to only ONE element. An ID attribute must be unique within the document. The style rule below will match a p element that has the id value "para1":
The style rule below will match the first element that has the id value "wer345":
The rule above will match this h1 element:
The style rule below will match a p element that has the id value "wer345":
The rule above will not match this h2 element:
CSS CommentsYou can insert comments in CSS to explain your code, which can help you when you edit the source code at a later date. A comment will be ignored by the browser. A CSS comment begins with "/*", and ends with "*/", like this:
|
CSS How To...ExamplesHow to Insert a Style SheetWhen a browser reads a style sheet, it will format the document according to it. There are three ways of inserting a style sheet: External Style SheetAn external style sheet is ideal when the style is applied to many pages. With an external style sheet, you can change the look of an entire Web site by changing one file. Each page must link to the style sheet using the <link> tag. The <link> tag goes inside the head section:
The browser will read the style definitions from the file mystyle.css, and format the document according to it. An external style sheet can be written in any text editor. The file should not contain any html tags. Your style sheet should be saved with a .css extension. An example of a style sheet file is shown below:
Internal Style SheetAn internal style sheet should be used when a single document has a unique style. You define internal styles in the head section by using the <style> tag, like this:
The browser will now read the style definitions, and format the document according to it. Note: A browser normally ignores unknown tags. This means that an old browser that does not support styles, will ignore the <style> tag, but the content of the <style> tag will be displayed on the page. It is possible to prevent an old browser from displaying the content by hiding it in the HTML comment element:
Inline StylesAn inline style loses many of the advantages of style sheets by mixing content with presentation. Use this method sparingly, such as when a style is to be applied to a single occurrence of an element. To use inline styles you use the style attribute in the relevant tag. The style attribute can contain any CSS property. The example shows how to change the color and the left margin of a paragraph:
Multiple Style SheetsIf some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet. For example, an external style sheet has these properties for the h3 selector:
And an internal style sheet has these properties for the h3 selector:
If the page with the internal style sheet also links to the external style sheet the properties for h3 will be:
The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by the internal style sheet. |
CSS Background PropertiesCSS Background properties define the background effects of an element. ExamplesSet the background color Set an image as the background How to repeat a background image How to place the background image How to set a fixed background image All the background properties in one
declaration CSS BackgroundThe Background properties allow you to control the background color of an element, set an image as the background, repeat a background image vertically or horizontally, and position an image on a page. Background Properties:NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS Text PropertiesCSS Text properties define the appearance of text. ExamplesSet the color of the text Set the background-color of the text Specify the space between characters Align the text Decorate the text Indent text Control the letters in a text CSS TextText properties allow you to control the appearance of text. It is possible to change the color of a text, increase or decrease the space between characters in a text, align a text, decorate a text, indent the first line in a text, and more. Text Properties:NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS Font PropertiesCSS Font properties define the font in text. ExamplesSet the font of a text Set the size of the font Set the style of the font Set the variant of the font Set the boldness of the font All the font properties in one declaration CSS FontsThe Font properties allow you to change the font family, boldness, size, and the style of a text. Notes - Useful TipsFonts are identified by their name in CSS1. Note that if a browser does not support the font that is specified, it will use a default font. Font Properties:NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS Border PropertiesCSS Border properties define the borders around an element. ExamplesSet the style of the four borders Set different borders on each side Set the color of the four borders Set the width of the bottom border Set the width of the left border Set the width of the right border Set the width of the top border All the bottom border properties in one
declaration All the left border properties in one
declaration All the right border properties in one
declaration All the top border properties in one
declaration All the width of the border properties in one
declaration All the border properties in one
declaration CSS BordersThe Border properties allow you to specify the style, color, and width of an element's border. In HTML we use tables to create borders around a text, but with the CSS Border properties we can create borders with nice effects, and it can be applied to any element. Border Properties:NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS Margin PropertiesCSS Margin properties define the space around elements. ExamplesSet the left margin of a text Set the right margin of a text Set the top margin of a text Set the bottom margin of a text All the margin properties in one
declaration CSS MarginsThe Margin properties define the space around elements. It is possible to use negative values to overlap content. The top, right, bottom, and left margin can be changed independently using separate properties. A shorthand margin property can also be used to change all of the margins at once. Browser Note: Netscape and IE give the body tag a default margin of 8px. Opera does not! Instead, Opera applies a default padding of 8px, so if one wants to adjust the margin for an entire page and have it display correctly in Opera, the body padding must be set as well! Margin Properties:NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS Padding PropertiesCSS Padding properties define the space between the element border and the element content. ExamplesSet the left padding Set the right padding Set the top padding Set the bottom padding All the padding properties in one
declaration CSS PaddingThe Padding properties define the space between the element border and the element content. Negative values are not allowed. The top, right, bottom, and left padding can be changed independently using separate properties. A shorthand padding property is also created to control multiple sides at once. Padding Properties:NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS List PropertiesThe List properties allow you to change between different list-item markers, set an image as a list-item marker, and set where to place a list-item marker. ExamplesThe different list-item markers in unordered
lists The different list-item markers in ordered
lists Set an image as the list-item marker Place the list-item marker All list properties in one declaration List Properties:NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS Dimension PropertiesThe Dimension properties allow you to control the height and width of an element. It also allows you to increase the space between two lines. ExamplesSet the height and width of an image Increase the space between lines Dimension Properties:NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS Classification PropertiesThe Classification properties allow you to control how to display an element, set where an image will appear in another element, position an element relative to its normal position, position an element using an absolute value, and how to control the visibility of an element. ExamplesHow to display an element A simple use of the float property An image with border and margins that floats to the
right in a paragraph An image with a caption that floats to the
right Let the first letter of a paragraph float to the
left Creating a horizontal menu Creating a homepage without tables Position:relative Position:absolute How to make an element invisible Change the cursor Classification Properties:NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS Positioning PropertiesCSS Positioning properties define the position of an element. ExamplesSet the shape of an element Set the left and top position of an
element Set the right and bottom position of an
element Overflow Vertical align an image Z-index Z-index CSS PositioningThe Positioning properties allow you to specify the left, right, top, and bottom position of an element. It also allows you to set the shape of an element, place an element behind another, and to specify what should happen when an element's content is too big to fit in a specified area. Positioning Properties:NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS Pseudo-classesPseudo-classes are used in CSS to add different effects to some selectors, or to a part of some selectors. ExamplesHyperlink Hyperlink 2 SyntaxThe syntax of pseudo-classes:
CSS classes can also be used with pseudo-classes:
Anchor Pseudo-classesA link that is active, visited, unvisited, or when you mouse over a link can all be displayed in different ways in a CSS-supporting browser:
Note: a:hover MUST come after a:link and a:visited in the CSS definition in order to be effective!! Note: a:active MUST come after a:hover in the CSS definition in order to be effective!! Note: Pseudo-class names are not case-sensitive. Note: IE 4 and higher supports the anchor pseudo-class. NN 4.5 and Netscape 6 support the anchor pseudo-class only partially. Pseudo-classes and CSS ClassesPseudo-classes can be combined with CSS classes:
If the link in the example above has been visited, it will be displayed in red. CSS2 - The :first-child Pseudo-classThe :first-child pseudo-class matches a specified element that is the first child of another element.
CSS2 - The :lang Pseudo-classThe :lang pseudo-class allows the author to specify a language to use in a document or to use in a specified element. In the example below, the rule sets the type of quotation marks for an HTML document that is in Norwegian:
In the next example, the rule sets the type of quotation marks for blockquote elements:
Pseudo-classesNN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS Pseudo-elementsPseudo-elements are used in CSS to add different effects to some selectors, or to a part of some selectors. ExamplesMake the first letter special Make the first line special SyntaxThe syntax of pseudo-elements:
CSS classes can also be used with pseudo-elements:
The :first-line Pseudo-elementThe "first-line" pseudo-element is used to add special styles to the first line of the text in a selector:
The output could be something like this:
In the example above the browser displays the first line formatted according to the "first-line" pseudo element. Where the browser breaks the line depends on the size of the browser window. Note: The "first-line" pseudo-element can only be used with block-level elements. Note: The following properties apply to the "first-line" pseudo-element:
Note: IE 4.01 and IE 5.0 do not support the "first-line" pseudo-element, but IE 5.5 does. Note: NN 4.5 does not support the "first-line" pseudo-element. The :first-letter Pseudo-elementThe "first-letter" pseudo-element is used to add special style to the first letter of the text in a selector:
The output could be something like this:
Note: The "first-letter" pseudo-element can only be used with block-level elements. Note: The following properties apply to the "first-letter" pseudo- element:
Note: IE 4.01 and IE 5.0 do not support the "first-letter" pseudo-element, but IE 5.5 does. Note: NN 4.5 does not support the "first-letter" pseudo-element. Pseudo-elements and CSS ClassesPseudo-elements can be combined with CSS classes:
The example above will make the first letter of all paragraphs with class="article" red. Multiple Pseudo-elementsSeveral pseudo-elements can be combined:
The output could be something like this:
In the example above the first letter of the paragraph will be red with a font size of 24pt. The rest of the first line would be blue while the rest of the paragraph would be the default color. CSS2 - The :before Pseudo-elementThe ":before" pseudo-element can be used to insert some content before an element. The style below will play a sound before each occurrence of a header one element.
CSS2 - The :after Pseudo-elementThe ":after" pseudo-element can be used to insert some content after an element. The style below will play a sound after each occurrence of a header one element.
Pseudo-elementsNN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS2 Media TypesMedia Types allow you to specify how documents will be presented in different media. The document can be displayed differently on the screen, on the paper, with an aural browser, etc. Media TypesSome CSS properties are only designed for a certain media. For example the "voice-family" property is designed for aural user agents. Some other properties can be used for different media types. For example, the "font-size" property can be used for both screen and print media, but perhaps with different values. A document usually needs a larger font-size on a screen than on paper, and sans-serif fonts are easier to read on the screen, while serif fonts are easier to read on paper. The @media RuleThe @media rule allows different style rules for different media in the same style sheet. The style in the example below tells the browser to display a 14 pixels Verdana font on the screen. But if the page is printed, it will be in a 10 pixels Times font. Notice that the font-weight is set to bold, both on screen and on paper:
See it yourself ! If you use IE 5.0 (or higher) and print this page, you will see that the paragraph under "Media Types" will be displayed in another font, and have a smaller font size than the rest of the text. Different Media TypesNote: The media type names are not case-sensitive.
|
CSS2 ReferenceThe links in the "Property" column point to more useful information about the specific property. Browser support: NN: Netscape, IE: Internet Explorer, W3C: Web Standard Background
Border
Classification
Dimension
Font
Generated Content
List and Marker
Margin
Outlines
Padding
Positioning
Table
Text
|
CSS2 Print ReferencePrint PropertiesPrinting HTML documents has always been problematic. In CSS2 the print properties are added to make it easier to print from the Web. The links in the "Property" column point to more useful information about the specific property. Browser support: NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS2 Aural ReferenceAural Style SheetsAural style sheets use a combination of speech synthesis and sound effects to make the user listen to information, instead of reading information. Aural presentation can be used:
The aural presentation converts the document to plain text and feed this to a screen reader (a program that reads all the characters on the screen). An example of an Aural style sheet:
The example above will make the speech synthesizer play a sound, then speak the headers in a very rich male voice. CSS2 Aural ReferenceThe links in the "Property" column point to more useful information about the specific property. Browser support: NN: Netscape, IE: Internet Explorer, W3C: Web Standard
|
CSS UnitsMeasurements
Colors
|