Write a comment on this article
HTML alone offers limited possibilities to format and layout a page. Cascading Style Sheets (CSS) help you to do that with a simple mechanism. It defines how HTML elements are to be displayed, e.g. the color, the font-size, the background-image, and so on.
There are three ways to define styles for a document:
Of course you can use more than one method to define the style. You can even define multiple styles for the same element, but be aware of how the styles are overwritten:
Syntax
Here is the basic CSS syntax consisting of a selector and one or more property-value-pairs:
selector {
property:value;
}
The following example sets the text color of all paragraph elements to red and underlines the text:
p {
color:red;
text-decoration:underline;
}
Grouping
It is possible to group several selectors to save lines of code. You can do this by delimitting the selectors with a colon. Here is an example which sets the text color of all paragraph and anchor elements:
p,a {
color:red;
}
Selectors
The following table gives an overview of the the available selectors you can use:
| Name | Description | Example |
| Universal selector: * | The universal selector applies to any element. | * { All elements are selected, like <p> and <a>: <p>Some text here.</p> |
| Type selector | You can use the tag name to select all tags with the given name. | p { All <p> and <a> elements are selected: <p>Some text here.</p> |
| Descendant selector | This selector selects all elements that are inside the first selected elements. | p a { All <a> elements which are inside a <p> element are selected: <p>Some text here. <a href="#">First link</a> <a href="#">Second link</a></p> |
| Child selector: > | This selector selects all elements that are a direct child of the first selected elements. | p>a { All <a> elements which are a direct child of a <p> element are selected: <p>Some text here. <a href="#">First link</a> <a href="#">Second link</a></p> |
| Pseudo-class selector: : |
Pseudo-classes allow the formatting based on information that lies outside the document tree. e.g. the first line of a paragraph or the mouseover-handling. The following pseudo-classes are available:
first-child (the first child of its parent) |
a:hover { All <a> elements turn into red color when the user moves the mouse over them: <p>Some text here. <a href="#">First link</a> <a href="#">Second link</a></p> |
| Class selector: . | With this selector you can define different classes which can be used in different HTML elements by assigning the class attribute. If you leave out the tag-name you can use this class at any element. You can even use more than one class (delimit classes with a space). | p.myFirstClass { The definitions above could be used like this: <p class="myFirstClass">Some text here.</p> |
| Id selector: # | The id selector is used for elements with an id attribute, so it is more specific. If you leave out the tag-name you can use this id at any element. The id must not start with a number! | p#myFirstId { The definitions above could be used like this: <p id="myFirstId">Some text here.</p> |
| Attribute selector: [] |
This selector allows you to select elements with specific attributes. The following possibilities are available:
[attribute] (the attribute is set) |
input { The definitions above could be used like this: <input type="text" /> |
CSS, introduction, syntax, grouping, selector
If you have a question or a problem and you're looking for a solution, please use the forum. Comments might not be answered.