Sibling Selector
Sibling Selector
html
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Adjacent Sibling Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Welcome to My Website</h1>
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<p>This is the third paragraph.</p>
</body>
</html>
CSS Code:
css
Copy
/* Style only the <p> that immediately follows <h1> */
h1 + p {
color: red;
font-weight: bold;
}
Explanation:
The selector h1 + p will only apply styles to the first <p> element that immediately
follows the <h1>.
In this case, the second <p> ("This is the first paragraph.") will be styled with red color
and bold font weight, because it is directly after the <h1>.
The third <p> ("This is the third paragraph.") will not be affected because it is not
immediately after the <h1>.
The general sibling selector (~)
in CSS selects all elements that are siblings of a specified element. It targets all elements that
come after the specified element in the same parent, regardless of their type.
Example:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>General Sibling Selector Example</title>
<style>
/* Apply a style to all <p> elements that follow an <h2> element */
h2 ~ p {
color: green;
font-weight: bold;
}
</style>
</head>
<body>
<h2>This is a Heading</h2>
<p>This paragraph will turn green and bold because it is a sibling of the
<code>h2</code>.</p>
<p>This paragraph will also turn green and bold.</p>
<h2>Another Heading</h2>
<p>This paragraph will not be affected because it comes after the second
<code>h2</code>.</p>
</body>
</html>
Output:
The paragraphs that come after the first <h2> (in this case, the first two <p> elements)
will have the text color changed to green and the font will be bold.
The last <p> element after the second <h2> will not be styled because it is not following
the first <h2>.
This demonstrates how the general sibling selector (~) targets all siblings that appear after the
specified element, in this case, <h2>.
In CSS, pseudo-classes are used to define the special state of an element, such as when it's being
hovered over or focused. These pseudo-classes are prefixed with a colon (:) and can be used to
target elements in various states.
:hover: When you hover over the button, the background color changes to light coral.
:focus: When the button is focused (such as by clicking on it or using the keyboard), the
border outline changes to orange.
:active: When you press the button, it turns dark red and the text color changes to
white.
</style>
</head>
<body>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
</body>
</html>
Explanation:
:nth-child(2n): Selects every second <li> in the list, changing its color to orange.
:first-child: Selects the first <li>, making its text bold and changing its color to blue.
:last-child: Selects the last <li>, underlining the text.
Example 3: :not()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pseudo-Class Example</title>
<style>
p {
color: gray;
}
Explanation:
:not(:first-child): This targets all paragraphs except the first one, turning their color
to black, while the first paragraph remains gray.
Summary:
Pseudo-classes allow you to select elements based on their dynamic state or position, providing
more control and interactivity in styling.
Pseudo-elements in CSS, specifically ::before and ::after, are used to add content before or
after an element's original content. These pseudo-elements can be used to enhance design, such
as adding decorations or icons, without modifying the HTML structure.
Syntax:
selector::before {
content: "Text or content before the element";
/* other styles */
}
selector::after {
content: "Text or content after the element";
/* other styles */
}
Example:
Let's take an example of a paragraph where we use ::before to add content before the text and
::after to add content after the text.
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pseudo-elements Example</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<p class="example">This is a paragraph.</p>
</body>
</html>
CSS:
.example::before {
content: "Before text - ";
color: red;
font-weight: bold;
}
.example::after {
content: " - After text";
color: blue;
font-style: italic;
}
Output:
The paragraph will appear like this:
The text "Before text - " will be added before the paragraph content, in red and bold.
The text " - After text" will be added after the paragraph content, in blue and italic.
Key Points: