Open In App

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:



  
 

HTML 5 Semantic block elements:

Example: 




<!DOCTYPE html>
<html lang="en">
 
<head>
    <!--Meta data-->
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible"
          content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
 
    <!--The description written on title tag
        get appeared as browser tab name-->
    <title>Geeks For Geeks </title>
 
    <style>
        h1 {
            color: #006600;
            text-align: center;
            border: 2px solid #091418;
            background-color: yellow;
        }
        .container {
            border: 2px solid #040804;
            background-color: slategray;
        }
        p{
            border: 2px solid #040804;
            background-color:  #4089c5;
        }
    </style>
</head>
   
<!-- Whatever content in the body tag
     appears on the webpage -->
 
<body>
    <div class="container" >
        <h1>Geeks for Geeks(h1) </h1>
         
<p>
             This is a paragraph example of block
             element which occupied whole width (p)
        </p>
 
    </div>
</body>
 
</html>

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:

Example: 




<!DOCTYPE html>
<html lang="en">
 
<head>
    <!--Meta data-->
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
 
    <style>
        h1 {
            color: #006600;
            text-align: center;
        }
 
        span {
            border: 2px solid #040804;
            background-color: #4089c5;
        }
 
        a {
            border: 2px solid #040804;
        }
    </style>
</head>
 
<!-- Whatever content in body tag
    appears on the webpage-->
<body>
    <div class="container">
        <h1>Geeks for Geeks</h1>
         
<p>
            This is a <span>span element </span>
            <span>and </span><b>this</b> is a
            <a href="#">link</a> which are examples
            of inline elements which occupy only
            sufficient width.
        </p>
 
    </div>
</body>
 
</html>

Output:

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.

Article Tags :