Open In App

How display: inline is different from display: inline-block in CSS ?

Last Updated : 28 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will know about the display property in CSS, along with understanding the 2 different property values for display property, i.e., display: inline & display: inline-block properties, & will understand their basic differences & implementation through the examples.

The display property facilitates setting the element by serving them as a block or inline element and the different layouts, such as flow layout, grid, or flex, that are used for its children. In simple words, it defines how the components(div, hyperlink, heading, etc) are going to be placed on the web page.

There are several property values for the display property, among which will cover & discuss 2 property values, i.e., display: inline & display: inline-block properties.

display: inline Property: When an element generates more than one inline element box & these boxes won’t create any line breaks before or after themselves. In the normal flow of the page, the subsequent element will appear on the same line, provided there is space. This property simply ignores the height and the width set by the user. 

Syntax:

element {
       display: inline;
       // CSS property
}

Example 1: This example describes the display property by setting the property value as inline.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>CSS Display property</title>
    <style>
        .item {
            height: 80px;
            width: 300px;
            margin-left: 200px;
            background: teal;
            text-align: center;
            display: inline;
        }
          
        #heading {
            font-size: 42px;
            font-weight: bold;
            color: #009900;
            margin-left: 130px;
        }
          
        #sub-heading {
            margin-left: 180px;
            margin-bottom: 10px;
        }
          
    </style>
</head>
  
<body>
    <div id="heading">
        GeeksforGeeks
    </div>
    <h3 id="sub-heading">
        display: inline Property
    </h3>
    <div class="item">Box 1 </div>
</body>
  
</html>


Output:

 

display: inline-block Property: By using the display: inline-block property, it is possible to set a specific width and height for an element while maintaining the top and bottom margins/paddings. Unlike the display: inline property, this property does not create a line-break after the element, & thus, allowing the element to sit alongside other elements horizontally without disrupting the page layout.

Syntax:

element {
       display: inline-block;
       // CSS properties
}

Example 2: This example describes the display property by setting the property value as inline-block.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>CSS Display property</title>
    <style>
        .item {
            height: 80px;
            width: 300px;
            padding-top: 20px;
            margin-left: 150px;
            background: teal;
            text-align: center;
            display: inline-block;
        }
          
        #heading {
            font-size: 42px;
            font-weight: bold;
            color: #009900;
            margin-left: 130px;
        }
          
        #sub-heading {
            margin-left: 180px;
            margin-bottom: 10px;
        }
          
    </style>
</head>
  
<body>
    <div id="heading">
        GeeksforGeeks
    </div>
    <h3 id="sub-heading">
        display: inline-block Property
    </h3>
    <div class="item">Box 1 </div>
</body>
  
</html>


Output:

 

Difference between display: inline vs display: inline-block:

                                                 display: inline

                             display: inline-block

We cannot change the width and height of the display: inline elements.

We can change the width and height of the display: inline-block elements.

It can have padding and margin in the horizontal direction. But it does not respect margin and padding in the vertical direction.

It can have padding and margin in all four directions and it respects all directions.

It cannot have a child block element. 

It can have a child block element.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads