📚 Week 2 Day 2: CSS Intermediate — Layout, Selectors, and Rules
🎯 Topics for Today:
1️⃣ Pseudo Classes and Elements
2️⃣ Sibling Combinators: ., >, +, ~
3️⃣ Box Model Deep Dive
4️⃣ Margin, Border, Padding — Full Control
5️⃣ CSS Conflicts: Cascading, Source, Specificity, Inheritance
✅ 1️⃣ Pseudo Classes and Elements
➤ What Are They?
They target
specific states or
parts of elements.
✨ Pseudo Classes Examples:
➡️ :hover
→ When mouse is over.
➝Imagine a button in an app , when you hover your mouse over it , the color changes.
button:hover {
background-color: yellow; }
➡️ :focus → When an input is active/selected.
input:focus {
border-color: blue; }
➡️ :first-child → First element inside parent.
ul li:first-child {
color: red; }
➡️ :nth-child(2) → Second child.
p:nth-child(2) {
font-weight: bold; }
✨ Pseudo Elements Examples:
➝they are used when you want to add decoration with out changing the html
➡️ ::before
p::before {
content: "👉 "; }
➡️ ::after
p::after {
content: " ✅"; }
➡️ ::first-letter
p::first-letter {
font-size: 30px; }
✅ 2️⃣ Sibling Combinators in CSS
➤ Why Use Them?
To style elements
based on their position relative to others. This helps you control exactly where and when styles apply.
✔️ Examples:
➡️ .class1 .class2 {} — Any .class2 inside .class1.
div p {
color: green; }
➡️ > (Child Selector)
div > p {
color: blue; }
Only selects
direct children, not grand children 😁
➡️ + (Adjacent Sibling Selector)
h1 + p {
color: orange; }
Selects the
very next <p> after <h1>.
➡️ ~ (General Sibling Selector)
h1 ~ p {
color: purple; }
Selects
all following siblings.
✅ 3️⃣ Box Model Deep Dive
All HTML elements have:
➝
Content = what's written/shown
→ Padding = space inside box , around content
→ Border = the edge of the box
→ Margin = space outside the box , around other boxes
➤ Display Types
✔️ display: block;
✔️ display: inline;
✔️ display: inline-block;
✅ Example:
span {
display: block; }
➤ Box Sizing
✔️ box-sizing: content-box; (default , width doesn't include padding or border)
✔️ box-sizing: border-box; (includes padding + border)
✅ Example:
div {
width: 200px;
padding: 20px;
box-sizing: border-box; }
✅ 4️⃣ Margin, Border, Padding — Sides
You can control:
margin-top
margin-bottom
margin-left
margin-right
✅ Example:
div {
margin-top: 20px;
margin-right: 10px;
border: 2px solid black;
padding: 15px; }
Or shorthand:
margin: 20px 10px 15px 5px; /* Top, Right, Bottom, Left */
✅ 5️⃣ CSS Conflicts: Cascading, Source, Specificity, Inheritance
When two styles target the same thing,
which one wins?
➤ Cascading Order
Inline styles (strongest)
Internal CSS (in <style> tags)
External CSS (weakest)
➤ Specificity
More specific selectors override simpler ones.
3⃣type selectors(p, img, div)==weak
2⃣class selectors(.highleight, .topic)==stronger
1⃣ID selectors (#main, #sec)==Strongest
➤ Source Order
The last rule in the CSS file usually wins if specificity is equal.
➤ Inheritance
Some properties like color, font-family, line-height inherit from parent.
But layout properties like margin, padding , border
don’t.
✅ 6️⃣ Quick Summary Checklist
✔️ Pseudo Classes = :hover, :first-child, :nth-child()
✔️ Pseudo Elements = ::before, ::after
✔️ Sibling Selectors = >, +, ~
✔️ Box Model = Padding, Border, Margin
✔️ CSS Conflicts = Understand specificity, cascade, inheritance
#Week2 #Day2 #Css #CssLayout #lesson