Class Flow
Outcomes
Target- Apply element, .class, #id selectors correctly
- Use group and descendant selectors to target multiple elements
- Understand impact of universal selector (*) and how to use it carefully
Setup
Tools- VS Code / Notepad++
- Chrome / Edge / Firefox
- Files:
index.html+selectors.css
Rule of Thumb
Pro Tip- Start broad (element) → then become precise (class/id)
- Prefer classes for reusable styling
- Use
*carefully (it affects everything)
IN-CLASS PRACTICAL 2B (Part 2) ACTIVITY
Types of CSS Selectors (External CSS File Only)
Create Folder + File
Goal: Create a clean workspace for practising CSS selectors.
- Create a new folder named:
RegNo_Practical2_2 - Inside the folder, create a file named:
index.html - Open the folder in VS Code / Notepad++
10DDT23F1705_Practical2_2
Create Basic HTML Skeleton (Using Emmet)
Goal: Generate a valid HTML document structure using Emmet (!).
- Open
index.htmlin VS Code. - Type
!then press Tab / Enter. - Change the
<title>as shown below. - Save the file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Practical 2.2 - CSS Selectors</title>
</head>
<body>
</body>
</html>
.html and try Ctrl + Space.
Add Content for Selector Practice (Paste Inside <body>)
- Inside
<body>...</body>, paste the HTML block below. - Save → Open in browser → Refresh.
- Verify: You can see the title, a paragraph, a “note box”, a list, and a mini card area.
<header>
<h1>Types of CSS Selectors</h1>
<p>We will practise element, class, id, group, descendant, and universal selectors using external CSS.</p>
</header>
<main>
<section>
<h2>Selector List (Reference)</h2>
<ul>
<li>Element Selector</li>
<li>Class Selector</li>
<li>ID Selector</li>
<li>Group Selector</li>
<li>Descendant Selector</li>
<li>Universal Selector</li>
</ul>
<p>Tip: We will style this list using different selector types step-by-step.</p>
</section>
<section>
<h2>Practice Area</h2>
<div>
<strong>Note:</strong> This box will be styled later using a class selector.
</div>
<div>
<div>
<h3>Card A</h3>
<p>This paragraph will be targeted using a descendant selector.</p>
</div>
<div>
<h3>Card B</h3>
<p>This card will later be styled using a class selector.</p>
</div>
</div>
<p>Next: create an external CSS file and apply selectors one by one.</p>
</section>
</main>
<footer>
<p>Practical 2B - CSS Selectors (External CSS Only)</p>
</footer>
Create External File + Link It (selectors.css)
selectors.css and connect it to index.html.
After this, all selector styling will be written in ONE external file.
- Create a new file in the same folder as
index.html. - Name it
selectors.css. - In
index.html, inside<head>, add the link tag below. - Save
index.html→ Refresh browser.
<link rel="stylesheet" href="selectors.css">
- In
selectors.css, paste this ONE line only: body { background: #fff7ed; }- Save → Refresh browser.
- If page background changes, linkage is correct ✅
selectors.css dan index.html duduk folder yang sama level.
APPLY UNIVERSAL SELECTOR
*) affects all elements globally.
- Open
selectors.css. - At the top of the file, write this selector (do NOT add anything yet):
* {
}
- Add the following properties inside your universal selector.
- Save the file.
box-sizing: border-box;
font-family: Arial, Helvetica, sans-serif;
- Refresh your browser.
- Observe changes in font and spacing.
- Notice how ALL elements are affected.
APPLY ELEMENT SELECTORS
body, h1, h2, p) — no class/id yet.
- After Task 5, type this selector only:
body {
}
- Paste inside
bodyselector:
margin: 0;
padding: 24px;
background: #ffffff;
color: #0f172a;
- Type this selector below
body:
h1 {
}
- Paste inside
h1selector:
margin: 0 0 8px;
text-align: center;
letter-spacing: 1px;
- Type this selector below
h1:
h2 {
}
- Paste inside
h2selector:
margin: 0 0 10px;
color: #dc2626;
text-transform: uppercase;
font-size: 1.05rem;
- Type this selector below
h2:
p {
}
- Paste inside
pselector:
line-height: 1.6;
margin: 10px 0;
- Refresh your browser.
- Observe spacing, font consistency, and readability.
body first important before other elements?
APPLY CLASS SELECTORS
.subtitle, .panel, .note, .highlight).
- Open
selectors.css. - After Task 6, type this class selector (do NOT add properties yet):
.subtitle {
}
.subtitle- Paste these properties inside your
.subtitleselector. - Save the file.
text-align: center;
color: #475569;
margin-bottom: 18px;
- Open your HTML file.
- Find the paragraph under the main heading and add
class="subtitle". - Save HTML → Refresh browser → Observe the subtitle becomes centred.
<p class="subtitle">We will practise element, class, id, group, descendant, and universal selectors using external CSS.</p>
- Back to
selectors.css. - Type this selector below
.subtitle(no properties yet):
.panel {
}
.panel- Paste these properties inside your
.panelselector. - Save the file.
background: #f8fafc;
border: 1px solid rgba(15,23,42,.12);
border-radius: 14px;
padding: 18px;
margin: 16px auto;
max-width: 900px;
- In HTML, add
class="panel"to BOTH<section>blocks. - Save HTML → Refresh browser → Observe sections now look like cards.
<section class="panel">...</section>
- Back to
selectors.css. - Type this selector below
.panel(no properties yet):
.note {
}
.note- Paste these properties inside your
.noteselector. - Save the file.
background: rgba(13,110,253,.08);
border: 1px solid rgba(13,110,253,.22);
border-radius: 12px;
padding: 12px 14px;
margin: 12px 0 16px;
- In HTML, find the Note box
<div>. - Add
class="note". - Save HTML → Refresh browser → Observe the note box now stands out.
<div class="note">
<strong>Note:</strong> This box will be styled later using a class selector.
</div>
- Back to
selectors.css. - Type this selector below
.note(no properties yet):
.highlight {
}
.highlight- Paste this property inside your
.highlightselector. - Save the file.
border: 2px solid rgba(255,193,7,.9);
- In HTML, find the container that holds Card B.
- Add
class="highlight"to that<div>. - Save HTML → Refresh browser → Observe Card B gets a highlight border.
<div class="highlight">
<h3>Card B</h3>
<p>This card will later be styled using a class selector.</p>
</div>
- Refresh your browser.
- Observe: subtitle alignment + colour change.
- Observe: sections appear as “cards”.
- Observe: note box stands out.
- Observe: Card B has a highlight border.
APPLY ID SELECTORS
#pageTitle, #practiceArea).
- Open
selectors.css. - After Task 7, type this selector (no properties yet):
#pageTitle {
}
#pageTitle- Paste these properties inside your
#pageTitleselector. - Save the file.
color: #ef4444;
font-size: 2rem;
- Open your HTML file.
- Find the main heading:
- Add
id="pageTitle"to the<h1>tag. - Save HTML → Refresh browser → Observe title colour & size change.
<h1 id="pageTitle">Types of CSS Selectors</h1>
- Back to
selectors.css. - Type this selector below
#pageTitle(no properties yet):
#practiceArea {
}
#practiceArea- Paste this property inside your
#practiceAreaselector. - Save the file.
border-left: 6px solid rgba(32,201,151,.55);
- In HTML, find the section titled Practice Area.
- Add
id="practiceArea"to that<section>tag. - Save HTML → Refresh browser → Observe left border appears.
<section id="practiceArea">
<h2>Practice Area</h2>
...
</section>
- Refresh your browser.
- Observe: page title changes colour & size.
- Observe: practice area has a left accent border.
- Confirm: each ID is used only once.
APPLY GROUP SELECTOR
- Open
selectors.css. - After Task 8, type this group selector (no properties yet):
h3, .footerText, footer p {
}
- Add this property inside your group selector.
- Save the file.
color: #2563eb;
- Open your HTML file.
- Find this paragraph:
- Add
class="footerText"to it.
<p class="footerText">Next: create an external CSS file and apply selectors one by one.</p>
- Refresh your browser.
- Observe all
<h3>headings turn blue. - Observe footer text colour changes.
- Observe how one rule affects different selector types.
APPLY DESCENDANT SELECTOR
.cards p, .selector-list li).
- Open
selectors.css. - After Task 9, type this selector (no properties yet):
.cards p {
}
- Add these properties inside
.cards p. - Save the file.
margin: 8px 0 0;
color: #334155;
- Open your HTML file.
- Find the container that wraps Card A and Card B.
- Add
class="cards"to that wrapper<div>.
<div class="cards">
<div>
<h3>Card A</h3>
<p>This paragraph will be targeted using a descendant selector.</p>
</div>
<div>
<h3>Card B</h3>
<p>This card will later be styled using a class selector.</p>
</div>
</div>
- Back to
selectors.css. - Type this selector below
.cards p(no properties yet):
.selector-list li {
}
- Add these properties inside
.selector-list li. - Save the file.
padding: 6px 0;
border-bottom: 1px dashed rgba(15,23,42,.18);
- In HTML, find the selector reference list (
<ul>). - Add
class="selector-list"to the<ul>.
<ul class="selector-list">
<li>Element Selector</li>
<li>Class Selector</li>
<li>ID Selector</li>
<li>Group Selector</li>
<li>Descendant Selector</li>
<li>Universal Selector</li>
</ul>
.cards p = “semua p yang berada di dalam .cards sahaja”.
Jadi p lain dekat page tak kacau.
- Refresh your browser.
- Observe: paragraph dalam
.cardsnampak lebih kemas (spacing + colour). - Observe: item
lidalam.selector-listada dashed line di bawah. - Confirm: hanya content dalam container yang terkesan.
.cards p not affect paragraphs outside .cards?
MAKE “CARDS” DISPLAY SIDE-BY-SIDE
.cards nampak macam 2 card sebelah-sebelah (basic layout).
Ini beginner-friendly—tak perlu framework, just CSS.
- Pastikan dalam HTML memang ada container
class="cards". - Dan setiap card ada
class="card-item". - Kalau dah ada (Task 7/10), teruskan ke Step 1.
<div class="cards">
<div class="card-item">
<h3>Card A</h3>
<p>...</p>
</div>
<div class="card-item">
<h3>Card B</h3>
<p>...</p>
</div>
</div>
- Open
selectors.css. - Add these selectors after Task 10 (don’t paste properties yet):
.cards {
}
.card-item {
}
.card-item h3 {
}
- Copy the properties below into the correct selector blocks.
- Save → Refresh browser.
/* Task 11: Cards Layout (simple grid) */
/* Layout: place card items side-by-side */
.cards {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
margin-top: 10px;
}
/* Card box styling */
.card-item {
background: #ffffff;
border: 1px solid rgba(15,23,42,.10);
border-radius: 12px;
padding: 14px;
}
/* Descendant: only style headings inside card-item */
.card-item h3 {
margin: 0 0 6px;
letter-spacing: .5px;
}
- Bila screen cukup lebar, Card A & Card B akan jadi 2 kolum (side-by-side).
- Setiap card nampak macam “box” (padding + border halus).
- Tajuk
h3dalam card nampak lebih kemas (spacing berubah).
.card-item h3 only affect headings inside cards, not all h3 on the page?
STYLE FOOTER + SMALL ENHANCEMENTS
- Open
selectors.css. - After Task 11, type this selector (no properties yet):
footer {
}
footer- Paste these properties inside
footer { }. - Save → Refresh browser → Observe footer becomes a neat “closing box”.
margin-top: 18px;
padding: 14px 16px;
border-top: 1px solid rgba(15,23,42,.10);
text-align: center;
background: #f1f5f9;
border-radius: 14px;
- Open
index.htmland make sure you already have a<footer>element. - No need to add class/id—this is an element selector.
- Refresh and confirm the footer section is styled.
<footer>
<p>Practical 2.2 - CSS Selectors (External CSS Only)</p>
</footer>
- In
selectors.css, type this selector belowfooter(no properties yet):
code {
}
code- Paste these properties inside
code { }. - Save → Refresh browser.
- Observe: semua inline
codejadi lebih jelas (macam label kecil).
background: rgba(2,6,23,.06);
padding: 2px 6px;
border-radius: 8px;
- No need to add class/id. Any existing
<code>will be styled automatically. - Confirm you can see multiple
codelabels inside instructions.
- In
selectors.css, type these selectors belowcode(no properties yet):
a {
}
a:hover {
}
a and a:hover- Paste these properties into the correct selectors.
- Save → Refresh browser.
/* (Optional) Link styling if you add any <a> later */
color: #2563eb;
text-decoration: none;
text-decoration: underline;
- Kalau page awak belum ada link, tambah satu link ringkas untuk test.
- Save HTML → Refresh → Hover link dan observe underline.
<footer>
<p>Practical 2.2 - CSS Selectors (External CSS Only) | <a href="#">Back to top</a></p>
</footer>
- Ensure both files exist:
index.html,selectors.css - Run in browser (refresh) and check all sections visible.
- Zip folder:
RegNo_Practical2_2.zip
Recap (What you just applied)
- Universal:
*untuk baseline (box-sizing, font) - Element:
body,h1,h2,p - Class:
.panel,.note,.highlight - ID:
#pageTitle,#practiceArea - Group:
h3, .footerText, footer p - Descendant:
.cards p,.selector-list li,.card-item h3