将容器div的宽度与浮动div的宽度总和对齐

9

我有以下的HTML:

<div class="main_container">
    <div class="sub_container">
        <div class="floating">wookie1</div>
        ...
        <div class="floating">wookie5</div>
    </div>
</div>

将其视为图库。

main_container的大小不固定,它被设置为用户分辨率的百分比。 我希望sub_container的宽度正好等于浮动div的总宽度。

如果我对sub_container使用"display:table;",对浮动div使用"display: inline-block;",那么它可以正常工作:

enter image description here

直到列表中有足够的div,使宽度总和大于main_container并在下一行断开:

enter image description here

但是,我希望子容器(黄色背景)始终以div的宽度之和为准确宽度,即使它们跨越多行,就像这样:

enter image description here

我已经谷歌了几个小时,但是没有找到一个优雅的解决方案(如果可能的话只使用css)。

这里是jsfiddle,可以玩一下。


我不理解这个问题。.main_container具有固定的宽度,小div元素的总和大于其宽度。 - galchen
也许我表达不够清晰。我希望 sub_container 始终具有浮动 div 宽度之和的确切宽度,即使 div 的数量换行。 - Sebastien
然后它们会断到另一行。但是子容器仍然不需要100%的宽度。 - Sebastien
我同意其他人的观点,仅使用CSS似乎无法实现这个功能。 - Matt Coughlin
你对SASS/LESS感到舒适吗?我认为它们可以做到这一点。 - user1685185
显示剩余5条评论
5个回答

5

纯CSS解决方案

问题在于,要使项目“知道”换行到下一行,容器必须“填满”其可用的水平空间,即您的.main_container宽度。然而,您希望背景不超出实际元素所需的范围。因此,我使用元素本身通过拼凑伪元素来创建背景。

这是 jsfiddle 链接(在 Win 7 机器上测试了 IE9、Firefox 18 和 Chrome 24)

我正在使用每个单独的.floating元素拼凑背景,这使得最右边的元素成为背景大小的右边界控制(请注意 fiddle 中的两个不同宽度示例)。

下面的注释中给出了每个部分的说明。有两个关键限制需要注意:

  1. .floating不能设置position,因此根据特定应用程序,这是一个潜在的限制。
  2. background需要是纯色或纯垂直定向的“运动”(即从上到下渐变的渐变或水平线条)。

关键 CSS(带有说明性注释)

.sub_container {
    border-left: 1px solid #333; /* forms its own left border */
    overflow: hidden; /* needed for background and border to work */ 
    position: relative; /* positioning background off this */
    z-index: 1; /* needs a stacking context */
}

.floating {
    font-size:20px;
    line-height:30px;
    padding:0 5px 0 5px;
    border: 1px solid black;
    margin: 3px;
    display: inline-block;
    /* NOTE: CANNOT be given positioning  */
}

.floating:after {
    content: '';
    position: absolute; /* will position off subcontainer */
    border-top: 1px solid black; /* makes the top/bottom border itself */
    border-bottom: 1px solid black;
    z-index: -1; /* push it to the background */
    top: 0; /* set it to top of sub_subcontainer */
    bottom: 0; /* set it to bottom of sub_container */
    margin-left: -100%; /* shove it past the far left edge of sub_container */
    /* next, use padding to bring it back to its position at the end
       of the text string of .floating */
    padding-left: 100%;
    /* next, add enough padding on the right to compensate for the right
       padding, right margin, and right border of .floating */
    padding-right: 9px; 
    background-color: yellow; /* set your background color */
    /* NOTE: this will not work with a background image that
       has any horizonal differences to it (top to bottom 
       gradient would probably be okay) */
}

.floating:before { /* make right border */
    content: '';
    padding-top: 10000px; /* give it some obscene height that will not be exceeded */
    margin: -5000px 0; /* counter the padding height by half size margins top/bottom */
     /* next, push this behind the background with an even lower z-index
        to hide it if it is not the right most element beign used to 
        form the right border */
    z-index: -2;
    border-right: 1px solid black; /* make the right border */
    float: right; /* get the before element to the right */
    position: relative; /* needs to be adjusted in position */
    right: -9px; /* move it same as padding-right of the after element */
    display: block; /* give it a display */
}

太棒了!!我分叉了这个小发明,使它更接近我想要的:http://jsfiddle.net/3svfj/2/。您正在进行验证和悬赏。最后一个问题:我们可以在main_container上使用overflow: auto来使其工作吗?到目前为止我失败了。 - Sebastien
@Seb37:只是为了确保我清楚,你之前说过main_container将由浏览器窗口大小确定(我相信)。你想让它的宽度与窗口一样,但高度上有overflow-y吗?还是你想要两个方向都溢出(在这种情况下,我必须问,你想让里面的元素何时换行到新行)? - ScottS
只有在高度上溢出。是的,我希望main_container的大小由浏览器窗口确定。 - Sebastien
@Seb37:您的fiddle中有很多关键设置没有完成才使它可以运行。背景颜色不能在sub_container上,以使其“适应”内容的宽度。此外,我发现这不允许将text-align:center放在包装元素上(但可以放在floating元素本身上)。这是已调整的fiddle链接。 然而,如果您只是想要对齐并居中,那么您实际上不需要您提出问题,所有的工作都白费了(几乎)。 - ScottS
我不仅希望将 sub_container 居中对齐。我需要 sub_container 定义特定的样式(阴影框、背景等)。我实际上有一个设计,其中页面的宽度分为三个元素:左菜单(固定宽度)、右菜单(固定宽度)和占据剩余宽度的中心区域。我不能控制 main_container 的大小,但我想在其中添加一个 sub_container,以为画廊添加一些背景和样式,所以我真的需要将 sub_container div 设置为我想要的确切宽度。我还需要能够在主容器上使用 overflow-y。谢谢你的工作。 - Sebastien

2

我尝试解决这个问题时感到有些无聊,于是创建了一个基于jQuery的JS脚本来解决它。

var t = $(".sub_container").width()/$(".floating").outerWidth();
$(".sub_container").width(parseInt(t) * $(".floating").outerWidth());

Demo


好的,祝你好运。如果找不到,你可以退而求其次。 :) - Starx
@Seb37 Starx说得对,使用HTML/CSS无法解决这个问题。最好遵循jQuery代码,这样会更好,并且完全兼容浏览器。我也给你点赞! - w3uiguru
好的,这是我目前正在使用的解决方法。如果在悬赏结束之前我没有得到更好的答案,我将验证这个解决方案。 - Sebastien

2
重新阅读你的问题...由于你不会承诺任何事情(如最大宽度:80%,500像素等),我按照你的示例破坏了第四个子元素。
CSS
.main_container, .sub_container, .floating { outline:1px solid black }
.sub_container { background-color:yellow; display:table; padding-left:5px }
.floating {
    float:left;
    padding:5px;
    margin:5px 5px 5px 0;
}
.floating:nth-child(4n+5) {
    clear:left;
    float:left;
}

HTML

<div class="main_container">
    <div class="sub_container">
        <div class="floating">wookie1</div>
        <div class="floating">wookie2</div>
        <div class="floating">wookie3</div>
        <div class="floating">wookie4</div>
        <div class="floating">wookie5</div>
        <div class="floating">wookie6</div>
        <div class="floating">wookie7</div>
        <div class="floating">wookie8</div>
        <div class="floating">wookie9</div>
    </div>
</div>

这里输入图片描述

编辑(选项2)
我不认为仅通过HTML/CSS就可以完成你想做的事情。我提供另一种解决方案,基于我的猜测...以及你的“图库评论”。

CSS

.main_container, .sub_container, .floating { outline:1px solid black }
.main_container {
    text-align:center
}
.sub_container { background-color:yellow; display:inline-block; max-width:80%; }
.floating {
    display:inline-block;
    padding:5px;
    margin:5px;
}

HTML

<div class="main_container">
    <div class="sub_container">
        <div class="floating">wookie1wookie1wookie1</div>
        <div class="floating">wookie2</div>
        <div class="floating">wookie3</div>
        <div class="floating">wookie4</div>
        <div class="floating">wookie5</div>
        <div class="floating">wookie6wookie6</div>
        <div class="floating">wookie7wookie7wookie7</div>
        <div class="floating">wookie8</div>
        <div class="floating">wookie9</div>
    </div>
</div>

它不会使.subcontainer贴合内容; 然而,(使用max-width) 它允许您为其提供一些与主容器的间距,其子元素可以是各种尺寸。

enter image description here


这实际上是我得到的最有教育意义的答案。但不幸的是,这还不够:我无法控制main_container的大小,因为它是根据用户分辨率设置的百分比。否则,我可以通过一些LESS/SASS定义来调整main_container的宽度,以完美匹配4个浮动div的宽度。我仍将编辑问题。 - Sebastien
添加了另一种解决你问题的方法。这不完全是你想要的,但我认为使用纯HTML/CSS实现你想要的功能是不可能的。 - Dawson
显示:表格;这就是解决方案。 - FARHAD AFSAR

0
删除主容器的宽度和.sub_container的绝对定位,你就可以顺利进行了。
.main_container {
    #width:380px;
}

.sub_container {
    #position: absolute;
}

我仍然希望main_container有它的宽度,我只想让sub_container完全适应div总和的宽度。 - Sebastien
我明白了,尝试将max-width: 335px;添加到.main_container中。 - jakmarkiewicz
这不是我想要去的地方。实际上,我希望那些div在需要时可以分成几行。我想要的是sub_container仍然适合div的宽度。我编辑了问题,现在可能更清楚了。 - Sebastien
我明白了,我认为纯CSS实现这个可能是不可能的。你可能需要尝试用jQuery来解决,可以参考这个链接:https://dev59.com/63E95IYBdhLWcg3wY81l - jakmarkiewicz

0
请使用元素代替
元素来定义“sub_container”。
.sub_container{background-color:yellow;}     

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接