HTML Block and Inline Elements
Block and Inline Elements
All the HTML elements can be categorized into two main categories:
-
Block-level elements:
- These elements take up the full width of their parent container and start on a new line.
- Block-level elements are used to define larger structures and sections of a web page. They typically contain other block-level or inline elements.
-
Examples:
<div>,<p>,<header>,<footer>,<section>,<article>,<nav>,<ul>,<li>,<table>, etc.
-
Inline elements:
- In HTML, inline elements are elements that do not start on a new line and only take up as much width as necessary. They are typically used for styling parts of a text or grouping small chunks of content within a line.
- These elements only take up as much width as necessary for their content and do not force a new line after them.
- Inline elements are generally used within block-level elements to define smaller parts of content such as links, text formatting, and images.
- Examples:
<a>,<span>,<img>,<strong>,<em>,<code>, etc.
Together, these two types of elements help create the layout and structure of a web page, with block-level elements providing the overall structure and inline elements allowing for finer details and formatting within that structure.
Block-level Elements
| HTML Block Elements | ||||
|---|---|---|---|---|
| <blockquote> | <canvas> | <address> | <article> | <aside> |
| <figcaption>> | <figure> | <footer> | <form> | <h1> - <h6> |
| <dd> | <div> | <dl> | <dt> | <fieldset> |
| <header> | <hr> | <li> | <main> | <nav> |
| <noscript> | <ol> | <p> | <pre> | <section> |
| <table> | <tfoot> | <ul> | <video> | |
Inline Elements
In HTML, inline elements are elements that do not start on a new line and only take up as much width as necessary. They are typically used for styling parts of a text or grouping small chunks of content within a line.
Characteristics of Inline Elements:
- Do Not Start on a New Line: Inline elements remain "in line" with the surrounding content.
- Width and Height: They only occupy the space their content requires, ignoring width and height properties unless explicitly set with CSS.
- Used Within Block Elements: Inline elements are usually placed inside block-level elements.
Common Inline Elements
Here’s a list of frequently used inline elements in HTML:
Text Formatting
<b>: Bold text.<i>: Italic text.<strong>: Important (bold by default).<em>: Emphasized text (italic by default).<u>: Underlined text.<span>: Generic inline container for applying styles or grouping text.
Hyperlinks and Navigation
<a>: Anchor (hyperlink).
Media
<img>: Inline image.<audio>(with controls): Embedded audio content.<video>(with controls): Embedded video content.<iframe>: Inline frame for embedding another webpage.
Programming and Technical Elements
<code>: Inline code snippet.<kbd>: Text representing user input (e.g., keyboard input).<samp>: Sample output from a program.<var>: Variable name.
Other Inline Elements
<abbr>: Abbreviation.<cite>: Citation.<mark>: Highlighted text.<time>: Time or date.<small>: Smaller text.<sup>: Superscript text.<sub>: Subscript text.<q>: Inline quotation.
Differences Between Inline and Block Elements
| Aspect | Inline | Block |
|---|---|---|
| Display | Inline with other elements. | Starts on a new line. |
| Width | Only as wide as the content. | Expands to the parent width. |
| Height | Set by the content. | Can be set explicitly. |
Example: Inline Elements in Action
In this example, the <b>, <i>, and <a> elements are inline and flow naturally within the paragraph without breaking the line.
<p>
This is <b>bold </b>, <i>italic </i>, and <a href="#">a link </a>.
</p>
Prefer Learning by Watching?
Watch these YouTube tutorials to understand HTML Tutorial visually:
What You'll Learn:
- 📌 HTML Block vs Inline Elements
- 📌 HTML Block and Inline Elements Tutorial