简体   繁体   English

如何使父div包装(并收缩)其浮动子?

[英]How to make a parent div wrap (and shrink) its floating children?

I have a responsive layout which consists of a bunch of elements. 我有一个响应式布局,由一堆元素组成。 Here is a very simplified version of the code: 这是代码的一个非常简化的版本:

HTML: HTML:

<div class="parent">
  <div class="header"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
</div>

CSS: CSS:

.parent {
  background: red;
  display:inline-block;
}

.child {
  background: green;
  width:100px;
  height:100px;
  float:left;
}

.header {
  background: yellow;
  width:100%;
  height:30px;
}

JSFiddle 的jsfiddle

What I am trying to achieve is making the header (yellow box) take the same width as the child divs (green boxes). 我想要实现的是使标题(黄色框)与子div(绿色框)的宽度相同。 And it works well when the layout is rendered at first, here is a screenshot from the JSFiddle 当首先渲染布局时,它运行良好,这是JSFiddle的截图 在此输入图像描述

But when the screen size becomes more narrow the header doesn't shrink correctly, here is a screenshot for a smaller area: 但是当屏幕尺寸变得更窄时,标题不会正确缩小,这里是一个较小区域的屏幕截图:

在此输入图像描述

I spent a few hours trying to figure it out, but I could not. 我花了几个小时试图搞清楚,但我做不到。 Part of the challenge is that I don't know in advance what the number of green boxes is going to be as they are rendered dynamically by .NET 部分挑战是我事先并不知道绿色盒子的数量是什么,因为它们是由.NET动态呈现的

Update 1 更新1

The fact that the green boxes wrap is desired, what I'm trying to achieve is for the yellow box to have the same width as the combined width of the green boxes. 需要绿色框包装的事实,我想要实现的是黄色框具有与绿色框的组合宽度相同的宽度。 Desired result: 期望的结果:

在此输入图像描述

I don't think it's possible to do what you want with pure CSS. 我不认为用纯CSS做你想做的事。 The dangling space is there because that's how much space is left . 悬空空间就在那里,因为剩下多少空间。 The box just goes below because there's no space above, you cannot shrink the yellow header because it does not "know" anything about the fact that green boxes do not fit. 盒子就在下方,因为上方没有空间,你不能缩小黄色标题,因为它不会“知道”绿箱不适合的事实。 I tried a few hacks but couldn't get it to work. 我尝试了一些黑客,但无法让它工作。 The safest bet is to go with JS. 最安全的选择是与JS合作。

However, here are some alternatives which you might find worth checking if you're flexible to change your design a bit. 但是,如果您可以灵活地更改设计,可以找到一些值得检查的替代方案。

1. Don't specify the size at all 1.根本不要指定尺寸

You can let flex-box do its thing. 你可以让flex-box做它的事情。 JSFiddle . JSFiddle Works with every number of elements, but they get too big or too small depending on the parent size and number of children. 适用于每个元素,但根据父级大小和子级数量,它们会变得太大或太小。

2. Allow for space inbetween 2.允许中间的空间

flex-box to the rescue again, you can use justify-content: space-between to keep the header from the most-left box to most-right box correctly, but introduce space between. 再次使用flex-box ,你可以使用justify-content: space-between来保持标题从最左边的框到最右边的框正确,但是在它们之间引入空格。 JSFiddle . JSFiddle

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

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