HTML Block & Inline Elements
π Understanding Block vs Inline Elements in HTML
In HTML, every element is either a block-level or an inline-level element. This classification affects how elements behave in the page layout.
>>"Block and inline are not just styles β they define how content flows on the web." π
π§± What is a Block-Level Element?
Block-level elements occupy the full width of their parent container and start on a new line. They are typically used to structure the main layout of a page.
Block-Level Element Example
<div>This is a block element</div>
<p>This is another block element</p>
<h1>This is a heading</h1>π Common Block Elements
- <div> β Generic container
- <p> β Paragraph
- <h1> to <h6> β Headings
- <ul>, <ol>, <li> β Lists
- <section>, <article>, <footer> β Semantic layout tags
Note
β οΈ Block elements can contain other block or inline elements.
π What is an Inline Element?
Inline elements do not start on a new line. They only take up as much width as necessary and flow with surrounding text.
Inline Element Example
<p>This is <span>inline text</span> inside a paragraph.</p>
<a href="#">This is a link</a>π§Ύ Common Inline Elements
- <span> β Generic inline container
- <a> β Anchor (link)
- <strong>, <em> β Formatting tags
- <img> β Image (inline by default)
- <br> β Line break
Note
π‘ Inline elements cannot contain block-level elements.
βοΈ Visual Difference
Here's a quick comparison of how block and inline elements behave:
| Property | Block | Inline |
|---|---|---|
| Starts on new line? | β Yes | β No |
| Takes full width? | β Yes | β No |
| Can contain block? | β Yes | β No |
| Can contain inline? | β Yes | β Yes |
π¨ Can You Change It?
Yes! Using CSS, you can change how elements behave:
Changing Display Type with CSS
span {
display: block;
}
div {
display: inline;
}Note
π§ Use display: inline-block to combine features of both block and inline elements.
π Learn More
>>βUnderstanding layout starts with mastering block and inline.β β Web Wisdom π§±β¨