如何摆脱盒子之间的空间?

发布于 2025-02-10 17:23:39 字数 845 浏览 1 评论 0原文

我正在尝试学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫以 2025-02-17 17:23:39

因此,您实际的HTML代码中的空格(默认情况下)被解释为文本(尽管连续的多个白色空间确实被凝结成一个单一空间,就像这里发生的那样)。您可以说是这种情况,因为如果悬停在缝隙上,光标会更改为I光束,并且实际上可以选择该空间。


一种解决方案只是在您的两个div标签之间没有空格,例如,

</div>

<div>

另一个解决方案

</div><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.

</div>

<div>

would become

</div><div>

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!)

流殇 2025-02-17 17:23:39
* {
  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;
  display:flex;
}
<main>
  <section>
    <div class="box">
    </div>
    <div class="box">
    </div>
  </section>
</main>
<footer>
</footer>

只需在您的“部分”样式中添加显示:flex;

* {
  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;
  display:flex;
}
<main>
  <section>
    <div class="box">
    </div>
    <div class="box">
    </div>
  </section>
</main>
<footer>
</footer>

Just add display:flex; in your "section" styling.

梦过后 2025-02-17 17:23:39
* {
  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;
  float:left
}

section {
  white-space: nowrap;
}
<main>
  <section>
    <div class="box">
    </div>
    <div class="box">
    </div>
  </section>
</main>
<footer>
</footer>

* {
  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;
  float:left
}

section {
  white-space: nowrap;
}
<main>
  <section>
    <div class="box">
    </div>
    <div class="box">
    </div>
  </section>
</main>
<footer>
</footer>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文