[英]Floating repeated divs, how to keep XY together
I am currently creating many figures using the JavaScript library D3 (but I don't think D3 has any relevance for my problem). 我目前正在使用JavaScript库D3创建许多图形(但我认为D3与我的问题没有任何关系)。 The figure is placed in div X
and the text explaining the figure is in div Y
. 该图放在div X
,说明该图的文本在div Y
。 I basically want to create a pattern like this: 我基本上想创建一个像这样的模式:
XYXYXY
XYXYXY
but instead (depending on how wide my window since I do not want to fix the width), what I get this: 但是相反(取决于我不想固定宽度的窗口宽度),我得到的是:
XYXYX
YXYXY
I tried putting XY
in a parent div Z<XY>
, so that every pair of XY
stays together, but that does not work. 我尝试将XY
放在父div Z<XY>
,以使每对XY
保持在一起,但这不起作用。 I also don't think clearing is necessarily the answer here, but I have tried all combinations without success. 我也认为清除并不一定是解决问题的办法,但我尝试了所有组合但均未成功。
Any help is greatly appreciated. 任何帮助是极大的赞赏。
Try 尝试
white-space: nowrap
You may also have to change the floats to for your XY divs: 您可能还需要将XY div的float更改为:
display: inline-block
If I understood the problem correctly, you don't need to use float
. 如果我正确理解了该问题,则无需使用float
。 Display the divs as inline blocks: display: inline-block
. 将div显示为嵌入式块: display: inline-block
。
That will flow the divs as "character blocks" doing the wrap, you'll need to have a parent for the XY to keep the text together with the image. 这将使div作为“字符块”进行换行,您需要为XY提供父级,以将文本和图像保持在一起。
An example: http://jsfiddle.net/D9BAv/ 范例: http : //jsfiddle.net/D9BAv/
HTML: HTML:
<div class="figure">
<div class="picture"></div>
<div class="text">Example 1</div>
</div><!-- reapeated ... -->
CSS: CSS:
.figure {
display: inline-block;
}
.picture {
width: 3rem;
height: 3rem;
margin:auto;
background-color: blue;
}
If I have understood you correctly, maybe this will work. 如果我对您的理解正确,那么也许可以。 You could also use display: inline-block
instead of float: left
if you don't need to support IE8 and below. 如果您不需要支持IE8及以下版本,也可以使用display: inline-block
而不是float: left
。
http://jsfiddle.net/GQ8Uw/ http://jsfiddle.net/GQ8Uw/
HTML HTML
<div class="cont">
<div class="x">X</div><div class="y">Y</div>
</div><div class="cont">
<div class="x">X</div><div class="y">Y</div>
</div><div class="cont">
<div class="x">X</div><div class="y">Y</div>
</div><div class="cont">
<div class="x">X</div><div class="y">Y</div>
</div><div class="cont">
<div class="x">X</div><div class="y">Y</div>
</div>
<div class="cont">
<div class="x">X</div><div class="y">Y</div>
</div>
CSS CSS
.cont {
width: 100px;
float: left;
}
.x, .y {
width: 50%;
float: left;
}
.x {
background: #ccc;
}
.y {
background: #ecc;
}
Ok, I solved the problem. 好的,我解决了问题。 So I was wrong, it did have something to do with D3. 所以我错了,它确实与D3有关。 Each time, I was essentially adding a child div to the same parent, and therefore the inline-block simply had no effect. 每次,我本质上都是在同一个父对象中添加一个子div,因此,内联块根本没有任何作用。
I ended up adding a "last-child" feature in my code like "d3.select(".figure:last-child").append(...", for both the picture and the text, and it works perfectly. 我最终在代码中添加了“ last-child”功能,例如“ d3.select(“。figure:last-child”)。append(...“),用于图片和文本,并且效果很好。
I saw the problem by adding a border around the parent div, and I noticed that all children were in the same div. 我通过在父div周围添加边框看到了问题,并且我注意到所有子级都在同一个div中。 I then found the solution from: What is the equivalent of jQuery's $(".cell:first") in D3? 然后我从以下位置找到了解决方案: D3中jQuery的$(“。cell:first”)等效于什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.