简体   繁体   English

如何控制内联块div元素之间的间隙,以使间隙可以夹在视口后面?

[英]How to control gap between inline-block div elements, such that the gap can be allowed to clip behind the viewport?

For example, if we use display: inline-block and margin-right: 20px to style two div elements, the second div element prematurely jumps to the next line when the window frame is resized to hit its right margin. 例如,如果我们使用display: inline-blockmargin-right: 20px设置两个div元素的样式,则在调整窗口框架的大小以使其达到右边距时,第二个div元素会过早跳到下一行。

Is there a simple way to get around this problem? 是否有解决此问题的简单方法?

The question isn't perfectly clear, but if Eric's answer doesn't tell you what you need to know, then this might be your issue: 这个问题不是很清楚,但是如果Eric的答案没有告诉您您需要知道什么,那么这可能是您的问题:

When you set elements to inline-block, any whitespace between them (including newlines) in the source affects the spacing. 将元素设置为内联块时,源中元素之间的任何空格(包括换行符)都会影响间距。 It's subtle and very frustrating until you realize that's what's happening. 直到您意识到那是微妙的,这是微妙而令人沮丧的。

You have two possible solutions. 您有两种可能的解决方案。

1) Put everything on one line with no spacing between them in the source code. 1)在源代码中将所有内容放在一行上,并且它们之间没有空格。 This is usually not ideal because it's much less readable. 这通常是不理想的,因为它的可读性差得多。

2) Use HTML comments to cause the browser to ignore the space between them. 2)使用HTML注释使浏览器忽略它们之间的空间。 It's not ideal because you have to change the HTML, rather than use some CSS trick, but it works. 这不是理想的,因为您必须更改HTML,而不是使用一些CSS技巧,但是它可以工作。

For example, if you were making the li elements inline-block, you would do this: 例如,如果要使li元素成为内联块,则可以这样做:

<ul>
    <li>Item 1</li><!--
 --><li>Item 2</li><!--
 --><li>Item 3</li>
</ul>

向容器添加margin-right: -20px

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM