Day 12 - Block and Inline

CSS Foundations

Block and Inline Elements

CSCI 3403 - Web Programming

Day 12 - Understanding Display Types

Today's Agenda 📋

Learning Objectives 🎯

By the end of this lesson, you will:

Quick Review: Box Model

Remember from last lesson:

Today: How boxes interact with each other!

Normal Flow

The default way browsers lay out HTML elements without any CSS positioning.

Key Concepts:

Think of it as: How elements "naturally" arrange themselves on the page

The Display Property

Controls how an element behaves in the document flow

/* Common display values */ display: block; display: inline; display: inline-block; display: none; display: flex; /* Coming next lesson! */

Today we focus on the first three!

Block Elements 📦

Characteristics:

/* Default behavior */ display: block;

Common Block Elements

Structural:

  • <div>
  • <section>
  • <article>
  • <header>
  • <footer>
  • <main>
  • <nav>

Content:

  • <h1> - <h6>
  • <p>
  • <ul>, <ol>, <li>
  • <form>
  • <table>
  • <hr>

Block Elements Visualization

<div> Block element 1 </div>
<p> Block element 2 </p>
<h3> Block element 3 </h3>

Notice how each element:

Inline Elements ➡️

Characteristics:

/* Default behavior */ display: inline;

Common Inline Elements

Inline Elements Visualization

This is a paragraph with an inline span and another span that flow with the text. Here's a link too!

Notice how inline elements:

Block vs Inline: Key Differences

Property Block Inline
Line breaks Yes (before & after) No
Width 100% by default Content-based
Can set width/height? Yes No
Margin/Padding All sides work Horizontal only*

*Vertical padding/margin on inline elements can cause overlap issues

⚠️ Inline Elements: Padding & Margin

Important: Vertical padding and margin on inline elements don't push other elements away!
/* This can cause overlapping! */ span { padding: 20px 10px; /* Vertical padding may overlap */ margin: 20px 10px; /* Vertical margin often ignored */ }
Best Practice: Avoid vertical spacing on inline elements. Use inline-block if needed!

Inline-Block: The Middle Ground 🎯

Combines features of both block and inline elements!

Characteristics:

display: inline-block;

Inline-Block Example

Box 1
Box 2
Box 3
.box { display: inline-block; width: 100px; height: 100px; margin: 10px; padding: 20px; }

Note: While useful, flexbox (next lesson) is often better for layouts!

All Three Display Types

Block: Takes full width
Inline: Flows with text
Inline-block:
Best of both

Choose based on your layout needs!

The <div> Element

The generic block container

Characteristics:

<div class="container"> <div class="header">...</div> <div class="content">...</div> </div>

The <span> Element

The generic inline container

Characteristics:

<p>This text has a <span class="highlight">highlighted</span> word.</p>

When to Use Divs vs Spans

Use <div> when:

  • Creating layout sections
  • Grouping block elements
  • Need a container for styling
  • Building card components

Use <span> when:

  • Styling part of text
  • Adding inline icons
  • Applying CSS to words
  • No semantic element fits
Remember: Use semantic HTML first! Only use div/span when no semantic element applies.

Practical Example: Navigation

<nav> <a href="#">Home</a> <a href="#">About</a> <a href="#">Contact</a> </nav> /* Make links display as blocks */ nav a { display: inline-block; padding: 10px 20px; margin: 5px; background: #667eea; color: white; }

Inline-block perfect for horizontal navigation!

Practical Example: Card Layout

<div class="card"> <h2>Card Title</h2> <p>Card content with <span class="badge">NEW</span> badge</p> <button>Learn More</button> </div> .card { display: block; } /* Container */ .badge { display: inline; } /* Flows with text */ button { display: inline-block; } /* Respects padding */

Overriding Default Display

You can change any element's display property!

/* Make a span act like a block */ span.block-span { display: block; } /* Make a div act inline */ div.inline-div { display: inline; } /* Hide an element completely */ .hidden { display: none; }
Note: Changing display doesn't change semantic meaning!

Common Gotchas 🤔

Quick Check! 🎯

What display type would you use for:

  1. A navigation bar with horizontal links?
  2. A container for your page content?
  3. Highlighting a word in a paragraph?
  4. Creating a grid of cards?
  5. Adding an icon next to text?

Answers: 1) inline-block, 2) block, 3) inline, 4) inline-block (or flex next week!), 5) inline

Best Practices

Resources for Practice

Required Reading:

Optional:

Homework Assignment 📚

Part 1: CSS Exercises

Complete from The Odin Project repository:

GitHub: TheOdinProject/css-exercises/foundations/block-and-inline

Part 2: Style Your Recipe Page

Key Takeaways

Next lesson: Flexbox - Modern Layouts! 🚀

Questions?

Let's practice together!

Remember: The best way to learn CSS is by doing