我想知道如果有一个唯一的CSS的解决方案,使内容扩展到适合自己的容器,无论是图像或文本,就像这样:是否HTML/CSS支持自动缩放内容以匹配/填充容器?
.container {
display: inline-block;
width: 2in;
height: 1in;
font-size: fit-to-container /* <- what should this be? */
}
<div class="container">Some Text</div>
<div class="container">Another container with significantly
more content, and so the font should scale down to fit</div>
或者,图片:
.container {
display: inline-block;
width: 2in;
height: 1in;
overflow: resize-to-fit /* <- not really, overflow can't do it */
}
<div class="container">
<img src="one.png"/>
</div>
<div class="container">
<img src="one.png"/>
<img src="two.png"/>
<img src="three.png"/>
<img src="four.png"/>
<img src="five.png"/>
<img src="six.png"/>
<img src="seven.png"/>
<img src="eight.png"/>
</div>
作为视觉例如,参见https://jsfiddle.net/jarrowwx/jemxLqyL/94/。但那个小提琴并不完美,而且创作过于复杂。它使用SVG是因为我正在试验将内容制作为单个图像,然后设置max-width
,但这并不奏效,它只是裁剪图像。
有一个纯CSS解决方案来创建这种效果呢?
是的,这是可能的。最好的方法是使用[flexboxes](https://css-tricks.com/snippets/css/a-guide-to-flexbox/)。 – pol
这里是一个更新的小提琴:https://jsfiddle.net/jarrowwx/jemxLqyL/148/ - 柔性盒似乎不能自动缩小图像。它似乎缩小了内容的范围,但不缩小内容本身。如果我错过了一些东西,请告诉我我错过了什么。 –
[Here's something](http://stackoverflow.com/questions/30788131/css3-flexbox-maintain-image)帮助您处理图像高度:宽度比问题。特别是一个答案,非常简单,你只需要添加'object-fit:contains;',但它只能在Firefox 36 +,Chrome 31 +和Opera 11.6+上使用'-o-object-fit:contains ;'。 [看看](https://jsfiddle.net/jemxLqyL/150/)。当然,你可以寻找一个后备。 [这是一个使用](http://stackoverflow.com/questions/33473978/alternate-option-object-fit)'背景'属性('背景大小:包含在你的情况),做的伎俩。 – pol