首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSS --使用肖像图像在圆圈内显示,而不失真

CSS --使用肖像图像在圆圈内显示,而不失真
EN

Stack Overflow用户
提问于 2016-04-05 13:11:02
回答 3查看 2.7K关注 0票数 3

我有以下几点:

代码语言:javascript
复制
<div class="profile-picture">
  <img src="http://thumb9.shutterstock.com/display_pic_with_logo/239779/154705646/stock-photo-portrait-of-real-man-face-looking-at-camera-on-blue-background-154705646.jpg" alt="Profile - James Hales">
</div>

CSS

代码语言:javascript
复制
.profile-picture img {
    border-radius: 50%;
    margin-top: 0;
    margin-bottom: 20px;
    height: 392px;
    width: 250px;
}

我希望配置文件图片是一个完美的圆圈与图像放置在里面。有没有一种方法基本上是裁剪图像与CSS,以便它将'x‘的像素数量从底部,以便图像显示在一个圆圈内,而不扭曲图像?

JSFIDDLE

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-04-05 14:38:06

这里有两种方法来实现你想要的。第二张图片以图像为中心,而不是在顶部垂直对齐。

代码语言:javascript
复制
.profile-picture {
    position:relative;
    overflow:hidden;
    border-radius: 50%;
    height: 200px;
    width: 200px;
}

.profile-picture img {
    position:absolute;
    height: auto;
    width: auto;    
    max-width:100%;
}

.centered img {
    position:absolute;
    top:50%;
    transform:translateY(-50%);
}
代码语言:javascript
复制
<div class="profile-picture">
  <img src="http://thumb9.shutterstock.com/display_pic_with_logo/239779/154705646/stock-photo-portrait-of-real-man-face-looking-at-camera-on-blue-background-154705646.jpg" alt="Profile - James Hales">
</div>
<div class="profile-picture centered">
  <img src="http://thumb9.shutterstock.com/display_pic_with_logo/239779/154705646/stock-photo-portrait-of-real-man-face-looking-at-camera-on-blue-background-154705646.jpg" alt="Profile - James Hales">
  </div>

票数 4
EN

Stack Overflow用户

发布于 2016-04-05 13:27:02

您可以使用object-fit,但是support不是很好

代码语言:javascript
复制
.profile-picture {
  border-radius: 50%;
  margin-top: 0;
  border: 1px solid black;
  margin-bottom: 20px;
  height: 250px;
  width: 250px;
  overflow: hidden;
}
img {
  object-fit: cover;
  width: 100%;
  height: 100%;
  display: block;
}
代码语言:javascript
复制
<div class="profile-picture">
  <img src="http://thumb9.shutterstock.com/display_pic_with_logo/239779/154705646/stock-photo-portrait-of-real-man-face-looking-at-camera-on-blue-background-154705646.jpg" alt="Profile - James Hales">
</div>

或者您可以将img设置为背景

代码语言:javascript
复制
.profile-picture {
  border-radius: 50%;
  margin-top: 0;
  border: 1px solid black;
  margin-bottom: 20px;
  height: 250px;
  width: 250px;
  overflow: hidden;
  background: url("http://thumb9.shutterstock.com/display_pic_with_logo/239779/154705646/stock-photo-portrait-of-real-man-face-looking-at-camera-on-blue-background-154705646.jpg");
  background-size: cover;
  background-position: center;
}
代码语言:javascript
复制
<div class="profile-picture"></div>

票数 0
EN

Stack Overflow用户

发布于 2016-04-05 13:27:06

Flexbox可以做到这一点。当然,容器必须是正方形的。

代码语言:javascript
复制
.profile-picture {
  border-radius: 50%;
  margin-top: 0;
  border: 1px solid black;
  margin-bottom: 20px;
  height: 250px;
  width: 250px;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}
img {
  display: block;
}
代码语言:javascript
复制
<div class="profile-picture">
  <img src="http://thumb9.shutterstock.com/display_pic_with_logo/239779/154705646/stock-photo-portrait-of-real-man-face-looking-at-camera-on-blue-background-154705646.jpg" alt="Profile - James Hales">
</div>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36427375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档