我想知道如果有一個唯一的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