Utilizing various CSS selectors is one of the most important skills of CSS coding. There are several CSS selectors. The most basic ones are type selector, class selector, and ID selector. In this section, we'll explain three basic CSS selectors.
This selector is the simplest selector. Use the element name (or tag name) to select elements in the HTML documents which you want to style. The elements with the same tag name will be styled in this approach. You can utilize this approach to create a standard CSS template; however, this approach is not useful for highly customized web pages as the same styles are applied to all the same-name tags.
You can select multiple elements for the same declaration. Use ",
" comma to separate selector names like shown below.
This selector gives you more flexibility in styling. Using a class selector, you can apply different styles to elements with the same tag name. For example, you can make some selected <h3>
elements colored in blue while other <h3>
elements colored in red. There are two steps to set a class sector.
Add a class attribute value in the start tag of the target HTML element.
<li class="nav-link">xxx </li>
Set CSS using the class selector you defined. For the class selector, you need to start with ".
" period.
.nav-link{
color: blue;
}
This selector is similar to the class selector but it can be used for a more specific element. You can use the same class selector multiple times in the same HTML document; however, you can use the same id selector only once in the same HTML document. This is because the id is used to locate a unique element in the HTML document.
There are two steps to set an ID sector.
Similarly to the class attribute, add an id
to the target HTML element.
<p id="highlight">xxx </p>
Set CSS using the id selector you defined. For ID selector, you need to start with "#
" hash.
#highlight{
color: red;
}
You can assign multiple class
attributes or a class
and id
together to one HTML element as in the example below.
<div class="primary small" id="main">
To assign multiple class attributes, you can use a space between them.
chapter8.html
file with VS Code.<h2>
and <ul>
elements before the </body>
tag.
<body>
<h1><span style="color:red">Chapter 8.</span> CSS Basics</h1>
<h2 class="first-class">Basic Selectors</h2>
<ul>
<li>Type Selector : select elements by tags</li>
<li class="first-class second-class">Class Selector : select elements by class </li>
<li id="first-id">ID Selector : select an element by id</li>
</ul>
</body>
practice.css
file with VS Code.
li{
color:blue;
}
.first-class{
color:red;
font-weight: bold;
}
.second-class{
background-color:aqua;
}
#first-id{
color:green;
}
You can also check the sample result here (Demo Site ). In this demo site, the background color may be different as we'll update the background color in the later part of this course.
Utilizing various CSS selectors is one of the most important skills of CSS coding. There are several CSS selectors. The most basic ones are type selector, class selector, and ID selector. In this section, we'll explain three basic CSS selectors.
This selector is the simplest selector. Use the element name (or tag name) to select elements in the HTML documents which you want to style. The elements with the same tag name will be styled in this approach. You can utilize this approach to create a standard CSS template; however, this approach is not useful for highly customized web pages as the same styles are applied to all the same-name tags.
You can select multiple elements for the same declaration. Use ",
" comma to separate selector names like shown below.
This selector gives you more flexibility in styling. Using a class selector, you can apply different styles to elements with the same tag name. For example, you can make some selected <h3>
elements colored in blue while other <h3>
elements colored in red. There are two steps to set a class sector.
Add a class attribute value in the start tag of the target HTML element.
<li class="nav-link">xxx </li>
Set CSS using the class selector you defined. For the class selector, you need to start with ".
" period.
.nav-link{
color: blue;
}
This selector is similar to the class selector but it can be used for a more specific element. You can use the same class selector multiple times in the same HTML document; however, you can use the same id selector only once in the same HTML document. This is because the id is used to locate a unique element in the HTML document.
There are two steps to set an ID sector.
Similarly to the class attribute, add an id
to the target HTML element.
<p id="highlight">xxx </p>
Set CSS using the id selector you defined. For ID selector, you need to start with "#
" hash.
#highlight{
color: red;
}
You can assign multiple class
attributes or a class
and id
together to one HTML element as in the example below.
<div class="primary small" id="main">
To assign multiple class attributes, you can use a space between them.
chapter8.html
file with VS Code.<h2>
and <ul>
elements before the </body>
tag.
<body>
<h1><span style="color:red">Chapter 8.</span> CSS Basics</h1>
<h2 class="first-class">Basic Selectors</h2>
<ul>
<li>Type Selector : select elements by tags</li>
<li class="first-class second-class">Class Selector : select elements by class </li>
<li id="first-id">ID Selector : select an element by id</li>
</ul>
</body>
practice.css
file with VS Code.
li{
color:blue;
}
.first-class{
color:red;
font-weight: bold;
}
.second-class{
background-color:aqua;
}
#first-id{
color:green;
}
You can also check the sample result here (Demo Site ). In this demo site, the background color may be different as we'll update the background color in the later part of this course.