HTML
π HTML Block and Inline Elements
Overview
In HTML, every tag has a default behavior that determines how it appears in the browser. This behavior defines how the element occupies space on the page.
The two most common types are :
- Block (block-level elements)
- Inline (inline elements)
1. Block-level Elements
Features :
- Always start on a new line
- Take up the full width of the available space
- Usually create space above and below
- Can contain both block and inline elements
Example Tags :
-
<p>β paragraph -
<div>β general-purpose container -
<h1>-<h6>β headings
Example Tags :
HTML
<p style="border:1px solid black;">Hello World (p tag)</p>
<div style="border:1px solid black;">Hello World (div tag)</div>
π These two elements appear one below the other because they are block-level and take up the full width.
2 Inline Elements
Features :
- Do not start on a new line, continue on the same line
- Only take up as much width as their content
- Typically used to style small parts within text
- Should not contain block-level elements
Example Tags :
-
<span>β used to style/select part of text -
<a>β link -
<strong>-<h6>β bold text -
<em>β italic text -
<img>β image
Example Tags :
HTML
<p>This is a<span style="color:blue; font-weight:bold;">blue and bold</span> word.</p>
π Here, <span> only affects the word and does not break the line.
3 Difference Between <div> and <span>
-
<div>β a block-level container -
<span>β an inline container
Both do not carry meaning on their own and are usually used for styling with CSS.
In short :
- Organizing large sections β
<div> - Styling small parts of tex β
<span>
4 Changing Behavior with CSS (display)
You can change the default behavior of an element using CSS.
Example :
HTML
a{
display: block;
}
π Normally, the <a> tag is inline, but with this code, it behaves like a block element.
