LILLY ROBOT |
The pseudo-classes define dynamic states of an element. We use them when we want to add special styles to an element.
We have already used pseudo-classes Under the section links.
selector:pseudo-class{property:value}
Pseudo-Class | Description | Example |
---|---|---|
:link | Styles an unvisited link that has not been visited by the user. | a:link{color:red} |
:visited | Styles a visited link once it has been visited by the user. | a:visited{color:green} |
:hover | Styles an element when the user moves the cursor over the link. | a:hover{color:blue} |
:active | Styles an element when it is activated by the user. | a:active{color:black} |
:focus | Styles an element with focus, as soon as the user uses an input device e.g. keyboard to input the data. | input:focus{background-color:silver} |
:first-child | Styles an element that is the first child of some other element. | div > p:first-child{color:red} |
:last-child | Styles an element that is the last child of some other element. | div > p:last-child{color:blue} |
:lang | Styles an element that matches the language attribute. | a:lang(fr){color:red} |
In the example below we have created a red link for an unvisted web page. If you place the cursor on the link the colour changes to green.
If you press the mouse down on the link the colour changes to black. If you click the link it directs you to the
top of the page and the link colour changes to blue./p>
In the example below, if you click the text box the background colour changes to black and the text colour to white.
Displays
Name:
In the example below, we have several paragraphs but only the first child (first paragraph) will be in red because it is the first child of the div element.
Displays
This is paragraph one.
This is paragraph two.
This is paragraph three.
This is paragraph four.
This is paragraph five.
This is paragraph six.
In the example below, we have 3 paragraphs but only the last child (last paragraph) background colour will be in yellow because it is the last child of the div element.
Displays
This is paragraph one.
This is paragraph two.
This is paragraph three.
This is paragraph one.
This is paragraph two.
This is paragraph three.
In the example below we have added a green background colour and a white font color to the element with the language attribute french.
The pseudo-elements are used when you want to add special styles to specified parts of an element.
selector:pseudo-element{property:value}
Pseudo-Element | Description | Example |
---|---|---|
::first line | An element with more than one line of text has the first line styled. | p::first-line{color:blue} |
::first-letter | Styles the first letter of the text in an element. | p::first-letter{font-size:34px} |
::before | Adds a content before an element. | p::before{content:url(images/circ.jpg)} |
::after | Adds a content after an element. | p::after{content:url(images/circ.jpg)} |
In the example below, we have a paragraph with 2 lines but only the first line is styled with the colour blue.
Displays
The first line is in blue and
the second line in black.
In the example below, the first letter of the paragraph has been styled with a font size of 34px.
Displays
First letter font-size is 34px.
The ::before pseudo element allows you to place a content before an element. Have a look at the example below to help you understand.
Displays
An image of circles is before this paragraph.
An open quote is before this paragraph.
The ::after pseudo element allows you to place a content after an element. Have a look at the example below to help you understand.
Displays
An image of circles is after this paragraph.
A close quote is after this paragraph.