Home > Article > Web Front-end > 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!