简体   繁体   English

使用CSS强制浮动第1列中的超宽元素出现在浮动第2列下方

[英]Using CSS to force an over-wide element in a floated column-1 appear below a floated column-2

I have a two-column layout, composed of fixed-width floated divs. 我有一个两列的布局,由固定宽度的div组成。 Inside the first column is an extra wide element. 第一列内部是一个超宽元素。 So the html layout looks something like this: 所以html布局看起来像这样:

<div id="container">
    <div id="column1">
        ...short content...
        <div id="extra-wide">
            ...wide content...
        </div>
    </div>
    <div id="column2">
        ...long content...
    </div>
</div>

As annotated, the first column contains a short amount of content and the second column contains a long amount of content. 如注释所示,第一列包含少量内容,第二列包含大量内容。 The CSS looks something like this: CSS看起来像这样:

#column1 { width: 200px; }
#column2 { width: 100px; }
#extra-wide { width: 250px; }

So the extra-wide element is actually wider than its parent, column1. 因此,超宽元素实际上比其父元素column1宽。 And I don't know the heights of any of the elements ahead of time -- they are all variable. 而且我不知道任何元素的高度提前-它们都是可变的。

The issue here is that column2 appears overlapped over the extra-wide element in column1. 这里的问题是column2看起来与column1中的超宽元素重叠。 Here's a jsfiddle to help visualize this: http://jsfiddle.net/e3KNg/ (This fiddle has some content inserted and colors added to make what's happening more clear). 这是一个jsfiddle来帮助形象化: http : //jsfiddle.net/e3KNg/ (此小提琴中插入了一些内容,并添加了颜色以使发生的事情更加清晰)。 Here's a screenshot: 这是屏幕截图:

column2重叠的超宽

Without altering the basic HTML structure or the widths of the three elements, is it possible (without the aid of Javascript) to force the extra-wide element in column1 to appear below column2? 在不更改基本HTML结构或这三个元素的宽度的情况下,是否有可能(无需借助Javascript)强制将column1中的超宽元素显示在column2之下? Here is the effect I'm trying to achieve: 这是我想要达到的效果:

column2向下推超宽

I know this can be done by rearranging html elements or by using Javascript, but I'm looking for a solution within the above constraints. 我知道可以通过重新排列html元素或使用Javascript来完成,但是我正在寻找上述约束范围内的解决方案。 I tried using clearing divs in various places, removing or adjusting floats, and trying some overflow settings, but could not achieve the effect I was looking for. 我尝试在各个位置使用div清除,删除或调整浮点数以及尝试一些溢出设置,但无法达到我想要的效果。

looking at it, without set heights on the content elements or using javascript to work their heights out or set positioning on the extra-wide element, what you are looking for can't be achieved. 看着它,如果没有在内容元素上设置高度,或者没有使用JavaScript来计算它们的高度,或者没有在超宽元素上设置位置,就无法实现您所寻找的目标。

Heres a fiddle which works using an absolute positioning of the extra wide element and a top value, as well as using some clever css to make the columns the same height. 这是一个小提琴,它使用超宽元素的绝对位置和最高值,以及使用一些巧妙的CSS使列具有相同的高度来工作。

http://jsfiddle.net/yKRu4/1/ http://jsfiddle.net/yKRu4/1/

As i siad this works, but i honestly feel what you are looking for can't be achieved with the restrictions you have put in place. 正如我所说的那样,但老实说,我觉得您所寻求的限制无法实现。

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

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