Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Mastering CSS Selectors for Styling HTML Elements | Getting Started with CSS
CSS Fundamentals
course content

Conteúdo do Curso

CSS Fundamentals

CSS Fundamentals

1. Getting Started with CSS
2. Styling Text in CSS
3. The CSS Box Model & Spacing Elements
4. Mastering Flexbox for Layouts
5. Adding Decorative Effects with CSS

book
Mastering CSS Selectors for Styling HTML Elements

To apply styles effectively, you need to understand CSS selectors, which determine the HTML elements to target for styling. Let's explore the main types of selectors.

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

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

css

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

html

index.html

css

styles.css

copy

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

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

css

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

styles.css

copy

All elements with the class="text" attribute are styled with red text, a font size of 24px, and a wheat-colored background. The class name text is defined in index.css using a . prefix.

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

In the CSS, define styles for each class separately:

css

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

styles.css

copy

The <p> element with both text and font classes receives styles from both selectors. The text class sets the color to navy, and the font class sets the font size to 24px.

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

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

css

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

styles.css

copy

The id="title" attribute uniquely identifies the <p> element, and the styles defined with the #title selector apply only to that specific element.

1. Select all possible ways to target an HTML element to apply styles.

2. What is the way to target and style the HTML element with class="navigation-link"?

question mark

Select all possible ways to target an HTML element to apply styles.

Select the correct answer

question mark

What is the way to target and style the HTML element with class="navigation-link"?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
Sentimos muito que algo saiu errado. O que aconteceu?
some-alt