javascript 我们可以强制一个 div 元素通过 css 来“包装”吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6651845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:24:41  来源:igfitidea点击:

Can we force a div element to "wrap" by css?

javascripthtmlcss

提问by jAndy

(Final solution below)

下面的最终解决方案



I'm pretty sure it's not possible the way I imagine it, but there are so many smart, clever and gentle people on Stackoverflow, I'll give it a shot.

我很确定这不可能像我想象的那样,但是 Stackoverflow 上有这么多聪明、聪明和温柔的人,我会试一试。

I have container-divelements which get filled with one or more "child-div" elements. The containers are more or less "virtual", actually they only should show which children belong together. Since I don't know how many child's are placed within each parent I cannot work with a fixed width. Those elements should be able to float to handle window resizing gracefully.

我有container-div填充了一个或多个 " child-div" 元素的元素。容器或多或少是“虚拟的”,实际上它们只应该显示哪些孩子属于一起。由于我不知道每个父级中放置了多少个孩子,因此我无法使用固定宽度。这些元素应该能够浮动以优雅地处理窗口大小调整。

My goal is, to display the same amount of "child-divs" in a row regardless in which parent-div they are. My current problem is, that the floatcauses the whole parent-container to wrap, so the question is, is there any possible way to "saw" a div, like this:

我的目标是,连续显示相同数量的“子 div”,而不管它们在哪个父 div 中。我目前的问题是,float导致整个父容器包装,所以问题是,有没有可能“看到” a 的方法div,如下所示:

Before

enter image description here

在此处输入图片说明

After

enter image description here

在此处输入图片说明

Does that make any sense ? Here is my current playground:

这有任何意义吗 ?这是我目前的游乐场:

http://jsfiddle.net/5FXBu/

http://jsfiddle.net/5FXBu/



After lots of experiments together with Gaby aka G. Petrioli(thanks dude), I finally created this solution: http://jsfiddle.net/5FXBu/11/

经过与Gaby aka G. Petrioli 的大量实验(感谢老兄),我终于创建了这个解决方案:http: //jsfiddle.net/5FXBu/11/

采纳答案by Gabriele Petrioli

If the container-divare just for grouping and have little effect on the graphical design you can just set them as inline. Setting the child-divas inline-blockallows to pass some of the layout requirement of the container-divto the child-div(margins mainly..)

如果container-div仅用于分组并且对图形设计影响不大,则可以将它们设置为inline. 设置child-divasinline-block允许将一些布局要求传递container-divchild-div主要是边距..

example at http://jsfiddle.net/gaby/pvWHr/

示例在http://jsfiddle.net/gaby/pvWHr/