Class 8 - CSS Intro

CSS Foundations

Introduction to CSS 🎨

CSCI 3403 - Web Programming

Week 4, Class 8

Time to make the web beautiful! ✨

⚠️ IMPORTANT REMINDER ⚠️

Recipe Project Due TONIGHT!

Submit your GitHub link on D2L by end of day

  • At least 3 recipe pages
  • Proper HTML structure
  • All links working
  • Images displaying correctly

Quick Q&A: Any last-minute Recipe Project questions before we dive into CSS?

The Journey So Far πŸ—ΊοΈ

What we've built:

The problem:

Our websites work but... they look like they're from 1995! πŸ˜…

Today's Solution: CSS - Making HTML look amazing!

Today's Learning Objectives

What is CSS? πŸ€”

Cascading Style Sheets

HTML (Structure)

The skeleton 🦴

  • Content
  • Headings
  • Paragraphs
  • Links

CSS (Style)

The skin & clothes πŸ‘—

  • Colors
  • Fonts
  • Spacing
  • Layout

CSS describes how HTML elements should be displayed

Life Without CSS vs With CSS

Without CSS 😒

  • Plain black text
  • Times New Roman everywhere
  • Blue underlined links
  • No layout control
  • Looks like 1990s web

With CSS πŸŽ‰

  • Beautiful colors
  • Custom fonts
  • Styled buttons
  • Professional layouts
  • Modern, responsive design

Fun Fact: CSS Zen Garden shows the same HTML with different CSS - mind-blowing!

CSS Basic Syntax πŸ“

/* This is a CSS rule */
selector {
  property: value;
  property: value;
}

Real Example:

p {
  color: red;
  font-size: 16px;
}

CSS Syntax Rules ⚠️

Remember These!

/* Good - clean and readable */
h1 {
  color: blue;
  font-size: 24px;
}

/* Also valid but harder to read */
h1{color:blue;font-size:24px;}

Universal Selector *️⃣

Selects EVERYTHING on the page!

* {
  color: purple;
  margin: 0;
}

When to use:

Warning: The universal selector can slow down your page if overused!

Type Selectors 🏷️

Select all elements of a specific HTML type

/* Selects ALL paragraphs */
p {
  color: blue;
}

/* Selects ALL h1 headings */
h1 {
  font-size: 32px;
}

/* Selects ALL divs */
div {
  background-color: gray;
}

Note: Type selector = just the HTML tag name, no brackets!

Class Selectors 🎯

Target elements with a specific class attribute

/* HTML */
<p class="highlight">Important text</p>
<p>Normal text</p>
<p class="highlight">Also important</p>

/* CSS - note the period! */
.highlight {
  background-color: yellow;
}

Key Points:

Multiple Classes on One Element

/* HTML - space-separated classes */
<p class="alert-text severe-alert">Warning!</p>

/* CSS */
.alert-text {
  color: red;
}

.severe-alert {
  font-weight: bold;
}

This paragraph gets BOTH styles applied!

Pro Tip: Use hyphens for multi-word class names (not spaces!)

ID Selectors πŸ†”

Target ONE specific element

/* HTML */
<div id="header">Site Header</div>
<div id="footer">Site Footer</div>

/* CSS - note the hashtag! */
#header {
  background-color: navy;
}

#footer {
  background-color: gray;
}

Remember: IDs must be unique! Only ONE element per ID on a page!

Class vs ID: When to Use Which?

Classes (.)

  • Can use on multiple elements
  • For reusable styles
  • Most common choice
  • Example: .button, .card, .highlight

IDs (#)

  • Only one per page
  • For unique elements
  • Use sparingly
  • Example: #header, #main-nav, #footer

Best Practice: Default to classes. Use IDs only when truly needed!

Grouping Selectors πŸ‘₯

Apply same styles to multiple selectors

/* Instead of repeating... */
h1 { color: blue; }
h2 { color: blue; }
h3 { color: blue; }

/* Group with commas! */
h1, h2, h3 {
  color: blue;
}

Benefits: Less repetition, easier to maintain, cleaner code!

Chaining Selectors ⛓️

Target elements that match ALL specified criteria

/* HTML */
<p class="highlight">Text 1</p>
<p class="highlight warning">Text 2</p>

/* CSS - NO SPACE between selectors */
.highlight.warning {
  background-color: orange;
  font-weight: bold;
}

Only "Text 2" gets the orange background (has BOTH classes)

Remember: No space = chaining, Space = descendant (we'll learn next!)

Descendant Combinator πŸ‘¨β€πŸ‘§

Select elements nested inside other elements

/* HTML */
<div class="container">
  <p>This paragraph is inside</p>
</div>
<p>This paragraph is outside</p>

/* CSS - SPACE between selectors */
.container p {
  color: red;
}

Only the paragraph INSIDE .container turns red!

Think: "Find all p elements that are descendants of .container"

Quick Selector Practice 🧠

What would these select?

1. p
2. .warning
3. #header
4. div p
5. .alert.urgent
6. h1, h2, h3
Click for Answers
  1. All paragraph elements
  2. Elements with class "warning"
  3. Element with id "header"
  4. Paragraphs inside divs
  5. Elements with BOTH alert AND urgent classes
  6. All h1, h2, and h3 elements

Color Properties 🎨

p {
  /* Text color */
  color: red;
  
  /* Background color */
  background-color: yellow;
}

Color Values:

/* Named colors */
color: red;

/* Hex codes */
color: #FF0000;

/* RGB */
color: rgb(255, 0, 0);

/* HSL */
color: hsl(0, 100%, 50%);

Typography Basics πŸ“

p {
  /* Font family with fallbacks */
  font-family: "Helvetica", Arial, sans-serif;
  
  /* Size (no space before unit!) */
  font-size: 16px;
  
  /* Weight (boldness) */
  font-weight: bold; /* or 700 */
  
  /* Alignment */
  text-align: center;
}

Pro Tip: Always include fallback fonts in case the first isn't available!

Image Dimensions πŸ“

img {
  /* Maintain aspect ratio */
  width: 300px;
  height: auto;
}

/* Or set both explicitly */
img {
  width: 300px;
  height: 200px;
}

Important: Setting both width and height can distort images! Use 'auto' for one dimension to maintain proportions.

Three Ways to Add CSS to HTML

  1. πŸ”— External CSS (Recommended!)
  2. πŸ“„ Internal CSS (Sometimes useful)
  3. 🏷️ Inline CSS (Avoid if possible)

Let's explore each method...

External CSS πŸ”—

The BEST method - CSS in a separate file!

<!-- index.html -->
<head>
  <link rel="stylesheet" href="styles.css">
</head>
/* styles.css */
p {
  color: blue;
}

βœ… Advantages:

Internal CSS πŸ“„

CSS inside the HTML file's <head>

<head>
  <style>
    p {
      color: blue;
    }
  </style>
</head>

When to use:

Downside: Can't reuse styles across multiple pages

Inline CSS 🏷️

CSS directly on HTML elements

<p style="color: blue; font-size: 16px;">Text</p>

❌ Problems:

Best Practice: Avoid inline CSS except for quick testing!

Let's Practice! πŸ’»

Exercise Time

We'll work through exercises from The Odin Project:

  1. 01-css-methods
  2. 02-class-id-selectors
  3. 03-grouping-selectors
  4. 04-chaining-selectors
  5. 05-descendant-combinator

We'll do these together - follow along!

Note: We might not finish all today - that's okay! We'll continue next class.

Setting Up the Exercises

# Clone the repository
git clone https://github.com/TheOdinProject/css-exercises.git

# Navigate to the CSS foundations
cd css-exercises/foundations/intro-to-css

# Open in VS Code
code .

For Each Exercise:

  1. Read the README file
  2. Open index.html in browser
  3. Edit the CSS file
  4. Refresh browser to see changes
  5. Compare with solution when done

Today's Key Takeaways 🎯

CSS Fundamentals:

Remember:

Homework πŸ“š

For Next Class:

  1. TONIGHT: Submit Recipe Project on D2L!
  2. Finish any CSS exercises we didn't complete
  3. Read about CSS Cascade and Specificity
  4. Try adding some basic CSS to your Recipe Project (optional)

Next Class: The Cascade - How CSS decides which styles win!

CSS is Your Creative Superpower! πŸš€

You Can Now:

Your websites are about to get a MAJOR upgrade! 🎨

Don't forget: Recipe Project due TONIGHT!