3-terrarium/2-intro-to-css/README.md
journey
title Your CSS Styling Journey
section Foundation
Link CSS file: 3: Student
Understand cascade: 4: Student
Learn inheritance: 4: Student
section Selectors
Element targeting: 4: Student
Class patterns: 5: Student
ID specificity: 5: Student
section Layout
Position elements: 4: Student
Create containers: 5: Student
Build terrarium: 5: Student
section Polish
Add visual effects: 5: Student
Responsive design: 5: Student
Glass reflections: 5: Student
Sketchnote by Tomomi Imura
Remember how your HTML terrarium looked quite basic? CSS is where we transform that plain structure into something visually appealing.
If HTML is like building the frame of a house, then CSS is everything that makes it feel like home - the paint colors, the furniture arrangement, the lighting, and how the rooms flow together. Think of how the Palace of Versailles started as a simple hunting lodge, but careful attention to decoration and layout transformed it into one of the world's most magnificent buildings.
Today, we'll transform your terrarium from functional to polished. You'll learn how to position elements precisely, make layouts respond to different screen sizes, and create the visual appeal that makes websites engaging.
By the end of this lesson, you'll see how strategic CSS styling can dramatically improve your project. Let's add some style to your terrarium.
mindmap
root((CSS Fundamentals))
Cascade
Specificity Rules
Inheritance
Priority Order
Conflict Resolution
Selectors
Element Tags
Classes (.class)
IDs (#id)
Combinators
Box Model
Margin
Border
Padding
Content
Layout
Positioning
Display Types
Flexbox
Grid
Visual Effects
Colors
Shadows
Transitions
Animations
Responsive Design
Media Queries
Flexible Units
Viewport Meta
Mobile First
CSS is often thought of as just "making things pretty," but it serves a much broader purpose. CSS is like being the director of a movie - you control not just how everything looks, but how it moves, responds to interaction, and adapts to different situations.
Modern CSS is remarkably capable. You can write code that automatically adjusts layouts for phones, tablets, and desktop computers. You can create smooth animations that guide users' attention where needed. The results can be quite impressive when everything works together.
š” Pro Tip: CSS is constantly evolving with new features and capabilities. Always check CanIUse.com to verify browser support for newer CSS features before using them in production projects.
Here's what we'll accomplish in this lesson:
You should have completed the HTML structure for your terrarium from the previous lesson and have it ready to be styled.
šŗ Video Resource: Check out this helpful video walkthrough
Before we can start styling, we need to connect CSS to our HTML. This connection tells the browser where to find the styling instructions for our terrarium.
In your terrarium folder, create a new file called style.css, then link it in your HTML document's <head> section:
<link rel="stylesheet" href="./style.css" />
Here's what this code does:
style.cssrel="stylesheet" attribute to specify this is a CSS filehref="./style.css"Ever wondered why CSS is called "Cascading" Style Sheets? Styles cascade down like a waterfall, and sometimes they conflict with each other.
Consider how military command structures work - a general order might say "all troops wear green," but a specific order to your unit might say "wear dress blues for the ceremony." The more specific instruction takes precedence. CSS follows similar logic, and understanding this hierarchy makes debugging much more manageable.
Let's see the cascade in action by creating a style conflict. First, add an inline style to your <h1> tag:
<h1 style="color: red">My Terrarium</h1>
What this code does:
<h1> element using inline stylingstyle attribute to embed CSS directly in the HTMLNext, add this rule to your style.css file:
h1 {
color: blue;
}
In the above, we've:
<h1> elementsā Knowledge Check: Which color displays in your web app? Why does that color win? Can you think of scenarios where you might want to override styles?
flowchart TD
A["Browser encounters h1 element"] --> B{"Check for inline styles"}
B -->|Found| C["style='color: red'"]
B -->|None| D{"Check for ID rules"}
C --> E["Apply red color (1000 points)"]
D -->|Found| F["#heading { color: green }"]
D -->|None| G{"Check for class rules"}
F --> H["Apply green color (100 points)"]
G -->|Found| I[".title { color: blue }"]
G -->|None| J{"Check element rules"}
I --> K["Apply blue color (10 points)"]
J -->|Found| L["h1 { color: purple }"]
J -->|None| M["Use browser default"]
L --> N["Apply purple color (1 point)"]
style C fill:#ff6b6b
style F fill:#51cf66
style I fill:#339af0
style L fill:#9775fa
š” CSS Priority Order (highest to lowest):
- Inline styles (style attribute)
- IDs (#myId)
- Classes (.myClass) and attributes
- Element selectors (h1, div, p)
- Browser defaults
CSS inheritance works like genetics - elements inherit certain properties from their parent elements. If you set the font family on the body element, all text inside automatically uses that same font. It's similar to how the Habsburg family's distinctive jawline appeared across generations without being specified for each individual.
However, not everything gets inherited. Text styles like fonts and colors do inherit, but layout properties like margins and borders do not. Just as children might inherit physical traits but not their parents' fashion choices.
Let's see inheritance in action by setting a font family on the <body> element:
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
Breaking down what happens here:
<body> elementOpen your browser's developer tools (F12), navigate to the Elements tab, and inspect your <h1> element. You'll see that it inherits the font family from the body:
ā
Experiment Time: Try setting other inheritable properties on the <body> like color, line-height, or text-align. What happens to your heading and other elements?
š Inheritable Properties Include:
color,font-family,font-size,line-height,text-align,visibilityNon-Inheritable Properties Include:
margin,padding,border,width,height,position
CSS Foundation Understanding: Before moving to selectors, ensure you can:
Quick Test: If you have these styles, what color will an <h1> inside a <div class="special"> be?
div { color: blue; }
.special { color: green; }
h1 { color: red; }
Answer: Red (element selector directly targets h1)
CSS selectors are your way of targeting specific elements for styling. They work like giving precise directions - instead of saying "the house," you might say "the blue house with the red door on Maple Street."
CSS provides different ways to be specific, and choosing the right selector is like choosing the appropriate tool for the task. Sometimes you need to style every door in the neighborhood, and sometimes just one specific door.
Element selectors target HTML elements by their tag name. They're perfect for setting base styles that apply broadly across your page:
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
}
h1 {
color: #3a241d;
text-align: center;
font-size: 2.5rem;
margin-bottom: 1rem;
}
Understanding these styles:
body selectorrem units for scalable, accessible font sizingWhile element selectors work well for general styling, you'll need more specific selectors to style individual components like the plants in your terrarium.
ID selectors use the # symbol and target elements with specific id attributes. Since IDs must be unique on a page, they're perfect for styling individual, special elements like our left and right plant containers.
Let's create the styling for our terrarium's side containers where the plants will live:
#left-container {
background-color: #f5f5f5;
width: 15%;
left: 0;
top: 0;
position: absolute;
height: 100vh;
padding: 1rem;
box-sizing: border-box;
}
#right-container {
background-color: #f5f5f5;
width: 15%;
right: 0;
top: 0;
position: absolute;
height: 100vh;
padding: 1rem;
box-sizing: border-box;
}
Here's what this code accomplishes:
absolute positioningvh (viewport height) units for responsive height that adapts to screen sizebox-sizing: border-box so padding is included in the total widthpx units from zero values for cleaner codeā Code Quality Challenge: Notice how this CSS violates the DRY (Don't Repeat Yourself) principle. Can you refactor it using both an ID and a class?
Improved approach:
<div id="left-container" class="container"></div>
<div id="right-container" class="container"></div>
.container {
background-color: #f5f5f5;
width: 15%;
top: 0;
position: absolute;
height: 100vh;
padding: 1rem;
box-sizing: border-box;
}
#left-container {
left: 0;
}
#right-container {
right: 0;
}
Class selectors use the . symbol and are perfect when you want to apply the same styles to multiple elements. Unlike IDs, classes can be reused throughout your HTML, making them ideal for consistent styling patterns.
In our terrarium, each plant needs similar styling but also needs individual positioning. We'll use a combination of classes for shared styles and IDs for unique positioning.
Here's the HTML structure for each plant:
<div class="plant-holder">
</div>
Key elements explained:
class="plant-holder" for consistent container styling across all plantsclass="plant" for shared image styling and behaviorid="plant1" for individual positioning and JavaScript interactionNow add these styles to your style.css file:
.plant-holder {
position: relative;
height: 13%;
left: -0.6rem;
}
.plant {
position: absolute;
max-width: 150%;
max-height: 150%;
z-index: 2;
transition: transform 0.3s ease;
}
.plant:hover {
transform: scale(1.05);
}
Breaking down these styles:
max-width and max-height propertiesz-index to layer plants above other elements in the terrariumā
Critical Thinking: Why do we need both .plant-holder and .plant selectors? What would happen if we tried to use just one?
š” Design Pattern: The container (
.plant-holder) controls layout and positioning, while the content (.plant) controls appearance and scaling. This separation makes the code more maintainable and flexible.
CSS positioning is like being the stage director for a play - you direct where every actor stands and how they move around the stage. Some actors follow the standard formation, while others need specific positioning for dramatic effect.
Once you understand positioning, many layout challenges become manageable. Need a navigation bar that stays at the top while users scroll? Positioning handles that. Want a tooltip that appears at a specific location? That's positioning too.
quadrantChart
title CSS Positioning Strategy
x-axis Document Flow --> Removed from Flow
y-axis Static Position --> Precise Control
quadrant-1 Absolute
quadrant-2 Fixed
quadrant-3 Static
quadrant-4 Sticky
Static: [0.2, 0.2]
Relative: [0.3, 0.6]
Absolute: [0.8, 0.8]
Fixed: [0.9, 0.7]
Sticky: [0.5, 0.9]
| Position Value | Behavior | Use Case |
|---|---|---|
static | Default flow, ignores top/left/right/bottom | Normal document layout |
relative | Positioned relative to its normal position | Small adjustments, creating positioning context |
absolute | Positioned relative to nearest positioned ancestor | Precise placement, overlays |
fixed | Positioned relative to viewport | Navigation bars, floating elements |
sticky | Switches between relative and fixed based on scroll | Headers that stick when scrolling |
Our terrarium uses a strategic combination of positioning types to create the desired layout:
/* Container positioning */
.container {
position: absolute; /* Removes from normal flow */
/* ... other styles ... */
}
/* Plant holder positioning */
.plant-holder {
position: relative; /* Creates positioning context */
/* ... other styles ... */
}
/* Plant positioning */
.plant {
position: absolute; /* Allows precise placement within holder */
/* ... other styles ... */
}
Understanding the positioning strategy:
šÆ Why This Matters: The
plantelements need absolute positioning to become draggable in the next lesson. Absolute positioning removes them from the normal layout flow, making drag-and-drop interactions possible.
ā Experiment Time: Try changing the positioning values and observe the results:
.container from absolute to relative?.plant-holder uses absolute instead of relative?.plant to relative positioning?CSS Positioning Mastery: Pause to verify your understanding:
Real-World Connection: Think about how CSS positioning mirrors real-world layout:
Now we'll build a glass jar using only CSS - no images or graphics software required.
Creating realistic-looking glass, shadows, and depth effects using positioning and transparency demonstrates CSS's visual capabilities. This technique mirrors how architects in the Bauhaus movement used simple geometric forms to create complex, beautiful structures. Once you understand these principles, you'll recognize the CSS techniques behind many web designs.
flowchart LR
A[Jar Top] --> E[Complete Terrarium]
B[Jar Walls] --> E
C[Dirt Layer] --> E
D[Jar Bottom] --> E
F[Glass Effects] --> E
A1["50% width
5% height
Top position"] --> A
B1["60% width
80% height
Rounded corners
0.5 opacity"] --> B
C1["60% width
5% height
Dark brown
Bottom layer"] --> C
D1["50% width
1% height
Bottom position"] --> D
F1["Subtle shadows
Transparency
Z-index layering"] --> F
style E fill:#d1e1df,stroke:#3a241d
style A fill:#e8f5e8
style B fill:#e8f5e8
style C fill:#8B4513
style D fill:#e8f5e8
Let's build the terrarium jar piece by piece. Each part uses absolute positioning and percentage-based sizing for responsive design:
.jar-walls {
height: 80%;
width: 60%;
background: #d1e1df;
border-radius: 1rem;
position: absolute;
bottom: 0.5%;
left: 20%;
opacity: 0.5;
z-index: 1;
box-shadow: inset 0 0 2rem rgba(0, 0, 0, 0.1);
}
.jar-top {
width: 50%;
height: 5%;
background: #d1e1df;
position: absolute;
bottom: 80.5%;
left: 25%;
opacity: 0.7;
z-index: 1;
border-radius: 0.5rem 0.5rem 0 0;
}
.jar-bottom {
width: 50%;
height: 1%;
background: #d1e1df;
position: absolute;
bottom: 0;
left: 25%;
opacity: 0.7;
border-radius: 0 0 0.5rem 0.5rem;
}
.dirt {
width: 60%;
height: 5%;
background: #3a241d;
position: absolute;
border-radius: 0 0 1rem 1rem;
bottom: 1%;
left: 20%;
opacity: 0.7;
z-index: -1;
}
Understanding the terrarium construction:
z-index layering so plants appear inside the jarNotice how all dimensions use percentages rather than fixed pixel values:
Why this matters:
We're using rem units for border-radius, which scale relative to the root font size. This creates more accessible designs that respect user font preferences. Learn more about CSS relative units in the official specification.
ā Visual Experimentation: Try modifying these values and observe the effects:
#3a241d to #8B4513 ā what visual impact does this have?z-index of the dirt to 2 ā what happens to the layering?CSS Visual Design Understanding: Confirm your grasp of visual CSS:
Design Principle: Notice how we're building complex visuals from simple shapes:
Use the Agent mode to complete the following challenge:
Description: Create a CSS animation that makes the terrarium plants gently sway back and forth, simulating a natural breeze effect. This will help you practice CSS animations, transforms, and keyframes while enhancing the visual appeal of your terrarium.
Prompt: Add CSS keyframe animations to make the plants in the terrarium sway gently from side to side. Create a swaying animation that rotates each plant slightly (2-3 degrees) left and right with a duration of 3-4 seconds, and apply it to the .plant class. Make sure the animation loops infinitely and has an easing function for natural movement.
Learn more about agent mode here.
Ready to enhance your terrarium with realistic glass reflections? This technique will add depth and realism to the design.
You'll create subtle highlights that simulate how light reflects off glass surfaces. This approach is similar to how Renaissance painters like Jan van Eyck used light and reflection to make painted glass appear three-dimensional. Here's what you're aiming for:
Your challenge:
border-radius to create organic, bubble-like shapesCSS can feel complex initially, but understanding these core concepts provides a solid foundation for more advanced techniques.
Your next CSS learning areas:
Practice these concepts with these engaging, hands-on games:
For comprehensive CSS fundamentals, complete this Microsoft Learn module: Style your HTML app with CSS
timeline
title CSS Learning Progression
section Foundation (10 minutes)
File Connection: Link CSS to HTML
: Understand cascade rules
: Learn inheritance basics
section Selectors (15 minutes)
Targeting Elements: Element selectors
: Class patterns
: ID specificity
: Combinators
section Box Model (20 minutes)
Layout Fundamentals: Margin and padding
: Border properties
: Content sizing
: Box-sizing behavior
section Positioning (25 minutes)
Element Placement: Static vs relative
: Absolute positioning
: Z-index layering
: Responsive units
section Visual Design (30 minutes)
Styling Mastery: Colors and opacity
: Shadows and effects
: Transitions
: Transform properties
section Responsive Design (45 minutes)
Multi-Device Support: Media queries
: Flexible layouts
: Mobile-first approach
: Viewport optimization
section Advanced Techniques (1 week)
Modern CSS: Flexbox layouts
: CSS Grid systems
: Custom properties
: Animation keyframes
section Professional Skills (1 month)
CSS Architecture: Component patterns
: Maintainable code
: Performance optimization
: Cross-browser compatibility
After completing this lesson, you now have:
Next Steps: Your terrarium now has both structure (HTML) and style (CSS). The final lesson will add interactivity with JavaScript!