Inline and Block Elements in HTML
Learn via video course
Overview
In this article, we will learn about inline and block elements in HTML, which are default display values. The most used block element tag is <div> tag and inline element tags are <a>, <span> tags.
Introduction
An element in HTML has a structure that consists of the start tag, content, and then the end tag. There are two display values - block and inline elements in HTML. Block elements in HTML begin on a new line and occupy the complete horizontal space of its parent element and have the same height as the content, for Example - the div tag. At the same time, Inline elements in HTML do not begin from a new line and occupy space as required, for Example - anchor tag.
Usage
Using this property, the element behaves as a block for its child elements.
Example
Output
From the output, we can say that an inline element, i.e., an anchor tag, is applied to a particular section while the <div> tag starts from a new line; moreover, it covers complete space from left to right.
Block Elements
Block elements begin from a new line by default and cover space to its left and right as far as it can go. The height that it covers is equal to the content height. Also, it covers the whole horizontal space of its parent element.
Example
Output
From the output, we can see that the border covers the whole space of its element, and the height that it covers is equal to the content height.
These are some supported tags of Block elements:
- <article> - Self- contained and independent content.
- <aside> - The content inside aside is often placed at the sidebar in a document.
- <div> - Container for HTML elements.
- <fieldset> - Group the same or related items.
- <figcaption> - Define the caption for <figure> element.
- <figure> - Contain content like illustrations, figures, images etc.
- <footer> - It defines the footer of the section.
- <form> - Get information from the user input.
- <h1>-<h6> - Define HTML headings, where h1 is largest and h6 is smallest.
- <header> - Container of introduction.
- <hr> - Separate content using horizontal lines.
- <li> - Add list items, ordered(<ol>) or unordered lists (<ul>).
- <main> - Add the main content we used the main tag.
- <nav> - Add navigation links.
- <section>- Add a section.
- <table> - Add a table.
<div> Element
The <div> tag is used as a container for HTML elements and is also useful for collecting large sections of HTML elements and styling them with CSS. Suppose we want to create a navigation bar containing links to different pages, and the same CSS is to be applied. Here, the <div> element plays a crucial role by making it a particular section and applying CSS to the complete section at once.
Example
Output
As we can see in the output, the div element covers complete width and height as per the content's height.
Inline Elements
Inline elements never start from a new line and only cover the width according to the size of bounded tags in the HTML element.
Example
Output As we can see in the output, the border only covers the area specified by the content bounded in the inline tag, i.e., the <span> tag, which is an inline element.
These are some Inline elements in HTML.
- <a> - It is used to link other web pages. The most important attribute of the anchor tag is the href because it indicates the destination of the link.
- <b> - It makes the text bold.
- <br> - It is used to insert a line break and has no end tag.
- <button> - To create a clickable button.
- <code> - To add computer code.
- <img> - To link image addresses.
- <input> - It is used to get user input text where users can enter data.
- <span> - To highlight a text or part of a document.
- <textarea> - It is used to get input data from users in multiline form.
<span> Element
The span tag is an inline element, and it is used to highlight a particular text or part of a document in HTML.
Example
Let's apply the span tag to highlight Scaler Academy.
Output
As we can see in the output a certain section i.e., Scaler Academy has a red color with font weight as bold.
Difference Between Inline and Block Elements in HTML
Block Elements | Inline Elements |
---|---|
Block elements always start from a new line. | Inline elements never start from a new line. |
Block elements cover space from left to right as far as it can go. | Inline elements only cover the space as bounded by the tags in the HTML element. |
Block elements have top and bottom margins. | Inline elements don't have a top and bottom margin. |
Examples of block elements - <p>,<div>,<hr> . | Examples of inline elements - <span>,<br> |
|
Supported Browser
Inline and Block elements support the following browsers -
- Google Chrome 93.0
- Mozilla Firefox 91.0
- Microsoft Edge 93.0
- IE 11.0
- Safari 14.1
- Opera 78.0
Summary
- Block elements start from a new line and cover complete space as per the content. Moreover, it covers the whole horizontal space.
- Inline elements cover only the area which is bounded by the tags in the HTML element and never start from the new line.
- The <div> tag is a block element. It is used as a section in the HTML page to group all large sections of HTML elements.
- The <span> tag is an inline element. It is used as a container to highlight or markup particular text or part of a document.