week4
week4
Summary
● Universal Selector (*) → Applies to all elements.
● Element Selector (h1) → Targets specific elements.
● ID Selector (#unique) → Styles a single unique element.
● Class Selector (.highlight) → Styles multiple elements with the same
class.
● Group Selector (h2, h3) → Styles multiple elements at once.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Selectors Example</title>
<style>
/* Universal Selector */
*{
font-family: Arial, sans-serif;
}
/* Element Selector */
h1 {
color: blue;
}
/* ID Selector */
#unique {
background-color: yellow;
padding: 10px;
}
/* Class Selector */
.highlight {
color: red;
font-weight: bold;
}
/* Group Selector */
h2, h3 {
color: green;
}
</style>
</head>
<body>
<h1>Element Selector Example</h1>
<h2>Group Selector Example (h2)</h2>
<h3>Group Selector Example (h3)</h3>
<p id="unique">This is an ID selector example.</p>
<p class="highlight">This is a Class selector example.</p>
<p>This is a normal paragraph.</p>
</body>
</html>
Combinator Selectors in CSS
Combinator selectors define relationships between elements in an HTML
document. They help apply styles based on how elements are structured.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Combinator Selectors</title>
<style>
/* Descendant Selector (any <p> inside a <div>) */
div p {
color: red;
}
<div>
<p>This paragraph is inside div (red).</p>
<section>
<p>This paragraph is inside a section within div (red, due to descendant
selector).</p>
</section>
<p>This paragraph is a direct child of div (blue).</p>
</div>
<p>This paragraph is outside div and not affected.</p>
</body>
</html>
Explanation
1. Descendant Selector (div p)
o Applies to all <p> elements inside a <div>, even if deeply nested.
o Example: <p> inside <section> within <div> is affected.
2. Child Selector (div > p)
o Applies only to direct <p> children of <div>.
o Example: The last <p> inside <div> is blue, but the nested one is
not.
3. Adjacent Sibling Selector (h1 + p)
o Styles only the first <p> that comes immediately after <h1>.
o Example: The first <p> after <h1> is green.
4. General Sibling Selector (h1 ~ p)
o Styles all <p> elements that appear after <h1>.
o Example: All <p> elements after <h1> are orange.
Pseudo-Class Selectors in CSS
A pseudo-class is a keyword added to a CSS selector that defines a special state of an
element. It allows you to style elements based on user interaction, position, or other
conditions without modifying the HTML.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Pseudo-Class Selectors</title>
<style>
/* Hover Effect */
button:hover {
background-color: blue;
color: white;
}
/* Focus Effect */
input:focus {
border: 2px solid green;
outline: none;
}
/* nth-child Example */
li:nth-child(odd) {
color: red;
}
/* First Child */
p:first-child {
font-weight: bold;
}
/* Last Child */
p:last-child {
color: blue;
}
/* Not Selector */
p:not(.special) {
color: gray;
}
</style>
</head>
<body>
<h1>Pseudo-Class Selector Example</h1>
<br><br>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
</ul>
<div>
<p>First paragraph (first-child, bold).</p>
<p class="special">Second paragraph (special class, not affected
by :not).</p>
<p>Third paragraph (gray, affected by :not).</p>
<p>Last paragraph (last-child, blue).</p>
</div>
</body>
</html>
Explanation
1. :hover → Changes button color when hovered.
2. :focus → Highlights input when focused.
3. :nth-child(odd) → Styles every odd <li> element in red.
4. :first-child → Makes the first <p> bold.
5. :last-child → Colors the last <p> blue.
6. :not(.special) → Styles all <p> elements except those with class special.