简体   繁体   English

HTML容器div在其子项之后占用了额外的空间?

[英]HTML container div occupying extra space bellow its child item?

I've got a container div containing a grid of items in this website 我有一个容器div,其中包含该网站中的项目网格

However I'm noticing that the parent div is occupying some extra space at the bottom as can be seen in the image bellow. 但是,我注意到父div在底部占据了一些额外的空间,如下面的图像波纹所示。

I don't have any padding or margins on the child div, and when I inspect its size in chrome inspector it seems right. 我的子div上没有任何填充或边距,当我在chrome inspector中检查其大小时,这似乎是正确的。

额外的空间

Here is a minimal codepen to reproduce the issue: codpen link , notice the spacing between the .container-team container div and the next div. 这是重现该问题的最小代码笔: codpen链接 ,请注意.container-team容器div和下一个div之间的间距。

How can I eliminate this spacing? 如何消除此间距?

Remove off the scale transformation from the style of container-team . container-team样式中删除比例转换。 That is what is causing the issue on your page. 这就是导致您的页面出现问题的原因。

The quick and dirty solution is to just add a negative margin to the parent container: 快速而肮脏的解决方案是向父容器添加负的边距:

.container-team {  
  max-width: 1170px;
  width: 100%;
  margin: 0px auto;
  margin-bottom: -25%;
  padding: 0 0px;
  box-sizing: border-box;
  -webkit-transform-origin: top center;
          transform-origin: top center;
  -webkit-transform: scale(0.8);
          transform: scale(0.8);
}

https://codepen.io/anon/pen/eXJNBw?editors=1100 https://codepen.io/anon/pen/eXJNBw?editors=1100

However, the reason you are seeing this extra white space is because you scaled everything by 0.8. 但是,看到此额外空白的原因是因为您将所有内容缩放了0.8。 Whenever you do a css transform: scale , it creates the layout before doing the scaling, meaning the positioning of the elements is relative to where it was at 1.0 scaling. 每当执行css transform: scale ,它都会在进行缩放之前创建布局,这意味着元素的位置相对于1.0缩放时的位置。 The reason it's tied to the top is because the css is set to be that way using a transform-origin: top center , which puts the transformed element at the top and center. 之所以将其绑定到顶部,是因为使用transform-origin: top center将css设置为这种方式,它将转换后的元素放在顶部和中央。 If you removed this, it would simply scale it all down by 0.8, adding white space to the top and the bottom. 如果删除此选项,它将简单地将其全部缩小0.8,从而在顶部和底部添加空白。

The negative margin fix is fine, but if you want a more robust solution, you should maybe consider scaling each user element down and using margin-left and margin-right instead of scaling the whole thing. 负边距修复很好,但是如果您想要一个更强大的解决方案,则应该考虑将每个用户元素缩小并使用margin-left和margin-right而不是整个缩放。

You must add a value for the height in the style corresponding to the element .container-team. 您必须以与元素.container-team对应的样式添加高度值。 The following style can solve the problem: 以下样式可以解决问题:

height: -webkit-fill-available;

Adding any other value for height can also work 添加其他任何高度值也可以

height: 100px;

I moved your last div to inside the previous div. 我将您的最后一个div移到了上一个div。

Now you should be able to add the spacing that you want without all that extra spacing: https://codepen.io/anon/pen/BbjyYW?editors=1100 现在,您应该能够添加所需的间距,而无需所有额外的间距: https : //codepen.io/anon/pen/BbjyYW?editors=1100

<div style="background-color: #999;"> next item </div>

If you don't want all of that spacing, but you don't want that last div to be inside of the container-team div, nest the container-team div and "next item" div inside of another div, like this: 如果您不希望所有间距都一样,但又不想将最后一个div放在container-team div内,则将container-team div和“ next item” div嵌套在另一个div内,如下所示:

<div> <div class="container-team"> Container team content </div> <div style="background-color: #999;"> next item </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM