添加并排 div 混乱

发布于 2024-11-18 22:23:11 字数 655 浏览 3 评论 0原文

我似乎总是把这个简单的 HTML 内容弄错。我目前正在尝试在我制作的这个测试页面的页面中间添加并排的 div:

http: //www.comehike.com/hiking_dev.php

我添加的代码是这样的:

<div style="width: 460px; float: left; ">
<strong>Test hello</strong>
</div>
<div style="width: 300px; float: right; ">
<strong>Test hello 2</strong>
</div>

我添加了 标签,以便您可以更好地在页面上找到它。

但是您是否看到那里出现了类似“考虑任何伤害风险时”的文本 - 但该文本位于下面的

标签中。为什么会出现在那里?

将我试图对齐的 2 个 div 包裹在另一个 div 中是否是更好的做法?

I always seems to get this simple HTML stuff wrong. I am currently trying to add side by side divs in the middle of a page on this test page I made:

http://www.comehike.com/hiking_dev.php

The code I added was something like this:

<div style="width: 460px; float: left; ">
<strong>Test hello</strong>
</div>
<div style="width: 300px; float: right; ">
<strong>Test hello 2</strong>
</div>

I added <strong> tags so you can spot it on the page better.

But do you see there is text that appears there that reads like this "When considering the injury risk of any" - but that text is in the <p> tag below. Why is it appearing there?

Is it better practice to wrap my 2 divs that I am trying to align, within another div?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

安稳善良 2024-11-25 22:23:11

在两个浮动 div 之后,添加另一个空 div...

<div style="clear:both;"></div>

这将导致两个浮动 div 将所有后续内容推送到它们下方。正如您现在所看到的,第一个 div 之后有 200 像素的空白空间,允许其他内容简单地环绕它。

增加浮动 div 的宽度可能不适合您的布局,因此 clear:both; 对于这种情况最为典型。

After your two floating divs, add another empty div...

<div style="clear:both;"></div>

This will cause the two floating divs to push all subsequent content below them. As you have it now, there is 200 pixels of empty space after the first div allowing the other content to simply wrap around it.

Increasing the width of the floating divs may not be desirable for your layout so clear:both; is most typical for this situation.

三生路 2024-11-25 22:23:11

将这两个 div 包围在父 div 中,并将溢出设置为隐藏。

<div style="overflow:hidden;">
  <div style="width: 460px; float: left; ">
    <strong>Test hello</strong>
  </div>
  <div style="width: 300px; float: right; ">
    <strong>Test hello 2</strong>
  </div>
</div>

另一种选择(正如其他人指出的那样)是使用第三个元素:

<div style="clear:both;"></div>

关于哪个更好是有争议的。通常,两者都可以。这是关于该主题的帖子:
浮动子元素:overflow:hidden还是clear:both?

Surround those two divs in a parent div with overflow set to hidden.

<div style="overflow:hidden;">
  <div style="width: 460px; float: left; ">
    <strong>Test hello</strong>
  </div>
  <div style="width: 300px; float: right; ">
    <strong>Test hello 2</strong>
  </div>
</div>

An alternative (as others have pointed out) is to use a third element:

<div style="clear:both;"></div>

It's debatable as to which is better. Usually, either is just fine. Here's a post about the topic:
Floated Child Elements: overflow:hidden or clear:both?

泡沫很甜 2024-11-25 22:23:11

您需要按照其他人的建议,使用 clear:both; 在两个 div 下面添加一个 div,或者您可以将 clear:both; 添加到以下

元素。

You'll either need to add a div below your two divs with clear:both; as others have suggested, or you could add clear:both; to the following <p> element.

时光瘦了 2024-11-25 22:23:11

因为整个页面宽度为 960px。您的组合 div 宽度为 760px (400+300)。如果你给第二个 div 添加 200px 应该没问题。

编辑:由于填充的原因,您可以将任一 div 增加 150px 就可以了。

Because you have an entire page width of 960px. You're combined div widths are 760px (400+300). If you add 200px to the second div you should be fine.

Edit: Because of padding, you can increase either of the divs by 150px and be fine.

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