Home Web Front-end CSS Tutorial CSS method to solve the gap caused by display:inline-block; layout

CSS method to solve the gap caused by display:inline-block; layout

May 21, 2017 am 10:38 AM
Block inline gap

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>
Copy after login

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>
Copy after login

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;}
Copy after login

html:


<p class="box">
    <span>111</span>
    <span>111</span>
    <span>111</span>
    <span>111</span>
</p>
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

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

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

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

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

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

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

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

Creating Your Own Bragdoc With Eleventy Creating Your Own Bragdoc With Eleventy Mar 18, 2025 am 11:23 AM

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.

What the Heck Are npm Commands? What the Heck Are npm Commands? Mar 15, 2025 am 11:36 AM

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.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

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

See all articles