In HTML, there are two types of elements: block
and inline
.
The h2
element is a block element.
According to w3schools
A block-level element always starts on a new line, and the browsers automatically add some space (a margin) before and after the element.
A block-level element always takes up the full width available (stretches out to the left and right as far as it can).
An inline element does not start on a new line.
An inline element only takes up as much width as necessary.
for example :
.border-red{
border: 1px solid red;
}
<span class="border-red" >Hello World (span element is an inline)</span>
<button class="border-red" >Hello World (button element is an inline)</button>
<h2 class="border-red" > Hello world (h2 element is a block) </h2>
<p class="border-red" > Hello world (p element is a block) </p>
You can modify these behaviors using CSS.
.border-red {
border: 1px solid red;
}
.inline {
display: inline
}
.block {
display: block
}
<span class="border-red block" >Hello World (span element is inline)</span>
<button class="border-red block" >Hello World (button element is inline)</button>
<h2 class="border-red inline" > Hello world (h2 element is a block) </h2>
<p class="border-red inline" > Hello world (p element is a block) </p>