Inline vs. Block Elements

Understanding the difference between inline and block elements is fundamental to HTML layout and CSS styling.

Block Elements

<div>Block element takes full width</div>
<div>Each div starts on a new line</div>
<p>Paragraphs are also block elements</p>
<h1>Headings too!</h1>
[Each element on its own line, full width]

Block element characteristics:

  • Start on a new line
  • Take up full available width
  • Can contain other block and inline elements
  • Examples: div, p, h1-h6, ul, li

Inline Elements

<p>
This is <span>inline text</span> that flows
<strong>naturally</strong> with the
<em>surrounding content</em>.
</p>
[Text flows in a line with inline styling]

Inline element characteristics:

  • Flow within text content
  • Only take up necessary width
  • Cannot contain block elements
  • Examples: span, strong, em, a, img

CSS Box Model Basics

<style>
.box-demo {
width: 200px;
padding: 20px;
border: 5px solid blue;
margin: 10px;
background: lightgray;
}
</style>

<div class="box-demo">Content</div>
[Box with margin → border → padding → content]

Box model components:

  • Content - The actual text/elements
  • Padding - Space inside the border
  • Border - Edge around padding
  • Margin - Space outside the border

Styling Differences

<style>
.block-style {
width: 300px; /* Works on block */
height: 100px; /* Works on block */
margin: 20px; /* All sides work */
}
.inline-style {
width: 300px; /* Ignored on inline */
height: 100px; /* Ignored on inline */
margin: 20px 0; /* Top/bottom ignored */
}
</style>
[Block elements respect all dimensions, inline elements don't]

Common Errors

<!-- Wrong: Block inside inline -->
<span><div>Not allowed!</div></span>

<!-- Wrong: Expecting inline to have block behavior -->
<span style="width: 200px;">Won't work</span>

<!-- Correct: Proper nesting -->
<div><span>This works fine</span></div>

Practical Use: Content Layout

<div class="article">
<h2>Article Title</h2>
<p>
This article discusses <strong>important concepts</strong>
and includes <em>emphasized points</em>. You can
<span class="highlight">highlight specific text</span>
within the flow of content.
</p>
<div class="callout">
<strong>Key Takeaway:</strong> Understanding element types
helps you structure content effectively.
</div>
</div>
[Well-structured article with proper inline/block usage]

Your Task: Element Types Demo

Create a page demonstrating:

  1. Paragraph with span and strong inline elements
  2. Container div with nested content
  3. CSS styling showing box model differences
  4. Use of em for emphasis
Section 1/7Block Elements

Block Elements

Block element characteristics:Start on a new lineTake up full available widthCan contain other block and inline elementsExamples: div, p, h1-h6, ul, li

Inline vs. Block Elements

Understanding the difference between inline and block elements is fundamental to HTML layout and CSS styling.

Block Elements

<div>Block element takes full width</div>
<div>Each div starts on a new line</div>
<p>Paragraphs are also block elements</p>
<h1>Headings too!</h1>
[Each element on its own line, full width]

Block element characteristics:

  • Start on a new line
  • Take up full available width
  • Can contain other block and inline elements
  • Examples: div, p, h1-h6, ul, li

HTML Editor

Preview