添加并排 div 混乱
我似乎总是把这个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在两个浮动 div 之后,添加另一个空 div...
这将导致两个浮动 div 将所有后续内容推送到它们下方。正如您现在所看到的,第一个 div 之后有 200 像素的空白空间,允许其他内容简单地环绕它。
增加浮动 div 的宽度可能不适合您的布局,因此
clear:both;
对于这种情况最为典型。After your two floating divs, add another empty 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.将这两个 div 包围在父 div 中,并将溢出设置为隐藏。
另一种选择(正如其他人指出的那样)是使用第三个元素:
关于哪个更好是有争议的。通常,两者都可以。这是关于该主题的帖子:
浮动子元素:overflow:hidden还是clear:both?
Surround those two divs in a parent div with overflow set to hidden.
An alternative (as others have pointed out) is to use a third element:
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?
您需要按照其他人的建议,使用
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 addclear:both;
to the following<p>
element.因为整个页面宽度为
960px
。您的组合 div 宽度为760px
(400+300)。如果你给第二个 div 添加 200px 应该没问题。编辑:由于填充的原因,您可以将任一 div 增加
150px
就可以了。Because you have an entire page width of
960px
. You're combined div widths are760px
(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.