7

我有一个支架 div,它收缩包裹在其他几个 div 周围,因此支架没有固定宽度。支架内部还有另一个包含文本的 div。当文本过多时,它会扩展支架,因此不再收缩包装。这是我的问题的演示,http://jsfiddle.net/WSbAt/。这是一个照片库页面。

我在想可能没有办法不设置支架的宽度,我不能这样做,因为每行的图像数量是动态的。我可能可以用 jQuery 解决这个问题,但希望有一个 css 解决方案。

编辑:我试图不指定任何宽度,因为每行缩略图的数量取决于您的窗口大小。

演示:http: //jsfiddle.net/WSbAt/

CSS:

#container
{
    width:600px; /*only set for demo. will be random on live page*/
    border:1px solid black;
    text-align:center;
}
#holder
{
    display: inline-block;
    border:2px solid blue;
}
.thumbnail
{
    float:left;
    width:100px;
    height:100px;
    background-color:red;
    margin-right:10px;
    margin-bottom:10px;
}
.expandedHolder
{
    padding:10px;
    border:2px solid yellow;
    margin-bottom:10px;
    margin-right:10px;
}
.fullImage
{
    float:left;
    width:250px;/*only set for demo. will be random on live page*/
    height:200px;
    background-color:green;
}
.text
{
    float:left;
    margin-left:10px;
}

HTML:

<div id="container">
    <div id="holder">
        <div class="thumbnail"></div>
        <div class="thumbnail"></div>
        <div class="thumbnail"></div>
        <div class="thumbnail"></div>
        <div style="clear:both;"></div>
        <div class="expandedHolder">
            <div class="fullImage"></div>
            <div class="text">some text here. some text here. some text here. </div>
            <div style="clear:both;"></div>
        </div>
        <div style="clear:both;"></div>
    </div>
</div>
4

3 回答 3

3

使用百分比作为 .fullImage 和 .text 的宽度

        .fullImage
        {
            float:left;
            width:70%;
            height:200px;
            background-color:green;
        }
        .text
        {
            width: auto;
            float:left;
            width:20%;
            margin-left:10px;
        }

然后,如果文本变得很大,请使用 text-overflow

text-overflow:ellipsis; 

溢出

overflow:hidden;


http://jsfiddle.net/RubenJonker/WSbAt/5/

于 2013-04-26T13:41:33.107 回答
0

我正在尝试使用纯 CSS 做同样的事情。我发现了一种适用于 Chrome 的 hacky 方式(我还没有检查其他浏览器)。

#container {
    display: table-caption; /* Shrink the container */
    border:1px solid black;
    text-align:center;
}
于 2015-09-24T09:38:12.873 回答
-5

我使用 jQuery 来计算文本宽度应该是多少。如果有人好奇,这就是你将如何解决我的例子。

var textDiv = $(".text");
textDiv.hide();//hide the text to get holder width without text

var holderWidth = $("#holder").width();
var imageWidth = $(".fullImage").width();
var textWidth = (holderWidth - imageWidth) - 45;

textDiv.css("width", textWidth + "px");
textDiv.show();
于 2013-04-26T20:18:18.390 回答