`div` with display mode set to `inline-block` gets forced onto next line despite setting margin to 0

huangapple go评论100阅读模式
英文:

`div` with display mode set to `inline-block` gets forced onto next line despite setting margin to 0

问题

假设我们有以下的HTML和CSS:

* {
  box-sizing: border-box;
  margin: 0;
}

.left-column {
  display: inline-block;
  background-color: rgb(41, 52, 72);
  color: white;
  width: 30vw;
  height: 100vh;
  border: 5px solid red;
}

.right-column {
  display: inline-block;
  width: 70vw;
  height: 100vh;
  color: rgb(41, 52, 72);
  border: 5px solid green;
}
<div class="left-column">
</div>

<div class="right-column"></div>

我将left-column类的div的宽度设置为视口宽度的30%,并将right-column类的div的宽度设置为视口宽度的70%。由于我使用了边框框模型(box-sizing)、总体外边距为0,并将显示模式设置为inline-block,我期望这些框在行内方向上相邻显示。但奇怪的是,具有left-column类的div出现在具有right-column类的div的下方。

我觉得我可能没有考虑到某些额外的水平空间,这些空间以某种方式被渲染出来,强制right-column进入下一行。我在这里漏掉了什么?

英文:

Suppose we have the following HTML and CSS:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

* {
  box-sizing: border-box;
  margin: 0;
}

.left-column {
  display: inline-block;
  background-color: rgb(41, 52, 72);
  color: white;
  width: 30vw;
  height: 100vh;
  border: 5px solid red;
}

.right-column {
  display: inline-block;
  width: 70vw;
  height: 100vh;
  color: rgb(41, 52, 72);
  border: 5px solid green;
}

<!-- language: lang-html -->

&lt;div class=&quot;left-column&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;right-column&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

I am setting the width of the div with class left-column to 30% of the viewport width, and the width of the div with class right-column to 70% of the viewport width. Since I'm using border box sizing, an overall margin of 0 and display mode to inline-block I expect for these boxes to appear adjacent to each other in the inline direction. Strangely, the div with class right-column appears underneath the div with class left-column.

I feel like I am not accounting for some additional horizontal space which somehow gets rendered and forces the right-column onto the next line. What am I missing here?

答案1

得分: 3

以下是已翻译的内容:

A scroll bar also takes up space horizontal space.
滚动条还会占用水平空间。

Here is an example that hides the scroll bar for webkit based browsers.
这是一个隐藏基于 Webkit 浏览器滚动条的示例。

You should see that they align next to each other.
您应该看到它们对齐在一起。

(i've also removed any enters/spaces between the <div> elements, these take up space as well, like @kosh mentioned.)
(我还删除了<div>元素之间的任何换行符/空格,这些也会占用空间,就像 @kosh 提到的那样。)

flex-box

弹性盒子布局

You might be able to use flex-box to achieve the same without having to worry about scroll bars.
您可以尝试使用 flex-box 来实现相同的效果,而不必担心滚动条。

英文:

A scroll bar also takes up space horizontal space.
Here is an example that hides the scroll bar for webkit based browsers.

You should see that they align next to each other.
(i've also removed any enters/spaces between the &lt;div&gt; elements, these take up space as well, like @kosh mentioned.)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

::-webkit-scrollbar {
  display: none;
}

* {
  box-sizing: border-box;
  margin: 0;
}

.left-column {
  display: inline-block;
  background-color: rgb(41, 52, 72);
  color: white;
  width: 30vw;
  height: 100vh;
  border: 5px solid red;
}

.right-column {
  display: inline-block;
  width: 70vw;
  height: 100vh;
  color: rgb(41, 52, 72);
  border: 5px solid green;
}

<!-- language: lang-html -->

&lt;div class=&quot;left-column&quot;&gt;
&lt;/div&gt;&lt;div class=&quot;right-column&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

flex-box

You might be able to use flex-box to achieve the same without having to worry about scroll bars.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

body {
  display: flex;
  margin: 0;
}

* {
  box-sizing: border-box;
}

.left-column {
  flex: 30 30 auto;
  background-color: rgb(41, 52, 72);
  color: white;
  height: 100vh;
  border: 5px solid red;
}

.right-column {
  flex: 70 70 auto;
  height: 100vh;
  color: rgb(41, 52, 72);
  border: 5px solid green;
}

<!-- language: lang-html -->

&lt;div class=&quot;left-column&quot;&gt;
&lt;/div&gt;

&lt;div class=&quot;right-column&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

  1. border-box添加到所有元素以及beforeafter伪元素。
  2. 使用百分比(%)作为宽度单位,因为vw/vh包括滚动条等完整的视口大小。
  3. 将字体大小设置为0。
  4. inline-block元素之间不添加任何空格或空白。

以下是CSS代码的翻译部分:

*, *:before, *:after {
  box-sizing: border-box;
  margin: 0;
}

.left-column {
  display: inline-block;
  background-color: rgb(41, 52, 72);
  color: white;
  width: 30%;
  height: 100vh;
  border: 5px solid red;
}

.right-column {
  display: inline-block;
  width: 70%;
  height: 100vh;
  color: rgb(41, 52, 72);
  border: 5px solid green;
}

.left-column, .right-column {
  font-size: 0px;
}

请注意,这是CSS代码的翻译部分。

英文:

Here you go...

  1. add border-box to all elements and before and after
  2. Use % for width as vw/vh counts full viewport size including scrollbar etc.
  3. set font size to 0
  4. do not add any space, whitespace in between the inline-block elements

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-css -->

*, *:before, *:after {
  box-sizing: border-box;
  margin: 0;
}

.left-column {
  display: inline-block;
  background-color: rgb(41, 52, 72);
  color: white;
  width: 30%;
  height: 100vh;
  border: 5px solid red;
}

.right-column {
  display: inline-block;
  width: 70%;
  height: 100vh;
  color: rgb(41, 52, 72);
  border: 5px solid green;
}
.left-column, .right-column {
  font-size: 0px;
}

<!-- language: lang-html -->

&lt;div class=&quot;left-column&quot;&gt;&lt;/div&gt;&lt;div class=&quot;right-column&quot;&gt;&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月10日 23:13:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877097.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定