將圖像放入Flexbox細胞比例尺中而不定義尺寸?將圖像製作在flexbox細胞比例尺中...
我在flexbox中構建了一行,它有一個圖像作爲頂部元素,然後是下面文本的div容器。相當標準的東西。該行正在前往一個cms,因此,這就是問題所在。
客戶端將能夠改變圖像,因此我不知道圖像有多大。因此設置任何寬度或高度屬性是不可能的。此外,該行被設計爲適合任何屏幕,因此在更大的屏幕上,我根本不知道容器需要多寬。
我在codepen上做實驗,但是當圖像很大時,它似乎強制下一個對象向下。我顯然喜歡將圖像縮放到最適合父容器的位置。
HTML
<section class="cards">
<article class="card">
<a href="#">
<figure class="thumbnail">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1047234/library-placehold.jpg" alt="">
</figure>
<div class="card-content">
<h2>Title</h2>
<p>Placeholder text</p>
</div><!-- CLOSE CARD CONTENT -->
</a><!-- CLOSE LINK -->
</article><!-- CLOSE ARTICLE -->
<article class="card">
<a href="#">
<figure class="thumbnail">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1047234/library-placehold.jpg" alt="">
</figure>
<div class="card-content">
<h2>Title</h2>
<p>Placeholder text</p>
</div><!-- CLOSE CARD CONTENT -->
</a><!-- CLOSE LINK -->
</article><!-- CLOSE ARTICLE -->
<article class="card">
<a href="#">
<figure class="thumbnail">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/1047234/library-placehold.jpg" alt="">
</figure>
<div class="card-content">
<h2>Title</h2>
<p>Placeholder text</p>
</div><!-- CLOSE CARD CONTENT -->
</a><!-- CLOSE LINK -->
</article><!-- CLOSE ARTICLE -->
</section><!-- CLOSE SECTION -->
SASS
section.cards
width: 90%
margin: 50px auto
display: flex
flex-wrap: wrap
justify-content: space-between
flex-direction: row
article.card
background: #ffffff
margin-bottom: 2em
flex: 0 1 calc(33% -1em)
a
color: #000000
text-decoration: none
&:hover
box-shadow: 3px 3px 8px hsl(0, 0%, 70%)
.thumbnail
display: flex
justify-content: center
align-items: center
img
height: 100%
width: 100%
object-fit: contain
.card-content
padding: 1.4em
h2
margin-top: 0
margin-bottom: .5em
font-weight: normal
p
font-size: 95%