07-26-2023, 06:41 PM
Example:
<!DOCTYPE html>
<html>
<head>
<title>Selectors Example</title>
<style>
/* Select the paragraph elements and apply a red text color */
p {
color: red;
}
/* Select all elements with the class "highlight" and apply a yellow background color */
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<p>This is a paragraph with red text color.</p>
<p class="highlight">This is a highlighted paragraph with a yellow background.</p>
</body>
</html>
Explanation:
CSS selectors are used to target specific HTML elements for styling. In this example, we use two types of selectors: the element selector (p) targets all p elements and applies a red text color to them. The class selector (.highlight) targets all elements with the class "highlight" and applies a yellow background color.
<!DOCTYPE html>
<html>
<head>
<title>Selectors Example</title>
<style>
/* Select the paragraph elements and apply a red text color */
p {
color: red;
}
/* Select all elements with the class "highlight" and apply a yellow background color */
.highlight {
background-color: yellow;
}
</style>
</head>
<body>
<p>This is a paragraph with red text color.</p>
<p class="highlight">This is a highlighted paragraph with a yellow background.</p>
</body>
</html>
Explanation:
CSS selectors are used to target specific HTML elements for styling. In this example, we use two types of selectors: the element selector (p) targets all p elements and applies a red text color to them. The class selector (.highlight) targets all elements with the class "highlight" and applies a yellow background color.