📅 Week 2 - Day 2: Introduction to CSS

Wednesday | Duration: 3 hours

🎯 Learning Objectives

  • Understand what CSS is and its purpose
  • Learn CSS syntax, selectors, and properties
  • Apply inline, internal, and external CSS
  • Use Chrome DevTools for live CSS editing
  • Style the news page from Day 1

🎨 What is CSS?

CSS (Cascading Style Sheets) is a stylesheet language used to describe the presentation of HTML documents.

CSS controls:

  • Colors and backgrounds
  • Fonts and text styling
  • Spacing (margins, padding)
  • Layout and positioning
  • Animations and transitions

Why Use CSS?

  • Separation of concerns: Keep content (HTML) separate from presentation (CSS)
  • Consistency: Apply same styles across multiple pages
  • Maintainability: Change design without touching HTML
  • Efficiency: One CSS file can style entire website

📝 CSS Syntax

selector { property: value; property: value; }

Example:

h1 { color: blue; font-size: 32px; text-align: center; }
  • Selector: What element to style (h1)
  • Property: What aspect to change (color)
  • Value: The specific setting (blue)

🔗 Three Ways to Add CSS

1. Inline CSS (Not Recommended)

Applied directly to an element using the style attribute.

<p style="color: red; font-size: 20px;">This is red text.</p>

⚠️ Avoid inline CSS - It's hard to maintain and mixes content with presentation.

2. Internal CSS (For Single Pages)

Written in the <style> tag within the <head> section.

<head> <style> body { background-color: #f0f0f0; } h1 { color: navy; } </style> </head>

3. External CSS (Best Practice) ✅

Written in a separate .css file and linked to HTML.

<!-- In HTML file --> <head> <link rel="stylesheet" href="style.css"> </head>
/* In style.css file */ body { font-family: Arial, sans-serif; margin: 0; } h1 { color: #333; }

🎯 CSS Selectors

1. Element Selector

p { color: black; }

Selects all <p> elements.

2. Class Selector (.)

.highlight { background-color: yellow; }
<p class="highlight">This paragraph is highlighted.</p>

3. ID Selector (#)

#header { background-color: navy; color: white; }
<div id="header">Header content</div>

💡 Classes vs IDs:

  • Use classes for multiple elements
  • Use IDs for unique elements (only once per page)

4. Multiple Selectors

h1, h2, h3 { color: blue; font-family: Arial; }

5. Descendant Selector

article p { line-height: 1.6; }

Selects all <p> elements inside <article>.

🎨 Common CSS Properties

Text Styling

Property Values Example
color red, #ff0000, rgb(255,0,0) color: blue;
font-size 16px, 1.5em, 100% font-size: 20px;
font-family Arial, serif, sans-serif font-family: Arial;
font-weight normal, bold, 700 font-weight: bold;
text-align left, center, right, justify text-align: center;
text-decoration underline, none, line-through text-decoration: none;

Background Properties

body { background-color: #f5f5f5; background-image: url('background.jpg'); background-size: cover; background-position: center; }

Colors in CSS

Named Colors: red

Hex Colors: #3498db

RGB: rgb(52, 152, 219)

RGBA (with transparency): rgba(52, 152, 219, 0.5)

Spacing: Margin and Padding

div { margin: 20px; /* Space outside element */ padding: 15px; /* Space inside element */ } /* Individual sides */ div { margin-top: 10px; margin-right: 20px; margin-bottom: 10px; margin-left: 20px; } /* Shorthand */ div { margin: 10px 20px; /* top/bottom left/right */ margin: 10px 20px 15px 25px; /* top right bottom left */ }

Borders

div { border: 2px solid black; border-radius: 10px; /* Rounded corners */ } /* Individual properties */ div { border-width: 3px; border-style: dashed; border-color: red; }

🛠️ Chrome DevTools for CSS

How to Use DevTools:

  1. Right-click on any element → "Inspect"
  2. Or press F12 or Ctrl+Shift+I
  3. Select the Elements tab
  4. View and edit CSS in real-time
  5. See the box model (margin, padding, border)

💡 Pro Tip: Changes in DevTools are temporary - copy them to your CSS file!

💻 Main Activity: Style Your News Page

Task: Create style.css and link to news-article.html

Create a new file: C:\xampp\htdocs\web_basics\css\style.css

Step 1: Link CSS to HTML

Add this in the <head> of news-article.html:

<link rel="stylesheet" href="css/style.css">

Step 2: Create style.css

/* Reset default styles */ * { margin: 0; padding: 0; box-sizing: border-box; } /* Body styling */ body { font-family: 'Segoe UI', Arial, sans-serif; line-height: 1.6; color: #333; background-color: #f4f4f4; } /* Header styling */ header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 20px; text-align: center; } header h1 { font-size: 2.5em; margin-bottom: 10px; } /* Navigation */ nav { background-color: #333; padding: 10px; } nav a { color: white; text-decoration: none; padding: 10px 20px; display: inline-block; transition: background 0.3s; } nav a:hover { background-color: #555; } /* Main content */ main { max-width: 1200px; margin: 20px auto; padding: 20px; background: white; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); } /* Article styling */ article { margin-bottom: 30px; } article h2 { color: #667eea; font-size: 2em; margin-bottom: 15px; } article h3 { color: #555; margin-top: 20px; margin-bottom: 10px; border-bottom: 2px solid #667eea; padding-bottom: 5px; } article p { margin-bottom: 15px; text-align: justify; } /* Images */ figure { margin: 20px 0; } figure img { width: 100%; height: auto; border-radius: 8px; } figcaption { font-style: italic; color: #666; margin-top: 10px; text-align: center; } /* Blockquote */ blockquote { background: #f9f9f9; border-left: 5px solid #667eea; padding: 15px 20px; margin: 20px 0; font-style: italic; } blockquote footer { margin-top: 10px; font-size: 0.9em; color: #666; } /* Lists */ ul { margin-left: 30px; margin-bottom: 15px; } li { margin-bottom: 8px; } /* Sidebar */ aside { background: #f9f9f9; padding: 20px; border-radius: 8px; margin-top: 30px; } aside h3 { color: #667eea; margin-bottom: 15px; } /* Footer */ footer { background: #333; color: white; text-align: center; padding: 20px; margin-top: 30px; } footer a { color: #667eea; text-decoration: none; } footer a:hover { text-decoration: underline; } /* Video and Audio */ video, audio { width: 100%; max-width: 640px; border-radius: 8px; margin: 15px 0; }

✏️ Practice Exercises

Exercise 1: Experiment with Colors

Change the following in your CSS:

  • Try different background colors for header
  • Change text colors
  • Add a gradient background to body
  • Use rgba() for transparent effects

Exercise 2: Typography

  • Change font families (try: 'Georgia', 'Courier New', 'Times New Roman')
  • Adjust font sizes
  • Add letter-spacing and line-height
  • Experiment with font-weight

Exercise 3: Spacing and Borders

  • Add margins and padding to different elements
  • Create bordered boxes with border-radius
  • Add box-shadow to elements
/* Example */ .card { padding: 20px; margin: 15px; border: 1px solid #ddd; border-radius: 8px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }

Exercise 4: Style Your Profile Page

Go back to profile.html from Week 1 and create a separate CSS file for it.

  • Style headings with colors
  • Add borders and padding to sections
  • Style links (change color, remove underline, add hover effect)
  • Make the profile image circular
/* Circular image */ img { border-radius: 50%; border: 3px solid #667eea; }

📝 CSS Cheat Sheet

Common Properties Quick Reference:

/* Text */ color: blue; font-size: 16px; font-weight: bold; text-align: center; text-decoration: underline; line-height: 1.5; /* Background */ background-color: #f0f0f0; background-image: url('image.jpg'); /* Spacing */ margin: 10px; padding: 15px; /* Border */ border: 1px solid black; border-radius: 5px; /* Display */ display: block; display: inline; display: none; /* Size */ width: 100%; height: 200px; max-width: 1200px;

✅ Today's Deliverable

  • style.css file with comprehensive styling
  • Styled news-article.html page
  • Styled profile.html page with separate CSS
  • Screenshots of before and after styling
  • Committed and pushed to GitHub with message: "Add CSS styling"