如何摆脱盒子之间的空间?
我正在尝试学习HTML和CSS,并试图弄清楚如何将框架彼此排列。我设法安装了两个盒子的“宽度:50%”;彼此相邻,但它们之间仍然有一个空白,我想知道这个空白是什么以及如何摆脱它。这是codepen上的代码的链接: box
* {
margin: 0;
padding: 0;
}
.box {
box-sizing: border-box;
width: 50%;
height: 200px;
padding: 15px;
background-color: #127202;
display: inline-block;
border: 5px solid;
border-color: #000000;
}
section {
white-space: nowrap;
}
<main>
<section>
<div class="box">
</div>
<div class="box">
</div>
</section>
</main>
<footer>
</footer>
I'm trying to learn HTML and CSS and trying to figure out how to line up boxes next to each other. I've managed to fit two boxes with "width: 50%;" next to each other but there is still a white space between them and I'm wondering what this white space is and how to get rid of it. Here is the link to the code at CodePen: Boxes
* {
margin: 0;
padding: 0;
}
.box {
box-sizing: border-box;
width: 50%;
height: 200px;
padding: 15px;
background-color: #127202;
display: inline-block;
border: 5px solid;
border-color: #000000;
}
section {
white-space: nowrap;
}
<main>
<section>
<div class="box">
</div>
<div class="box">
</div>
</section>
</main>
<footer>
</footer>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,您实际的HTML代码中的空格(默认情况下)被解释为文本(尽管连续的多个白色空间确实被凝结成一个单一空间,就像这里发生的那样)。您可以说是这种情况,因为如果悬停在缝隙上,光标会更改为I光束,并且实际上可以选择该空间。
一种解决方案只是在您的两个
div
标签之间没有空格,例如,另一个解决方案
就是使用
So, whitespace in your actual HTML code is (by default) interpreted as text (although multiple white space in a row does get condensed into one singular space, as is happening here). You can tell that this is the case, because if you hover over the gap your cursor changes to the I-beam, and you can actually select the space.
One solution would just be to have no whitespace between your two
div
tags, e.g.would become
Another would be to lay out your divs using the css flexbox, which avoids this issue entirely (and still allows your HTML markup to look nice!)
只需在您的“部分”样式中添加
显示:flex;
。Just add
display:flex;
in your "section" styling.