Difference between block elements and inline elements
The inline and block elements of HTML are one of the important areas where web developers often get confused because they were unable to know which are inline and block elements which may cause clumsiness in a webpage in case he assumes some element to be a block but it is an inline element which causes next element comes next to it.
So let us see the differences between the inline and block elements in HTML and the different frequently used inline and block HTML elements.
Block elements: They consume the entire width available irrespective of their sufficiency. They always start in a new line and have top and bottom margins. It does not contain any other elements next to it.
Examples of Block elements:
- <h1>-<h6> : This element is used for including headings of different sizes ranging from 1 to 6.
- <div>: This is a container tag and is used to make separate divisions of content on the web page.
- <hr>: This is an empty tag and is used for separating content by horizontal lines.
- <li>: This tag is used for including list items of an ordered or unordered list.
- <ul>: This tag is used to make an unordered list.
- <ol>: This tag is used to make an ordered list.
- <p>: This tag is used to include paragraphs of content in the webpage.
- <table>: This tag is used for including the tables in the webpage when there is a need for tabular data.
HTML 5 Semantic block elements:
- <header>: This tag is used for including all the main things of the webpage like navbar, logos, and heading of the webpage.
- <nav>: This tag helps to navigate through different sections by including different blocks of hyperlinks in the webpage.
- <footer>: This contains all information about the authorization, contact, and copyright details of the webpage.
- <main>: The main content of the webpage resides in this tag.
- <section> : This is used separate different sections in the webpage.
- <article>: This tag is used to include different independent articles on the webpage.
- <aside>: This tag is used to mention details of the main content aside.
Output: From the above output, 3 different block elements with different background colour and a border are used to show how the block elements occupy the whole width and the margin they leave. Three-block elements <h1>,<p>,<div> are used in the above output.
Inline elements: Inline elements occupy only enough width that is sufficient to it and allows other elements next to it which are inline. Inline elements don’t start from a new line and don’t have top and bottom margins as block elements have.
Examples of Inline elements:
- <a>: This tag is used for including hyperlinks in the webpage.
- <br>: This tag is used for mentioning line breaks in the webpage where ever needed.
- <input>: This tag is used for taking input from the users and is mainly used in forms.
- <img>: This tag is used for including different images in the webpage to add beauty to the webpage.
- <span>: This is an inline container that takes necessary space only.
- <b>: This tag is used in places where bold text is needed.
- <label>: The tag in HTML is used to provide a usability improvement for mouse users i.e, if a user clicks on the text within the <label> element, it toggles the control.
From the above output, three different inline elements are used is <span>, <b>,<a> that occupied only enough width and allowed other elements to settle at their next.
Difference between Inline and Block elements:
Inline Elements Block Elements Inline elements occupy only sufficient width required. Block Elements occupy the full width irrespective of their sufficiency. Inline elements don’t start in a new line. Block elements always start in a line. Inline elements allow other inline elements to sit behind. Block elements doesn’t allow other elements to sit behind Inline elements don’t have top and bottom margin Block elements have top and bottom margin.