CSS method to solve the gap caused by display:inline-block; layout
This article mainly introduces the CSS method to solve the gap (gap) caused by display:inline-block;. It has certain reference value. If you need it, you can learn about it.
Today I used display:inline-block; when making H5 horizontal sliding cards, but found that there were gaps between elements on the same horizontal line. This is obviously not the effect I want, so I changed to left floating, which solved the gap problem, but the width of the parent element needs to be set to achieve horizontal left and right scrolling, which increases the amount of code, because the number of cards is not fixed, and its parent element needs to be set in real time. The width requires the use of js, so the amount of code increases, which is not the best choice. It seems that the best solution is to use display:inline-block;, so the gap problem arises. The code is as follows:
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"> <title>document</title> <style> *{margin:0;padding:0;} .box{overflow-x:auto;background:#fff;white-space:nowrap;} .box span{display:inline-block;width:100px;height:30px;line-height:30px;text-align:center;background:#f00;color:#fff;} </style> </head> <body> <p class="box"> <span>111</span> <span>111</span> <span>111</span> <span>111</span> </p> </body> </html>
The effect is as follows:
This gap obviously exists, and it is said that this performance is in compliance with the specification The expected performance is caused by whitespace characters caused by line breaks. But this effect is obviously not what we want. The gap we want is the margin we set according to our actual needs. So how to eliminate this gap? There are three methods:
Method one: No line breaks between elements, the code is as follows:
<p class="box"> <span>111</span><span>111</span><span>111</span><span>111</span> </p>
The effect As follows:
Method 2: Set font-size:0 for its parent element; set the actual required size for itself Font size. The bad thing is that some browsers, like Chrome and Opera, have minimum font settings, but it seems that Chrome no longer has this setting. The code is as follows:
css:
.box{overflow-x:auto;background:#fff;white-space:nowrap;font-size:0;} .box span{display:inline-block;width:100px;height:30px;line-height:30px;text-align:center;background:#f00;color:#fff;font-size:14px;}
html:
<p class="box"> <span>111</span> <span>111</span> <span>111</span> <span>111</span> </p>
The effect is as follows:
##Method three: For the negative margin method, it should be noted that this gap is related to the font size, so the gap is not a certain value.
Of the above three methods, the first two are better solutions, and the third method is not recommended. There are other solutions online, but I think the first two are better. PS: Combined with the comments and suggestions of the majority of front-end experts, you can also set display:flex to its parent element to eliminate gaps, and the amount of code is less, but it is used at my level The effect of sliding cards causes all elements to be in the visible window, and horizontal left and right sliding cannot be achieved.The above is the detailed content of CSS method to solve the gap caused by display:inline-block; layout. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and
