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/elementsPadding- Space inside the borderBorder- Edge around paddingMargin- 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 typeshelps you structure content effectively.</div></div>
→
[Well-structured article with proper inline/block usage]Your Task: Element Types Demo
Create a page demonstrating:
- Paragraph with
spanandstronginline elements - Container
divwith nested content - CSS styling showing box model differences
- Use of
emfor 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
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/elementsPadding- Space inside the borderBorder- Edge around paddingMargin- 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 typeshelps you structure content effectively.</div></div>
→
[Well-structured article with proper inline/block usage]Your Task: Element Types Demo
Create a page demonstrating:
- Paragraph with
spanandstronginline elements - Container
divwith nested content - CSS styling showing box model differences
- Use of
emfor emphasis
HTML Editor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!-- Task: Compare inline and block elements -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Element Types Demo</title>
<style>
.demo-box {
background: lightblue;
padding: 10px;
margin: 5px;
}
</style>
</head>
<body>
<!-- Add: -->
<!-- - Paragraph with <span> and <strong> inline elements -->
<!-- - Container <div> with nested content -->
<!-- - Show box model with CSS styling -->
</body>
</html>