CSS
In this tutorial we will learn about CSS syntax.
All CSS style rule starts with a
In the following example we have a CSS rule to set the color of paragraph element to red.
p {
color : red;
}
In the following example we are setting the font-size and color of h1 tag.
h1 {
font-size : 24px;
color : lightblue;
}
We use the forward slash and asterisk (/*) and asterisk and backward slash (*/) to create comment.
/* Setting color of p to red */
p {
color : red;
}
/* setting color of h1
to blue
*/
h1 {
color : blue;
}
The selectors tells us about the element(s) that will be affected by the CSS rule.
Selectors can be an element name, id and class.
For example, in the following example we have defined CSS rule for h1 element. So, the selector is h1.
h1 {
color : #d3d3d3;
}
In this we use the element name like p, h1, span etc.
Example:
/**
* color for all the paragraph
*/
p {
color : blue;
}
/* font-size for all ul */
ul {
font-size : 20px;
}
In this we use the id of the element to apply the CSS rule. We use the #
sign before the element id.
Id of an element cannot start with a number.
Following are valid id: group1, team-a, student-no-123.
Following is an invalid id: 1-number.
In the following example we are applying the style rule to an element having id mycanvas
.
#mycanvas {
color : #d1d1d1;
}
In this we use the class used for the element to apply the rule. We use the .
sign before the element class.
Class of an element cannot start with a number.
Following are valid class: fruits, blog-post and games.
Following is an invalid class: 1store.
In the following example we are applying the CSS rule to all the elements having fruits
class.
.fruits {
font-size : 20px;
}
We can combine both element name and id to apply the style rule.
In the following example we are targetting a paragraph having id team-a.
p#team-a {
font-size : 20px;
color : lightblue;
}
We can also combine element name and class of the element to apply the style.
In the following example the CSS rule will be applied to only those paragraph elements that are having class group-1.
p.group-1 {
font-size : 16px;
color : #d3d3d3;
}
It is a concept that is used by developers to tag style rule using the =
equal to symbol in the comment. It is used to search style using the Find feature in text editor and IDE. Since = symbole is not part of a CSS rule so it is used in search.
In the following example we have tagged the paragraph elements having class fruits using the = flag.
/**
* =p.fruits rules for all the paragraph elements having fruits class.
*/
p.fruits {
color : lightgreen;
font-size : 16px;
}
Now in the text editor or IDE we can use the Search feature to look for this rule by typing =p.fruits
and we will get this.
Douglas Bowman @stop introduced the idea of flags.
ADVERTISEMENT