- The CSS syntax is made up of three parts: a selector, a property and a value:
selector {property: value} - If the value is multiple words, put quotes around the value:
p {font-family: "sans serif"} - If you wish to specify more than one property, you must separate each property with a semicolon. The example below shows how to define a center aligned paragraph, with a red text colour:
p {text-align:center;color:red} - To make the style definitions more readable, you can describe one property on each line, like this:
p
{
text-align: center;
color: black;
font-family: arial
} - You can group selectors. Separate each selector with a comma. In the example below we have grouped all the header elements. All header elements will be displayed in green text colour:
h1,h2,h3,h4,h5,h6
{
color: green
} - Comments are used to explain your code, and may help you when you edit the source code at a later date. A comment will be ignored by browsers. A CSS comment begins with "/*", and ends with "*/", like this:
/* This is a comment */
p
{
text-align: center;
/* This is another comment */
color: black;
font-family: arial
}
Selectors
- Type selector
- Class selector
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:p.right {text-align: right}
p.center {text-align: center}
To apply more than one class per given element, the syntax is:<p class="right">
This paragraph will be right-aligned.
</p>
<p class="center">
This paragraph will be center-aligned.
</p>
The paragraph above will be styled by the class "center" and the class "bold".<p class="center bold">
This is a paragraph.
</p>
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:.center {text-align: center}<h1 class="center">
This heading will be center-aligned
</h1>
<p class="center">
This paragraph will also be center-aligned.
</p> - ID selector
You can also define styles for HTML elements with the ID selector. The ID selector is defined as a #.
The style rule below will match the element that has an id attribute with a value of "green":
The style rule below will match the p element that has an id with a value of "para1":#green {color: green}p#para1
{
text-align: center;
color: red
} - Descendant selector
The rule below will only select <em> elements that are descendants of <p> elements. If this rule is applied, the <em> element within the <h1> will not be coloured blue.
p em {color: blue; } - Child selector
Using the following rule you can target any <em> element that is a child of the <div>. Other <em> elements that are descendants but not direct children of the <div> will not be targeted.
ordiv > em { color: blue; }div>em { color: blue; } - Universal selector
Universal selectors are used to select any element. For example, the rule below will color all HTML elements on a page blue - regardless of their positions in the document tree.
* {color: blue; } - Adjacent sibling selector
Using the following rule, you can target any <h3> that follows an <h2>:
h2 + h3 {margin: -1em; } - Attributes selector
You can also apply styles to HTML elements with particular attributes.
The style rule below will match all input elements that have a type attribute with a value of "text":input[type="text"] {background-color: blue}
- The priority order is:
- Inline style (inside an HTML element)
- Internal style sheet (inside the <head> tag)
- External style sheet
- Browser default
positionrelative: the computed position value is relative. Thetopandbottomproperties specify the vertical offset from its normal position; theleftandrightproperties specify the horizontal offset.absolute/fixed:the computed position value is absolute or fixed. Thetop,right,bottom, andleftproperties specify offsets from the edges of the element's containing block. (The containing block is the ancestor relative to which the element is positioned.) If the element has margins, they are added to the offset.sticky: the computed position value is sticky. It's treated as relatively positioned until its containing block crosses a specified threshold (such as settingtopto value other thanauto) within its flow root (or the container it scrolls within), at which point it is treated as "stuck" until meeting the opposite edge of its containing block.