Type selectors
Lesson 3 of 15 in Coddy's CSS Selectors course.
Type selectors are a fundamental concept in CSS that allow you to target and style HTML elements based on their tag names. For example, the selector h1 selects all h1 elements on the page.
Here are some examples of type selectors:
h1- Selects allh1elementsp- Selects allpelementsimg- Selects allimgelementsdiv- Selects alldivelements
The syntax involves writing the tag name followed by curly braces containing the styling properties.
tagname {
/* styling properties */
}You can apply various styles to elements using type selectors. Here's an example of styling all <strong><h1></strong> (header 1) elements with a blue color and larger font size:
h1 {
color: red;
font-size: 34px;
}Type selectors can also be used to style a specific element of a particular type. For example, the following CSS rule will style the first h1 element on the page to be blue:
h1:first-child {
color: blue;
}You can also apply the same styles to multiple types of elements by grouping their type selectors together. Separate them with commas within the selector.
h2, h3, h4 {
font-style: italic;
}Challenge
EasyWrite a CSS rule to style all h1, h2, and h3 elements to have a red color and a bold font weight.
Try it yourself
<!DOCTYPE html>
<html>
<head>
<title>Type Selector Exercise</title>
<style>
/* CSS Code goes here */
</style>
</head>
<body>
<h1>This is a heading</h1>
<h2>This is a subheading</h3>
<h3>This is another subheading</h3>
</body>
</html>
All lessons in CSS Selectors
3Combinator Selectors
Child selectorDescendant selectorAdjacent sibling selectorGeneral sibling selector