我如何保持嵌套的 div 不“泄漏”出底部吗?

发布于 2024-11-16 10:54:22 字数 338 浏览 0 评论 0原文

在 haml 中,我有这样的内容:

#profile
   #photo

   #bio

但是发生的情况是 #photo#profile div 中“泄漏”出来。如果我检查它,#profile 是一个非常小的盒子,#photo 从它的外面突出。

我将 float:left 应用于 #photo,以便 #bio 位于

我似乎经常遇到的照片旁边。这是怎么回事?我该如何解决?

In haml, I have something like this:

#profile
   #photo

   #bio

But what hapens is the #photo "leaks" out of the #profile div. If I inspect it, #profile is a very small box and #photo peaks out of it.

I applied a float:left to #photo so that #bio will sit next to the photo

I seem to run into this alot. What is going on and how do I fix it?

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

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

发布评论

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

评论(6

慵挽 2024-11-23 10:54:22

我发现最简单的方法是将 overflow:hidden 添加到容器中,在您的情况下为 #profile

不需要额外的元素,也不会因清除左侧、右侧或两者而产生副作用。

I find that the easiest way is to add overflow:hidden to the container, #profile in your case.

No extra elements needed and no side-effects from clearing left, right or both.

深陷 2024-11-23 10:54:22

当您的容器内有浮子时,您需要某种方式告诉容器清除底部的浮子。

通常的方法是在所有浮动项之后在容器末尾的 DOM 中创建一个元素,并告诉它 clear:left; 同时将其设置为 visibility :隐藏;
(假设您的浮动项目浮动到left,否则使用rightboth作为clear

)可以使用纯 CSS 来做到这一点:

#container:after{
  content:".";
  display:block;
  clear:left;
  visibility:hidden;
}

总是可以选择直接将 HTML 元素添加到容器的底部,但是我发现纯 CSS 方式更好一点。

When you have a container with floats inside, you need some way of telling the container to clear the floats at the bottom.

The usual way of doing this is to create an element in the DOM at the end of the container after all of the floating items and tell it to clear:left; whilst also setting it to visibility:hidden;
(Assuming your floated items are floating to the left, otherwise use right or both as the clear value)

You can do this with pure CSS as:

#container:after{
  content:".";
  display:block;
  clear:left;
  visibility:hidden;
}

There's always the option of directly adding a HTML element to the bottom of the container however I find the pure CSS way a tad nicer.

染火枫林 2024-11-23 10:54:22

浮动元素不占用其父容器中的空间。要解决此问题,您需要在这些元素下方添加一些内容,不允许浮动元素浮动经过它。您可以使用 CSS 属性 clear 来完成此操作。因此,在您的情况下,添加:

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

清除那些浮动元素,使其不超过它。

Floated elements do not take up space in their parent container. To fix this, you need to add something below these elements that do not allow the floated elements to float past it. You can accomplish this with the CSS property clear. So in your case, add:

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

To clear those floated elements from extending past it.

七秒鱼° 2024-11-23 10:54:22

也许你应该告诉照片不要超过框限制,自动调整大小:

#photo {
  width:auto;
  height:auto;
  max-width:100%;
  max-height:100%;
}

这样照片就不会超过 div 大小。也许您还应该将 position:relative; 添加到 #profile div。

更新

我忘记指出,只有当您的 #photo 实际上是图像元素时,此功能才有效。

maybe you should just tell the photo not to exceed the box limits, auto resizing:

#photo {
  width:auto;
  height:auto;
  max-width:100%;
  max-height:100%;
}

This way the photo will not exceed the div size. Probably you should also add position:relative; to the #profile div.

Update

I forgot to specify that this works only if your #photo is actually an image element.

箜明 2024-11-23 10:54:22

有几种方法。我倾向于使用旧的方法,例如:

overflow:auto 应用于包装父 div。

或者将 div 元素放置在应用了 clear:both; 的父 div 结束标记之前。

有新的方法,例如在css中使用 :after 伪类,但我在使用该方法时遇到了问题。

There are a couple methods. I tend to use the older methods such:

Apply overflow:auto to the wrapping parent div.

Or placing a div element right before the parent div closing tag that has clear:both; applied.

There are new methods, such as using the :after pseudo class in css, but I have had problems with that method.

赤濁 2024-11-23 10:54:22

清除 div:

<style type="text/css">
    .clear { height: 0px; line-height: 0px; clear:both; font-size:0px; }
</style>


<div class="profile">
     <div class="photo"><img /></div>
     <div class="bio">Lorem ipsum</div>
     <div class="clear"></div>
</div>

应该适用于所有受影响的浏览器。

Clearing div:

<style type="text/css">
    .clear { height: 0px; line-height: 0px; clear:both; font-size:0px; }
</style>


<div class="profile">
     <div class="photo"><img /></div>
     <div class="bio">Lorem ipsum</div>
     <div class="clear"></div>
</div>

Should work for all browsers affected.

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