In CSS, selectors are used to select the element(s) you want to style.
There are numerous selector patterns and combinations that you can make of which some of the basic ones are explained below:
The .class
selector selects and styles all elements within the class that is specified.
.class-name {/* CSS code written here */}
In the example below, the background color of all elements in the .main
class changes to coral as specified in the CSS file.
The type
selector selects and styles all elements with the specified element name.
element {/* CSS code written here */}
In the example below, the font color of all the <p>
elements changes to red and all of them are aligned in the center.
The #id
selector selects and styles the element with the specified id.
#idname {/* CSS code written here */}
In the example below, one of the <p>
element changes its background color and the other changes its font color to blue as styled under their specific ids.
The Universal *
selector selects all the elements and styles them. It can also select all elements inside another element. The syntax and example is as follows:
/* Selects all the elements */* {/* CSS code written here*/}
In the example below, the background color of all elements changes to yellow and the background color of all elements inside the <div>
element changes to coral.