CSS Deep Dive ā The Clothing
CSS (Cascading Style Sheets) controls how HTML elements look.
- Without CSS: Plain, default-styled content
- With CSS: Beautiful, branded, professional designs
CSS Basic Structure
Every CSS rule follows this pattern:
CSS
selector { property: value; property: value;}Example:
CSS
h1 { color: blue; font-size: 32px;}- Selector (
h1) ā What to style - Property (
color) ā What aspect to change - Value (
blue) ā What to change it to
Selector Types
1. Element Selector
Targets all elements of a type:
CSS
p { color: gray;}Targets: ALL <p> elements on the page
2. Class Selector
Targets elements with a specific class:
CSS
.highlight { background-color: yellow;}Targets: All elements with class="highlight"
3. ID Selector
Targets ONE specific element:
CSS
#main-title { font-size: 48px;}Targets: The ONE element with id="main-title"
4. Descendant & Combined Selectors
CSS
/* Descendant - elements inside other elements */header nav a { color: white;} /* Combined - multiple conditions */div.card { border: 1px solid gray;} /* Multiple - same styles for many */h1, h2, h3 { font-family: Arial, sans-serif;}Selector Specificity
When multiple rules target the same element, which wins?
Specificity Hierarchy (strongest to weakest)
- Inline styles (
style="...") ā Highest priority - ID selectors (
#myId) ā High priority - Class selectors (
.myClass) ā Medium priority - Element selectors (
p) ā Low priority
CSS
p { color: black; } /* Specificity: 0,0,1 */.intro { color: blue; } /* Specificity: 0,1,0 */#main-paragraph { color: red; } /* Specificity: 1,0,0 */For <p class="intro" id="main-paragraph">: The text will be red because ID has highest specificity.
The Box Model
Every HTML element is a box with layers:
Text
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā MARGIN ā ā Outside spaceā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā āā ā BORDER ā ā ā The border lineā ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā āā ā ā PADDING ā ā ā ā Inside spaceā ā ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā ā āā ā ā ā CONTENT ā ā ā ā ā Your actual contentā ā ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā ā āā ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā āā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāBox Model Properties
CSS
.box { /* Content size */ width: 200px; height: 100px; /* Padding - space inside the border */ padding: 20px; /* All sides */ padding: 10px 20px; /* Top/bottom, Left/right */ /* Border */ border: 1px solid black; border-radius: 8px; /* Rounded corners */ /* Margin - space outside the border */ margin: 20px; /* All sides */ margin: 10px auto; /* Center horizontally */}Remember
- Padding: Space between content and border (inside)
- Margin: Space between elements (outside)
Color Properties
CSS
/* Text Color */p { color: blue; /* Named color */ color: #3498db; /* Hex code */ color: rgb(52, 152, 219); /* RGB */} /* Background */.card { background-color: #f5f5f5; /* Gradient */ background: linear-gradient(to right, #667eea, #764ba2); /* Image */ background-image: url('background.jpg'); background-size: cover;}Typography Properties
CSS
.text { /* Font family */ font-family: Arial, Helvetica, sans-serif; /* Size */ font-size: 16px; /* Weight */ font-weight: bold; /* or 700 */ /* Alignment */ text-align: center; /* Decoration */ text-decoration: none; /* Remove underlines */ /* Line spacing */ line-height: 1.6;}Layout Properties
Display Property
CSS
.element { display: block; /* Takes full width, new line */ display: inline; /* Only as wide as content */ display: flex; /* Flexbox container */ display: none; /* Hidden completely */}Flexbox (Most Important!)
CSS
.container { display: flex; /* Horizontal alignment */ justify-content: center; /* Center */ justify-content: space-between;/* Spread evenly */ /* Vertical alignment */ align-items: center; /* Center vertically */ /* Direction */ flex-direction: row; /* Horizontal (default) */ flex-direction: column; /* Vertical */ /* Gap between items */ gap: 20px;}Position Property
CSS
.element { position: static; /* Default, normal flow */ position: relative; /* Relative to normal position */ position: absolute; /* Relative to positioned ancestor */ position: fixed; /* Fixed to viewport */ /* Then position with */ top: 10px; right: 10px;}Interactive States
CSS
/* Hover Effect */button { background: blue; transition: background 0.3s; /* Smooth transition */} button:hover { background: darkblue;} /* Focus (for inputs) */input:focus { outline: 2px solid blue;} /* Active (while clicking) */button:active { transform: scale(0.95);}Responsive Design
CSS
/* Default styles (mobile first) */.container { padding: 10px; flex-direction: column;} /* Tablet and up */@media (min-width: 768px) { .container { padding: 20px; flex-direction: row; }} /* Desktop and up */@media (min-width: 1024px) { .container { padding: 40px; max-width: 1200px; margin: 0 auto; }}Breakpoint
Size
Mobile
< 768px
Tablet
768px - 1023px
Desktop
1024px+
Complete Example
CSS
/* Reset default margins */* { margin: 0; padding: 0; box-sizing: border-box;} /* Card component */.card { background: white; border-radius: 12px; padding: 24px; margin: 20px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);} /* Button */.button { display: inline-block; background: #3498db; color: white; padding: 12px 24px; border: none; border-radius: 6px; cursor: pointer; transition: background 0.3s;} .button:hover { background: #2980b9;}Lesson Summary
CSS Structure
selector { property: value; }
Selector Priority
- Inline styles (highest)
- ID selectors (#)
- Class selectors (.)
- Element selectors (lowest)
The Box Model
Content ā Padding ā Border ā Margin
Key Properties
Category
Properties
Colors
color, background-color
Typography
font-size, font-family, font-weight
Spacing
margin, padding, gap
Layout
display: flex, justify-content, align-items
Interactive
:hover, :focus, transition
š Quiz
1/3What selector has the HIGHEST specificity?
Next Up
Lesson 2.4: JavaScript Deep Dive
Understand the programming language that makes pages interactive!