在SO上有很多关于保持元素宽高比(使用flexbox或不使用)的问题。然而,我的问题略有不同,因为我想覆盖子图像元素的宽高比:
- 确保图像完全覆盖元素(
object-fit: cover
) - 确保元素是1:1的(即一个完美的圆形)
- 确保溢出的图像被隐藏
换句话说,图像必须表现得就像它是一个元素的背景一样(尽管我不能将它们用作背景图像),该元素的宽高比始终为1:1并且具有响应性。
在下面的示例中,除了元素根据它们的图像后代进行调整之外,一切都很正常。但是我希望它们保持1:1的比例,这样我就可以得到完美的圆形。(第一行的中间元素必须比其他元素大。)
HTML不能更改,但我可以使用现代CSS属性,例如object-fit和flexbox。(只要Chrome / Firefox的最新版本支持它。)
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.img-gallery {
background: #fafafa;
padding: 24px;
min-width: 320px;
width: 90%;
margin: 0 auto;
}
.img-gallery .row {
display: flex;
flex-wrap: nowrap;
justify-content: space-around;
align-items: center;
}
.img-gallery a {
display: block;
text-decoration: none;
background-image: linear-gradient(60deg, #004494 0%, #7db9e8 78%, #c2dfed 100%);
overflow: hidden;
border-radius: 50%;
padding: 3px;
flex: 1;
margin: 0 24px;
transition: padding 200ms;
}
.img-gallery a:hover,
#s_country .img-gallery .row:first-of-type a:nth-child(2):hover {
padding: 0;
}
.img-gallery a:hover span {
transform: scale(1.25);
}
.img-gallery .row:first-of-type a:not(:nth-child(2)) {
width: 30%;
width: calc((60% - 96px) / 2);
}
.img-gallery .row:first-of-type a:nth-child(2) {
flex: 2;
padding: 4px;
}
.img-gallery span {
width: 100%;
height: 100%;
display: block;
border-radius: 50%;
position: relative;
overflow: hidden;
transition: transform 250ms;
z-index: 2;
}
.img-gallery img {
width: 100%;
height: 100%;
object-fit: cover;
}
.img-gallery span::before {
content: "";
background-image: linear-gradient(60deg, transparent 48%, #ffc5e7 100%);
width: 100%;
height: 100%;
position: absolute;
z-index: 2;
border-radius: 50%;
opacity: .72;
}
<div class="img-gallery">
<div class="row">
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/b3/9c/54/b39c54776074d07ee0b567826768730a.jpg" id="img-1-3"></span></a>
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/d6/df/51/d6df512a2f15f517767b4d82d2d97a4c.jpg" id="img-1-4"></span></a>
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/ec/a9/dd/eca9dd106a04cdbee399870252ef711f.jpg" id="img-1-5"></span></a>
</div>
<div class="row">
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/7d/01/19/7d0119a2fec989e208f288326c7cad0f.jpg" id="img-1-6"></span></a>
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/d8/c3/32/d8c332d09b03673845b2e92a48816233.jpg" id="img-1-7"></span></a>
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/88/3b/dd/883bddab14168f5f0807fec021002d8d.jpg" id="img-1-8"></span></a>
</div>
<div class="row">
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/8e/4f/bb/8e4fbb89b155d15521b80d1baf9290d1.jpg" id="img-1-9"></span></a>
</div>
</div>
演示 Terry 的代码不能正常工作的示例:风景图片。
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.img-gallery {
background: #fafafa;
padding: 24px;
min-width: 320px;
width: 90%;
margin: 0 auto;
}
.img-gallery .row {
display: flex;
flex-wrap: nowrap;
justify-content: space-around;
align-items: center;
}
.img-gallery a {
display: block;
text-decoration: none;
background-image: linear-gradient(60deg, #004494 0%, #7db9e8 78%, #c2dfed 100%);
overflow: hidden;
border-radius: 50%;
padding: 3px;
flex: 1;
margin: 0 24px;
transition: padding 200ms;
}
.img-gallery .row:first-of-type a:not(:nth-child(2)) {
width: 30%;
width: calc((60% - 96px) / 2);
}
.img-gallery .row:first-of-type a:nth-child(2) {
flex: 2;
padding: 4px;
}
.img-gallery span {
height: 0;
display: block;
border-radius: 50%;
position: relative;
padding-bottom: 100%;
overflow: hidden;
transition: transform 250ms;
z-index: 2;
}
.img-gallery img {
width: 100%;
object-fit: cover;
transition: transform 250ms;
}
.img-gallery a:hover img {
transform: scale(1.25);
}
.img-gallery span::before {
content: "";
background-image: linear-gradient(60deg, transparent 48%, #ffc5e7 100%);
width: 100%;
height: 100%;
position: absolute;
z-index: 2;
border-radius: 50%;
opacity: .72;
}
<div class="img-gallery">
<div class="row">
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/b3/9c/54/b39c54776074d07ee0b567826768730a.jpg" id="img-1-3"></span></a>
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/d6/df/51/d6df512a2f15f517767b4d82d2d97a4c.jpg" id="img-1-4"></span></a>
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/ec/a9/dd/eca9dd106a04cdbee399870252ef711f.jpg" id="img-1-5"></span></a>
</div>
<div class="row">
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/7d/01/19/7d0119a2fec989e208f288326c7cad0f.jpg" id="img-1-6"></span></a>
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/d8/c3/32/d8c332d09b03673845b2e92a48816233.jpg" id="img-1-7"></span></a>
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/13/7c/3d/137c3d3bd9f25aa9d2677136d9336d74.jpg" id="img-1-8"></span></a>
</div>
<div class="row">
<a href="#" title="Show large image"><span><img itemprop="image" src="https://s-media-cache-ak0.pinimg.com/originals/8e/4f/bb/8e4fbb89b155d15521b80d1baf9290d1.jpg" id="img-1-9"></span></a>
</div>
</div>