Selectors HTML 4TH PRG
Selectors HTML 4TH PRG
<!DOCTYPE html>
<html>
<head>
<title>Simple Selectors Example</title>
<style>
/* Universal Selector */
*{
font-family: Arial, sans-serif; /* Applies to all elements */
}
/* Element Selector */
p{
color: blue; /* All <p> elements will have blue text */
}
/* Class Selector */
.highlight {
background-color: yellow; /* Elements with class="highlight" will have a yellow background */
}
/* ID Selector */
#header {
font-size: 24px; /* The element with id="header" will have a larger font size */
color: green;
}
/* Group Selector */
h1, h2, h3 {
color: red; /* All <h1>, <h2>, and <h3> elements will have red text */
}
</style>
</head>
<body>
<h1>This is a Heading 1</h1>
<h2>This is a Heading 2</h2>
<h3>This is a Heading 3</h3>
<p>This is a paragraph.</p>
<p class="highlight">This paragraph is highlighted.</p>
<div id="header">This is the header.</div>
</body>
</html>
Combinator selectors :
<!DOCTYPE html>
<html>
<head>
<title>Combinator Selectors Example</title>
<style>
/* Descendant Combinator */
div p {
color: blue; /* All <p> elements inside <div> */
}
/* Child Combinator */
div > span {
background-color: yellow; /* Only <span> elements directly inside <div> */
}
<h2>Heading 2</h2>
<p>This paragraph is styled because it is adjacent to h2 (adjacent sibling combinator).</p>
<p>This paragraph is not styled.</p>
<h3>Heading 3</h3>
<p>This paragraph is styled because it is a sibling of h3 (general sibling combinator).</p>
<p>This paragraph is also styled.</p>
</body>
</html>
PSEUD0-CLASS SELECTORS
Pseudo-selectors in CSS are used to style elements based on their state or position.
They are divided into two main categories:
Pseudo-classes: Target elements based on their state (e.g., :hover, :focus, :nth-child).
Pseudo-elements: Target specific parts of an element (e.g., ::before, ::after, ::first-
line).
<!DOCTYPE html>
<html>
<head>
<title>Pseudo-Selectors Example</title>
<style>
/* Pseudo-classes */
a:hover {
color: red; /* Changes link color to red on hover */
}
p:first-child {
font-weight: bold; /* Makes the first <p> element bold */
}
li:nth-child(odd) {
background-color: lightgray; /* Styles odd list items with a light gray background */
}
input:focus {
border: 2px solid blue; /* Adds a blue border to the input field when focused */
}
/* Pseudo-elements */
p::first-line {
font-size: 1.2em; /* Makes the first line of every <p> larger */
}
p::after {
content: " (Read more)"; /* Adds text after every <p> */
color: green;
}
</style>
</head>
<body>
<h1>Pseudo-Selectors Example</h1>
<p>This is the first paragraph. It will have a bold font and a larger first line.</p>
<p>This is the second paragraph. It will also have a larger first line and some additional text at the end.</p>
<ul>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ul>
<!DOCTYPE html>
<html>
<head>
<title>Pseudo-Element Selectors Example</title>
<style>
/* ::first-line - Styles the first line of a block-level element */
p::first-line {
font-weight: bold; /* Makes the first line bold */
color: blue; /* Changes the first line color to blue */
}
ATTRIBUTE SELECTOR
<!DOCTYPE html>
<html>
<head>
<title>Simple Attribute Selectors Example</title>
<style>
/* [attribute] - Styles elements with a specific attribute */
a[target] {
color: red; /* Links with a 'target' attribute will be red */
}