问题
I have a holder div that's shrink wrapped around a couple other divs, so the holder doesn't have a set width. Also inside the holder is another div that contains text. When there's too much text it's expanding the holder so it's not shrink wrapped anymore. Here's a demo of my problem, http://jsfiddle.net/WSbAt/. This is for a photo gallery page.
I'm thinking there might not be a way without setting the width of the holder, which I can't do since the number of images per row is dynamic. I can probably figure it out with jQuery, but was hoping there's a css solution.
Edit: I'm trying not to specify any widths since the number of thumbnails per row is based on your window size.
Demo: 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>
回答1:
Use a percentage as width for .fullImage and .text
.fullImage
{
float:left;
width:70%;
height:200px;
background-color:green;
}
.text
{
width: auto;
float:left;
width:20%;
margin-left:10px;
}
Then if the text gets to large use text-overflow
text-overflow:ellipsis;
or overflow
overflow:hidden;
http://jsfiddle.net/RubenJonker/WSbAt/5/
回答2:
I'm trying to do the same, with pure CSS. I found a hacky way that works in Chrome (I haven't check other browsers yet).
#container {
display: table-caption; /* Shrink the container */
border:1px solid black;
text-align:center;
}
回答3:
I used jQuery to calculate what the text width should be. If anyone is curious, this is how you would fix my example.
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();
来源:https://stackoverflow.com/questions/16237874/prevent-text-from-expanding-shrink-wrapped-div