英文:
`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 -->
<div class="left-column">
</div>
<div class="right-column"></div>
<!-- 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 <div>
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 -->
<div class="left-column">
</div><div class="right-column"></div>
<!-- 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 -->
<div class="left-column">
</div>
<div class="right-column"></div>
<!-- end snippet -->
答案2
得分: 0
- 将
border-box
添加到所有元素以及before
和after
伪元素。 - 使用百分比(%)作为宽度单位,因为
vw
/vh
包括滚动条等完整的视口大小。 - 将字体大小设置为0。
- 在
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...
- add border-box to all elements and before and after
- Use % for width as vw/vh counts full viewport size including scrollbar etc.
- set font size to 0
- 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 -->
<div class="left-column"></div><div class="right-column"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论