How to Remove the Space Between Inline-Block Elements in HTML?

How to Remove the Space Between Inline-Block Elements in HTML?

When you’re working with inline or inline-block elements in HTML and CSS, you might see extra spaces popping up between them. This happens because of how HTML handles white spaces by default. In this blog, we will discuss methods to remove unwanted space between inline or inline-block elements. 

Table of Contents:

Understanding the Space Issue

Inline and inline-block elements act like text, so any spaces, tabs, or line breaks between them show up as actual spaces. This can create gaps between your elements that you didn’t plan for.

Example: This code demonstrates how two inline-block elements can have extra space between them because of the whitespace in your HTML.

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .container {
            font-size: 16px; /* Default font size */
        }
        .box {
            display: inline-block;
            width: 100px;
            height: 100px;
            background-color: blue;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box"></div>
        <div class="box"></div> <!-- Space appears here -->
    </div>
</body>
</html>

 Output: 

Examples of spacing issues

Methods to Remove the Space Between Inline/Inline-Block Elements

You can use properties like font-size: 0, negative margins, and display: flex to remove the space between inline/inline-block elements. Let’s discuss these properties below. 

Method 1: Using font-size: 0 on Parent Element 

You can use the font-size: 0 to remove the extra space on the parent container. Since the spaces between inline elements depend on the font size, making it 0 removes the gaps completely.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .container {
            font-size: 0;
        }
        .box {
            display: inline-block;
            width: 100px;
            height: 100px;
            background-color: red;
            font-size: initial; 
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box"></div>
        <div class="box"></div>
    </div>
</body>
</html>

Output:

using font size 0 on parent element

Explanation: You can use this code to create a .container that is set to font-size: 0 to remove the whitespace. The child .box elements have their font size reset to normal using font-size: initial.    

Method 2: Using Negative Margins

You can use negative margins, like margin-right: -4px, to pull the elements closer by slightly overlapping them. This gets rid of the gap completely.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .box {
            display: inline-block;
            width: 100px;
            height: 100px;
            background-color: green;
            margin-right: -4px; 
        }
    </style>
</head>
<body>
    <div>
        <div class="box"></div>
        <div class="box"></div>
    </div>
</body>
</html>

Output:

using negative margins

Explanation: This code creates two green boxes side by side. The display: inline-block makes them inline elements, and the margin-right: -4px removes any extra space between them by slightly overlapping.

Method 3: Using display: flex Instead of inline-block

You can use Flexbox to avoid whitespace. You can set the parent container to display: flex to make the elements next to each other without any extra gaps.

Example:

<!DOCTYPE html>
<html lang="en">
<head>
    <style>
        .container {
            display: flex;
        }
        .box {
            width: 100px;
            height: 100px;
            background-color: orange;
            flex: 1;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="box"></div>
        <div class="box"></div>
    </div>
</body>
</html>

Output:  

using display flex instead of inline-block

Explanation: This code creates two orange boxes arranged side by side using Flexbox. The display: flex on the parent container makes the boxes align next to each other, and the flex: 1 ensures they take up equal space.

Conclusion 

You can use properties such as font-size: 0, negative margins, and display: flex to remove the space between inline/inline-block elements. You can remove the space between inline or inline-block elements to get better control over your layout. The space can mess up your design and create improper alignment of elements. By removing it, you get cleaner and more perfect styling. Therefore, you get a well-structured webpage.

How to Remove the Space Between Inline-Block Elements? – FAQs

1. Why does extra space appear between inline/inline-block elements?

The extra space comes from the way HTML treats whitespace (like spaces, tabs, or line breaks) between elements. Since inline and inline-block elements act like text, this whitespace gets rendered.

2. What are the common methods to remove the space?

The most common methods are font-size: 0, negative margins, and display: flex to remove the space between inline/inline-block elements.

3. Which is the easiest solution to try first?

You can use the font-size: 0 for an easy way to fix the issue. It is simple and effective for most layouts.

4. Are there any drawbacks to these methods?
  • font-size: 0: You’ll need to reset the font size for child elements where text is used.
  • Negative margins: Overlapping elements can cause design inconsistencies in some cases.
5. What is the modern solution for this issue?

Flexbox (display: flex;) is the most modern and cleanest solution.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner