Week 4
Week 4
Selector forms
● Pseudo-class selector
● Pseudo-element selector
● Attribute selector
--------------------------------@@@@@@@@@@@@------------------------------
Description:
A simple HTML and CSS example that demonstrates different types of selectors, including element, ID,
class, group, and universal selectors.
1. Element Selector: Styles all <h1> elements with a blue color and centered alignment.
2. ID Selector: Targets the element with the unique id="unique-paragraph".
3. Class Selector: Applies styles to elements with the class highlight.
4. Group Selector: Targets <p> and <li> elements together for shared styling.
5. Universal Selector: Applies global styling to reset margins, padding, and box-sizing for all
elements.
Source Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
h1 {
color: blue;
text-align: center;
/* Simple Selector: ID */
#unique-paragraph {
font-size: 18px;
color: green;
.highlight {
background-color: yellow;
font-weight: bold;
/* Group Selector */
p, li {
line-height: 1.6;
}
/* Universal Selector */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
</style>
</head>
<body>
<ul>
</ul>
</body>
</html>
Output:
Description:
1. Descendant Selector (div p): Styles all <p> elements that are descendants of a <div>.
2. Child Selector (div > p): Styles only the <p> elements that are direct children of a <div>.
3. Adjacent Sibling Selector (h1 + p): Styles the <p> element immediately following an <h1>.
4. General Sibling Selector (h1 ~ p): Styles all <p> elements that are siblings of an <h1>.
Source Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
color: blue;
text-align: center;
/* Simple Selector: ID */
#unique-paragraph {
font-size: 18px;
color: green;
.highlight {
background-color: yellow;
font-weight: bold;
/* Group Selector */
p, li {
line-height: 1.6;
/* Universal Selector */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Combinator Selectors */
/* Descendant Selector */
div p {
color: darkblue;
/* Child Selector */
div > p {
padding: 5px;
h1 + p {
font-style: italic;
h1 ~ p {
text-decoration: underline;
</style>
</head>
<body>
<div>
<span>
</span>
</div>
<ul>
</ul>
</body>
</html>
Output:
C. Write a program to apply different types of selector forms
● Pseudo-class selector
● Pseudo-element selector
● Attribute selector
Description:
1. Pseudo-class Selector
2. Pseudo-element Selector
3. Attribute Selector
Source Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
h1 {
color: blue;
text-align: center;
/* Simple Selector: ID */
#unique-paragraph {
font-size: 18px;
color: green;
.highlight {
background-color: yellow;
font-weight: bold;
/* Group Selector */
p, li {
line-height: 1.6;
}
/* Universal Selector */
*{
margin: 0;
padding: 0;
box-sizing: border-box;
/* Combinator Selectors */
/* Descendant Selector */
div p {
color: darkblue;
/* Child Selector */
div > p {
padding: 5px;
h1 + p {
font-style: italic;
h1 ~ p {
text-decoration: underline;
}
/* Pseudo-class Selectors */
a:hover {
color: red;
text-decoration: underline;
li:first-child {
font-weight: bold;
li:last-child {
font-style: italic;
/* Pseudo-element Selectors */
p::first-line {
font-size: larger;
font-weight: bold;
p::before {
color: gray;
/* Attribute Selectors */
input[type="text"] {
border: 2px solid blue;
padding: 5px;
input[placeholder] {
font-style: italic;
color: gray;
</style>
</head>
<body>
<div>
<span>
</span>
</div>
</ul>
<form>
</form>
</body>
</html>
Output: