mea*_*gar 56
您可以将图像设置为元素的背景,将其背景大小设置为cover,然后使用border-radius来舍入边缘.
#avatar {
/* This image is 687 wide by 1024 tall, similar to your aspect ratio */
background-image: url('http://i.stack.imgur.com/Dj7eP.jpg');
/* make a square container */
width: 150px;
height: 150px;
/* fill the container, preserving aspect ratio, and cropping to fit */
background-size: cover;
/* center the image vertically and horizontally */
background-position: top center;
/* round the edges to a circle with border radius 1/2 container size */
border-radius: 50%;
}Run Code Online (Sandbox Code Playgroud)
<div id="avatar"></div>Run Code Online (Sandbox Code Playgroud)
Bro*_*sig 24
另一个解决方案是设置img的大小并使用"object-fit:cover;".它将与"background-size:cover"相同,而不会弄乱背景图像.
img {
object-fit: cover;
border-radius:50%;
width: 150px;
height: 150px;
}Run Code Online (Sandbox Code Playgroud)
<img src="http://i.stack.imgur.com/Dj7eP.jpg" />Run Code Online (Sandbox Code Playgroud)
我在Chris Nager的帖子上找到了它.- 1
编辑:正如@prograhammer所提到的,这对IE不起作用.Edge仅在<img>标签上支持它.
Edge中的部分支持仅指
object-fit支持<img>- 2
编辑2: 这普里莫兹Cigler的岗位展示了如何使用Modernizr的增加对IE一个后备支持,但是,在这种情况下,你将需要添加一个包装脱图像.
Pau*_*e_D 19
如果图像需要在HTML而不是背景图像中
.wrapper {
width:150px;
height:150px;
margin: 25px auto;
overflow: hidden;
border-radius:50%;
position: relative;
}
.wrapper img {
position: absolute;
top:50%;
left:50%;
transform: translate(-50%,-50%)
}Run Code Online (Sandbox Code Playgroud)
<div class="wrapper">
<img src="http://lorempixel.com/output/business-q-c-640-480-4.jpg" alt="" />
</div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37114 次 |
| 最近记录: |