Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Other Selectors | Selectors and Cascade
Introduction to CSS Part I

book
Other Selectors

Element selector

This selector is used to select and style all elements of a particular type on the page. It is identified by the element type (e.g., p, div, h1, etc.).

css
p {
color: blue;
}

This selector will select and style all p elements on the page, giving them a blue color.

html

index.html

css

index.css

copy
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<p>I am a paragraph</p>
<p>I am also a paragraph</p>
</body>
</html>

Descendant selector

This selector is used to select and style elements that are descendants of a particular element. The ancestor element, a space, and the descendant element identify it.

css
div p{
font-size: 12px;
}

This selector will select and style all p elements descendants of a div element, giving them a font size of 12px.

html

index.html

css

index.css

copy
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div>
<p>I am a paragraph inside a div</p>
</div>
<p>I am a paragraph outside of a div</p>
</body>
</html>

Child selector

This selector is used to select and style elements that are direct children of a particular element. It is identified by the parent element, followed by a > symbol, followed by the child element.

css
div > p {
font-style: italic;
}

This selector will select and style all p elements that are direct children of a div element, giving them an italic font style.

html

index.html

css

index.css

copy
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div>
<p>I am a direct child of a div</p>
<a>
<p>I am not a direct child of a div</p>
</a>
</div>
<p>I am not a child of a div</p>
</body>
</html>

Adjacent sibling selector

This selector is used to select and style elements that are siblings of a particular element and come immediately after it. It is identified by the first element, followed by a + symbol, followed by the second element.

css
div + p {
margin-top: 0;
}
html

index.html

css

index.css

copy
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div>
<p>I am the paragraph inside a div</p>
</div>
<p>I am the first paragraph after the div</p>
<p>I am the second paragraph after the div</p>
</body>
</html>

General sibling selector

This selector is used to select and style elements that are siblings of a particular element and come after it. It is identified by the first element, followed by a ~ symbol, followed by the second element. For example:

css
div ~ p {
color: red;
}

This selector will select and style all p elements that are siblings of a div element and come after it, giving them a red color.

html

index.html

css

index.css

copy
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<div>
<p>I am the paragraph inside a div</p>
</div>
<p>I am the first paragraph after the div</p>
<p>I am the second paragraph after the div</p>
</body>
</html>

Attribute selector

This selector selects and styles elements based on their attribute values. It is identified by the element type, followed by an opening square bracket [, followed by the attribute name and value, and a closing square bracket ].

css
a[href^="https"] {
color: orange;
}

This selector will select and style all elements with an href attribute that starts with 'https', giving them an orange color.

html

index.html

css

index.css

copy
<html>
<head>
<link rel="stylesheet" href="index.css" />
</head>
<body>
<a href="https://codefinity.com/">
I am a link with href='https://codefinity.com/'
</a>
</body>
</html>

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. 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