HTML/XHTML:CSS

Introduction
Styles are an important technique used to separate content and formatting (style). Styles can be added directly to a tag, inserted in a section at the top of the web page or included in a linked file.

Embedded into a tag
A style can also be embedded into the actual tag, for example:

Will display the paragraph in green

Paragraph one in green

Span tag
Span is a generic inline container, and allows a style to be attached to specific words in text. Attributes allowed are id (replaces name from html 3.x), class, title, style, lang.

In this example the style changes the colour of the word world to red. The title tag gives a tool tip when the mouse hovers over the word world.

Hello world

This is better handled by using an embeded style separated from the tag.

Note that the .tooltip can be assigned to all tags

Embedded style sheet
The style sheet is placed between the and tags placed after the tag.

This style sheet defines the pages background and colour (body), as well as bold (b) and heading 1 (h1).

Linked Style sheet
This example assumes that page.css is kept in the css directory.

Syntax
The 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, each property is separated with a semi-colon. The example below shows how to define a center aligned paragraph, with a red text color:

Grouping

 * You 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:

Contextual selectors
Contextual selectors are strings of two or more simple selectors separated by a space. These selectors can be assigned normal properties and, due to the rules of cascading order, they will take precedence over simple selectors. For example, the contextual selector in:

is p strong. This rule says that strongly emphasized text within a paragraph should have a yellow background; strong text in a heading would be unaffected.

Classes

 * With the class selector you can define different styles for the same type of HTML element.


 * 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:

FAQ Example
If you want to simplify the coding on an FAQ page and change the formatting depending on whether you are displaying a question or answer you could use a style definition as follows:

ID selector #
The ID selector only applies to ONE element on a page.

For example

Will match

But not

Or

will match the first element with the id value of "green"

Class and ID selector differences

 * # is an id selector, used to target a single specific element with a unique id, but
 * . is a class selector used to target multiple elements with a particular class.


 * #foo {} will style the single element declared with an attribute id="foo"
 * .foo {} will style all elements with an attribute class="foo" (you can have multiple classes assigned to an element too, just separate them with spaces, e.g. class="foo bar")

In CSS what is the difference between “.” and “#” when declaring a set of styles? (2011)

Internal Priorities

 * 1) element
 * 2) .class
 * 3) #id

Comments in CSS

 * You 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:

Sample css file linked to an html page
This is an example called neon.css (from Frontpage)

To include this in an html file