This tutorial aims to give you a hands-on introduction to using basic CSS selectors. CSS selectors are the engine behind CSS - they determine how styles are applied to elements on a webpage.
By the end of this tutorial, you will have understood how to use type, class, and ID selectors in CSS, with the ability to target and style webpage elements using these foundational selectors.
Basic knowledge of HTML and CSS is advised, but not strictly necessary.
CSS selectors are used to "find" (or select) HTML elements based on their element name, id, class, attribute, and more.
The three basic selectors in CSS are:
Type Selectors: These target elements by their node name.
Class Selectors: These target elements by the class attribute. They're defined with a '.' followed by the class name.
ID Selectors: These target elements by the id attribute. They're defined with a '#' followed by the id name.
/* This will style all the <h1> elements */
h1 {
color: red;
}
/* This will style any element with class="intro" */
.intro {
font-size: 20px;
}
/* This will style the element with id="first" */
#first {
color: blue;
}
We've covered the basics of CSS selectors - the type, class, and id selectors. Remember:
For further learning, consider studying more advanced selectors like attribute selectors, pseudo-class selectors, and pseudo-element selectors.
<p>
) to have a font size of 18px..highlight
to have a yellow background.#main-title
to be bold and underlined.p {
font-size: 18px;
}
.highlight {
background-color: yellow;
}
#main-title {
font-weight: bold;
text-decoration: underline;
}
Continue practicing with different HTML elements, classes, and ids to get a better grasp of CSS selectors. Happy styling!