Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Selectors for Styling HTML Elements | Introduction to CSS
CSS Fundamentals

book
Selectors for Styling HTML Elements

To apply styles effectively, you need to understand CSS selectors, as they determine the HTML elements targeted for styling.

Tag selector

One way to apply styles is by using the element tag itself. Styles specified using a tag selector will affect all elements with that tag. This is useful for applying consistent styling to elements across the website.

Syntax: In the HTML, we have a p element:

html
<p>It was all in my mind</p>

To apply styles in the CSS file, use the tag name (p) as the selector:

css
p {
property: value;
}

Let's run the following example and check how it works.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<p>It was all in my mind</p>
</body>
</html>

Class selector

A more precise way to style elements is by using class selectors. These selectors target elements with specific class names, allowing us to apply styles selectively.

Syntax: In the HTML, add a class attribute with a meaningful class name:

html
<p class="text">This song is another hit.</p>

In the CSS, reference the class name with a period (.) to define the styles:

css
.text {
property: value;
}

Let's run the following example and observe that only elements with the text class will receive these styles, giving you finer control over your styling.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<p class="text">The weather is nice. We would rather go for a walk!</p>
<a class="link" href="#">Navigate to main page</a>
<p class="text">Cactuses are my passion.</p>
</body>
</html>

Class composition

We can also combine multiple classes on a single element, making class composition a powerful tool for applying styles - separate class names with spaces in the class attribute.

Syntax: In the HTML, add multiple class names to an element:

html
<p class="text font">A robot created chemicals.</p>

In the CSS, define styles for each class separately:

css
.text {
property: value;
}

.font {
property: value;
}

Let's run the following example and see how it works. Elements with both the text and font classes will receive the specified styles.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<p class="text font">We test multiple class names</p>
<p class="text">This paragraph has only "text" class name</p>
<p class="font">This paragraph has only "font" class name</p>
</body>
</html>

ID selector

While it's possible to use the id selector for styling, it's generally not recommended. IDs should be unique on a page, limiting their reuse.

Syntax: In the HTML, add an id attribute to an element:

html
<p id="title">Choose from different themes.</p>

In the CSS, reference the ID with a hashtag (#) to define the styles:

css
#title {
property: value;
}

Let's run the following example and observe how it works. This example applies styles to the unique element with the title ID.

html

index.html

css

index.css

copy
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<p id="title">Let's see what can id do for us.</p>
</body>
</html>

1. How can we target and apply styles to this HTML element:

2. How can we target and apply styles to the HTML element with the id="navigation-link"?

question mark

How can we target and apply styles to this HTML element:

<p class="main-paragraph">Please, select this element.</p>

Select the correct answer

question mark

How can we target and apply styles to the HTML element with the id="navigation-link"?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3

Demandez à l'IA

expand
ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

We use cookies to make your experience better!
some-alt