What is a selector?
A CSS selector selects the HTML element(s) for styling purpose. We can divide CSS selectors into five categories
Selector Categories
- Simple
- Combinator
- Pseudo-class
- Pseudo-element
- Attribute
Simple Selectors
Select elements based on element, id, class
Element selector
This group includes selectors that target an HTML element such as an <h1>
_3h1 {_3 color: red;_3}
Universal selector
Selects all elements
_3* {_3 color: green;_3}
Universal selectors can be namespaced when using @namespace
This is useful when dealing with documents containing multiple namespaces such as HTML with inline SVG or MathML, or XML that mixes multiple vocabularies
ns|*
- matches all elements in namespace ns*|*
- matches all elements|*
- matches all elements without any declared namespace
Class selector
- The .class selector selects elements with a specific class attribute
- To select elements with a specific class, write a period
.
character, followed by the name of the class - To select and style all elements with
class="super"
_3.super {_3 background-color: yellow;_3}
ID selector
- The id selector uses the id attribute of an HTML element to select a specific element.
- The id of an element is unique within a page, so the id selector is used to select one unique element!
- To select an element with a specific id, write a hash
#
character, followed by theid
of the element - The CSS rule below will be applied to the HTML element with
id="para1"
_4#para1 {_4 text-align: center;_4 color: red;_4}
Grouping Selector
- The grouping selector selects all the HTML elements with the same style definitions
- Look at the following CSS code (
h1
,h2
, andp
elements have the same style definitions)
_14h1 {_14 text-align: center;_14 color: red;_14}_14_14h2 {_14 text-align: center;_14 color: red;_14}_14_14p {_14 text-align: center;_14 color: red;_14}
- It would be better to group the selectors, to minimize the code
_6h1,_6h2,_6p {_6 text-align: center;_6 color: red;_6}
Combinator Selectors
A combinator is something that explains the relationship between the selectors
- Descendant selector
(space)
- Child selector
>
- Adjacent sibling selector
+
- General sibling selector
~
Descendant selector (space)
- The descendant selector matches all elements that are descendants of a specified element
- Selects all
<p>
elements inside<div>
elements
_3div p {_3 background-color: yellow;_3}
Child selector >
- The child selector selects all elements that are the children of a specified element
- Selects all
<p>
elements that are children of a<div>
element
_3div > p {_3 background-color: red;_3}
Adjacent Sibling Selector +
- The adjacent sibling selector is used to select an element that is directly after another specific element
- Sibling elements must have the same parent element, and adjacent means immediately following
- Selects the first
<p>
element that are placed immediately after<div>
elements
_3div + p {_3 background-color: yellow;_3}
General Sibling Selector ~
- The general sibling selector selects all elements that are next siblings of a specified element
- Selects all
<p>
elements that are next siblings of<div>
elements
_4div ~ p {_4 back_4 ground-color: yellow;_4}
Pseudo-Classes Selector
Select elements based on a certain state of the element
- A pseudo-class is used to define a special state of an element
- Style an element when a user mouses over it
- Style visited and unvisited links differently
- Style an element when it gets focus
General Syntax
_3selector:pseudo-class {_3 property: value;_3}
Anchor Pseudo-classes
_19/* unvisited link */_19a:link {_19 color: #ff0000;_19}_19_19/* visited link */_19a:visited {_19 color: #00ff00;_19}_19_19/* mouse over link */_19a:hover {_19 color: #ff00ff;_19}_19_19/* selected link */_19a:active {_19 color: #0000ff;_19}
Pseudo-classes and HTML Classes
- Pseudo-classes can be combined with HTML classes
- When you hover over the link with
highlight
class, it will change color
_3a.highlight:hover {_3 color: #ff0000;_3}
Hover on <div>
An example of using the :hover
pseudo-class on a <div>
element
_4div:hover {_4 backg_4 round-color: blue;_4}
Simple Tooltip Hover
- Hover over a
<div>
element to show a<p>
element (like a tooltip) - Hover over me to show the
<p>
element
_9p {_9 display: none;_9 background-color: yellow;_9 padding: 20px;_9}_9_9div:hover p {_9 display: block;_9}
:first-child
- The
:first-child
pseudo-class matches a specified element that is the first child of another element - Match the first
<p>
element. Below, the selector matches any<p>
element that is the first child of any element
_3p:first-child {_3 color: blue;_3}
Applies the styles to <i>
, only if <i>
is first-child in any <p>
element
_3p i:first-child {_3 color: blue;_3}
Applies the styles to all <i>
descendants of <p>
where <p>
is the first-child
_3p:first-child i {_3 color: blue;_3}
:lang
Pseudo-class
- The
:lang
pseudo-class allows you to define special rules for different languages - In the example below,
:lang
defines the quotation marks for<q>
elements withlang="no"
_12<html>_12 <head>_12 <style>_12 q:lang(no) {_12 quotes: "~""~";_12 }_12 </style>_12 </head>_12 <body>_12 <p>Some text <q lang="no">A quote in a paragraph</q> Some text.</p>_12 </body>_12</html>
:focus
When the element is focused the style will be applied.
_13<!DOCTYPE html>_13<html>_13 <head>_13 <style>_13 input:focus {_13 background-color: yellow;_13 }_13 </style>_13 </head>_13 <body>_13 <input type="text" name="username" placeholder="Username" />_13 </body>_13</html>
Pseudo-Element Selectors
Selects and style a part of an element. Usually added for decoration purposes
What are Pseudo-Element ?
- A CSS pseudo-element is used to style specified parts of an element
- pseudo refers to fake. Hence they are fake elements int the page
- Style the first letter, or line, of an element
- Insert content before, or after, the content of an element
General Syntax
_3selector::pseudo-element {_3 property: value;_3}
::first-line
- Adds a special style to the first line of a text
- The following example formats the first line of the text in all
<p>
elements
_4p::first-line {_4 color: #ff0000;_4 font-variant: small-caps;_4}
::first-letter
- Adds a special style to the first letter of a text
- The following example formats the first letter of the text in all
<p>
elements
_4p::first-letter {_4 color: #ff0000;_4 font-size: xx-large;_4}
Pseudo-elements and HTML Classes
Pseudo-elements can be combined with HTML classes
_4p.intro::first-letter {_4 color: #ff0000;_4 font-size: 200%;_4}
Multiple Pseudo-elements
- Several pseudo-elements can also be combined
- In the following example, the first letter of a paragraph will be red & in an xx-large font size
- The rest of the first line will be blue, and in small-caps
- The rest of the paragraph will be the default font size and color
_9p::first-letter {_9 color: #ff0000;_9 font-size: xx-large;_9}_9_9p::first-line {_9 color: #0000ff;_9 font-variant: small-caps;_9}
::before
- Inserts some content before the content of an element.
- The following example inserts an image before the content of each
<h1>
element
_3h1::before {_3 content: url(smiley.gif);_3}
::after
- Inserts some content after the content of an element.
- The following example inserts an image after the content of each
<h1>
element
_3h1::after {_3 content: url(smiley.gif);_3}
::marker
The ::marker
pseudo-element selects the markers of list items
_4::marker {_4 color: red;_4 font-size: 23px;_4}
::selection
- Matches the portion of an element that is selected
- The following CSS properties can be applied to
::selection
-color
,background
,cursor
, andoutline
- The selected text red on a yellow background
_4::selection {_4 color: red;_4 background: yellow;_4}
Attribute Selectors
Selects elements based on an attribute or an attribute value
- Style HTML Elements With Specific Attributes
- It is possible to style HTML elements that have specific attributes or attribute values
[attribute]
- Selects elements with a specified attribute.
- Selects all
<a>
elements with atarget
attribute
_3a[target] {_3 background-color: yellow;_3}
Value =
- Selects elements with a specified attribute and value
- Selects all
<a>
elements with atarget="_blank"
attribute
_3a[target="_blank"] {_3 background-color: yellow;_3}
Includes word ~=
- Selects elements with an attribute value containing a specified word
- Selects all elements with a title attribute that contains a space-separated list of words, one of which is "flower"
_3[title~="flower"] {_3 border: 5px solid yellow;_3}
Hyphen word |=
Selects elements with the specified attribute, whose value can be exactly the specified value, or the specified value followed by a hyphen -
_3[class|="top"] {_3 background: yellow;_3}
Starst with ^=
- Selects elements with the specified attribute, whose value starts with the specified value
- Selects all elements with a class attribute value that starts with
top
_3[class^="top"] {_3 background: yellow;_3}
Ends with $=
- Selects elements whose attribute value ends with a specified value
- Selects elements with a class attribute value that ends with
test
_3[class$="test"] {_3 background: yellow;_3}
Contains letter *=
- Selects elements whose attribute value contains a specified value
- Selects elements with a class attribute value that contains
te
_3[class*="te"] {_3 background: yellow;_3}