Table of contents
1.
Introduction
2.
Block Elements in HTML
2.1.
Program 1
2.2.
Output
2.3.
Program 2
2.4.
Output
3.
Supported Tags
4.
Inline elements in HTML
4.1.
Program 1
4.2.
Output
4.3.
Program 2
4.4.
Output
5.
Supported Tags
6.
Block and Inline elements in HTML
6.1.
Program
6.2.
Output
7.
Frequently Asked Questions
7.1.
What are HTML elements?
7.2.
How are elements different from attributes in HTML?
7.3.
What is the inline code in HTML?
7.4.
Is img inline or block?
7.5.
What is the purpose of elements in HTML?
7.6.
Why do we need to close the tag in HTML?
8.
Conclusion
Last Updated: Sep 8, 2024
Easy

Block and Inline Elements in HTML

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In this blog, we will be diving into the concept of block and inline elements in HTML. Before HTML5, HTML elements were either block or inline elements.

Block and Inline Elements in HTML

Under the traditional content model structure, all elements fall into two categories: block or inline. Block elements render on the new line by default. This is reversed for inline elements, which default render on the same line.

Block Elements in HTML

Block elements render on the new line by default. A Block-level element occupies the whole width of its container or, in other words, the entire horizontal space of its parent element.

visual representation


Some of the used block elements are:

<article> <div> <figure> <form> <h1> <h2> <h3> <h4> <h5> <h6> <header> <li> <main> <nav><ol> <p> <pre> <section> <table> <ul>

The two commonly used block elements are <p> and <div>. The <p> element defines a paragraph. The <div> element defines a division. Let's take an example and see how these block-level elements work.

Program 1

<html>
	<style>
		.mark {
			background-color: #fff300;
			color: black;
		}
	</style>
	<head>
		<title>Block-level elements</title>
	</head>
	<body>
		<div>
			The given below is a <p class="mark">block-level element</p> does not matter how much the content is in the block-level element. It takes the whole width of the container.
		</div>
	</body>
</html>

Output

output


You can observe that the text in between the <p> element appears on the new line irrespective of the tag in which it is enclosed.

Program 2

<html>
	<style>
		html {
			font-size: 30px;
		}

		.highlight1 {
			background-color: #61a916;
			color: white;
			padding-left: 0.7em;
		}

		.highlight2 {
			background-color: #1aa1e3;
			color: white;
			padding-left: 0.7em;
		}

		.highlight3 {
			background-color: #6a00ff;
			color: white;
			padding-left: 0.7em;
		}

		.highlight4 {
			background-color: #a20125;
			color: white;
			padding-left: 0.7em;
		}
	</style>
	<head>
		<title>Block elements</title>
	</head>
	<body>
		<p class="highlight1" align="center">Content 1</p>
		<p class="highlight2" align="center">Content 2</p>
		<p class="highlight3" align="center">Content 3</p>
		<p class="highlight4" align="center">Content 4</p>
	</body>
</html>

Output

output

Supported Tags

Below are the supported HTML tags categorized into block elements:

Block Elements

  • <div>
  • <header>
  • <footer>
  • <section>
  • <article>
  • <nav>
  • <aside>
  • <h1> through <h6>
  • <p>
  • <ul>
  • <ol>
  • <li>
  • <table>
  • <tr>
  • <th>
  • <td>
  • <form>
  • <fieldset>
  • <legend>
  • <address>

Inline elements in HTML

Inline elements behave differently than block-level elements, which always render on the same line. This means if you put inline elements next to each other, they all will be on the same line as if no newline character is present. They only contain other inline elements.

visual representation

Let's understand the inline element by taking an example.

Program 1

<html>
	<style>
		.mark {
			background-color: #fff300;
			color: black;
		}
	</style>
	<head>
		<title>Inline elements</title>
	</head>
	<body>
		<div>
			This is an - <span class="mark">inline element</span> it only occupies the space bounded by the tags defining the element.
		</div>
	</body>
</html>

Output

output

Program 2

<html>
	<style>
		html {
			font-size: 30px;
		}

		.highlight1 {
			background-color: #61a916;
			color: white;
			padding-left: 0.7em;
			padding-right: 0.7em;
		}

		.highlight2 {
			background-color: #1aa1e3;
			color: white;
			padding-left: 0.7em;
			padding-right: 0.7em;
		}

		.highlight3 {
			background-color: #6a00ff;
			color: white;
			padding-left: 0.7em;
			padding-right: 0.7em;
		}

		.highlight4 {
			background-color: #a20125;
			color: white;
			padding-left: 0.7em;
			padding-right: 0.7em;
		}
	</style>
	<head>
		<title>Inline elements</title>
	</head>
	<body>
		<p>
			<span class="highlight1">Content 1</span> <span class="highlight2">Content 2</span>
			<span class="highlight3">Content 3</span> <span class="highlight4">Content 4</span>
		</p>
	</body>
</html>

Output

output

Supported Tags

Below are the supported HTML tags categorized into inline elements:

Inline Elements

  • <span>
  • <a>
  • <strong>
  • <em>
  • <img>
  • <br>
  • <code>
  • <abbr>
  • <cite>
  • <b>
  • <i>
  • <u>
  • <small>
  • <sub>
  • <sup>
  • <mark>
  • <q>
  • <time>

Block and Inline elements in HTML

So let's look at more code demonstrating how the block and inline elements work hand in hand. 

Program

<html>
	<head>
		<style>
			.mark {
				background-color: #fff300;
				color: black;
			}
		</style>
		<title>Block and Inline elements</title>
	</head>
	<body>
		<div class="mark">DIV 1: Content</div>
		<p class="mark">P1: After DIV 1</p>
		<div>
			DIV 2: After P1
			<span class="mark">"SPAN 1: In DIV 2"</span>
			Continuing DIV 2
		</div>
	</body>
</html>

Output

output


So to understand the above example we can see that we have many divisions following one after the other, DIV 1 and DIV 2. Then, there is a span element that is inside the DIV 2.

Frequently Asked Questions

What are HTML elements?

A start tag, some content, and an end tag define an HTML element.

How are elements different from attributes in HTML?

Any object that appears on your page is considered an HTML element. They are used to describe the structure and content of a document, like <div> element represents a section in the document. In contrast, an attribute offers additional information about a certain HTML element.

What is the inline code in HTML?

Inline code in HTML refers to elements that do not start on a new line and only take up as much width as necessary.

Is img inline or block?

The img element is inline by default, meaning it does not start on a new line and fits within the flow of text.

What is the purpose of elements in HTML?

HTML, or HyperText Markup Language, generates Web pages and instructs browsers on how to display them. HTML comprises elements, tags, and attributes that work together to identify document portions and instruct browsers on how to display them.

Why do we need to close the tag in HTML?

An opening tag begins a page content section, and a closing tag ends it.

Conclusion

In this article, we have seen the concepts of block and inline elements in detail. To learn more on web development refer:


You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninjas!

Live masterclass