CSS矩形图像的圆形裁剪


72

我想从矩形照片制作一个居中的圆形图像。照片的尺寸未知。通常是矩形形式。我尝试了很多方法:

CSS:

.image-cropper {
    max-width: 100px;
    height: auto;
    position: relative;
    overflow: hidden;
}

.image-cropper img{
    display: block;
    margin: 0 auto;
    height: auto;
    width: 150%; 
    margin: 0 0 0 -20%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -ms-border-radius: 50%;
    -o-border-radius: 50%;
    border-radius: 50%;  
}

HTML:

<div class="image-cropper">
    <img src="http://www.electricvelocity.com.au/Upload/Blogs/smart-e-bike-side_2.jpg" class="rounded" />
</div>


您有两次用不同的值指定边距的原因吗?
gleenn

可能想通过删除“通常”来纠正问题,如果不是,那么所有CSS解决方案都不会解决。
Dejan.S

Answers:


103

该方法是错误的,您需要将应用于border-radius容器div而不是实际的图像。

这将工作:

.image-cropper {
  width: 100px;
  height: 100px;
  position: relative;
  overflow: hidden;
  border-radius: 50%;
}

img {
  display: inline;
  margin: 0 auto;
  height: 100%;
  width: auto;
}
<div class="image-cropper">
  <img src="https://via.placeholder.com/150" class="rounded" />
</div>


8
明白了 但是,为什么图像不在圆的中心?
49volro 2014年

1
真正。我认为Hiral的解决方案更适合您的需求,并且Wordpress可以轻松动态地提供背景图像。由于某些原因,我无法使用text-align解决我的解决方案:因此,目前我无法真正帮助您进行居中:(
Johnny Kutnowski 2014年

1
此解决方案不适用于横向图像。
Wolfgang Ziegler

1
@ 49volro如果要使图像居中,则将其左边缘留出-25%即可解决问题。
Cameron637 '16

这会使图像偏向侧面
彼得,俄罗斯,

42

object-fit属性提供了一种非骇客的方法(以图片为中心)。除了Internet Explorer,它已经在主要浏览器中受支持了几年(2013年以来为Chrome / Safari,自2015年以来为Firefox,自2015年以来为Edge)。

img.rounded {
  object-fit: cover;
  border-radius: 50%;
  height: 100px;
  width: 100px;
}
<img src="http://www.electricvelocity.com.au/Upload/Blogs/smart-e-bike-side_2.jpg" class="rounded">


1
辉煌!我喜欢这种简单的一步式解决方案。我必须img从您提供的css中删除并将其作为类添加到图像中,但是它的工作原理很吸引人!谢谢!
jord8 on

23

如果您可以不用<img>标签住,建议您将照片用作背景图像。

.cropcircle{
    width: 250px;
    height: 250px;
    border-radius: 100%;
    background: #eee no-repeat center;
    background-size: cover;
}

#image1{
    background-image: url(http://www.voont.com/files/images/edit/7-ridiculous-ways-boost-self-esteem/happy.jpg);
}
<div id="image1" class="cropcircle"></div>


10

尝试这个:

img {
    height: auto;
    width: 100%;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -ms-border-radius: 50%;
    -o-border-radius: 50%;
    border-radius: 50%;
}

演示在这里。

要么:

.rounded {
    height: 100px;
    width: 100px;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    -ms-border-radius: 50%;
    -o-border-radius: 50%;
    border-radius: 50%;
    background:url("http://www.electricvelocity.com.au/Upload/Blogs/smart-e-bike-side_2.jpg") center no-repeat;
    background-size:cover;
}

演示在这里。


2
您的演示不是圆形的。
Paulie_D 2014年

很好,但是我不能使用,background-image因为图像的URL会生成Wordpress。我可以这样写:<div class="image-cropper"> <img class="rounded" style="background:url('http://www.electricvelocity.com.au/Upload/Blogs/smart-e-bike-side_2.jpg') center no-repeat; background-size:cover;" /> </div>
49volro 2014年

@ 49volro在这种情况下,您可以这样编写:<div class =“ rounded” style =“ background-image:url('image url')”> </ div>,从css中删除图像URL
Hiral,2014年

这个例子对我有用,除了它破坏了我的形象。如果将图像放在div标签中并在div中指定图像尺寸,则可以正常工作。
John D. Cook

如果您的示例不是圆形的,即使您不能将其用作背景图像,它也会呈椭圆形怎么办
Aravind Reddy

2

约翰尼的解决方案很好。我发现添加min-width:100%确实可以帮助图像填充整个圆圈。您可以结合使用JavaScript来达到最佳效果,或者如果真的很想解决问题,请使用ImageMagick- http://www.imagemagick.org/script/index.php

.image-cropper {

  width: 35px;

  height: 35px;

  position: relative;

  overflow: hidden;

  border-radius: 50%;

}

.image-cropper__image {

  display: inline;

  margin: 0 auto;

  height: 100%;

  min-width: 100%;

}
<div class="image-cropper">
  <img src="#" class="image-cropper__image">
</div>


0

我知道上面提到的许多解决方案都可行,您也可以尝试使用flex。

但是我的图像是矩形的,无法正确拟合。所以这就是我所做的。

.parentDivClass {
    position: relative;
    height: 100px;
    width: 100px;
    overflow: hidden;
    border-radius: 50%;
    margin: 20px;
    display: flex;
    justify-content: center;
}

对于里面的图像,您可以使用

child Img {
    display: block;
    margin: 0 auto;
    height: 100%;
    width: auto;
}

当您使用bootstrap 4类时,这将很有帮助。


0

我能够做到这一点的最好方法是使用新的CSS object-fit(1)属性和padding-bottom(2)hack。

您需要在图像周围使用包装元素。您可以使用任何想要的东西,但是我喜欢使用新的HTMLpicture标记。

.rounded {
  display: block;
  width: 100%;
  height: 0;
  padding-bottom: 100%;
  border-radius: 50%;
  overflow: hidden;
}

.rounded img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}


/* These classes just used for demo */
.w25 {
  width: 25%;
}

.w50 {
  width: 50%;
}
<div class="w25">
<picture class="rounded">
  <img src="https://i.imgur.com/A8eQsll.jpg">
</picture>
</div>

<!-- example using a div -->
<div class="w50">
<div class="rounded">
  <img src="https://i.imgur.com/A8eQsll.jpg">
</div>
</div>

<picture class="rounded">
  <img src="https://i.imgur.com/A8eQsll.jpg">
</picture>

参考文献

  1. CSS图像大小,如何填充,不拉伸?

  2. 使用CSS保持div的长宽比


-1

插入图像,然后反手需要做的就是:

<style>
img {
  border-radius: 50%;
}
</style>

**图像代码将自动出现在这里**


OP希望有一个“圆形”,您的示例仅使边缘变圆,如果图像为矩形,则结果将为椭圆形。
stldoug

-2

您需要使用jQuery来做到这一点。这种方法使您能够拥有动态图像,并且无论大小如何都可以进行动态处理。

我的演示现在有一个缺陷,我没有将图像居中放置在容器中,而是在一分钟之内无法恢复(需要完成我正在处理的脚本)。

演示

<div class="container">
    <img src="" class="image" alt="lambo" />
</div>

//script
var container = $('.container'),
    image = container.find('img');

container.width(image.height());


//css    
.container {
    height: auto;
    overflow: hidden;
    border-radius: 50%;    
}

.image {
    height: 100%;    
    display: block;    
}

1
绝对不需要jQuery。请参阅公认的答案或@ Tom's
abettermap,2017年

@abettermap这些解决方案没有动态高度,如果您读到“通常是矩形形式”这个问题。并非总是如此,因此我使用javascript获得了图像高度。汤姆斯解决方案失去了Seo的价值。因此,在投票之前也请阅读并考虑最佳实践。
Dejan.S

使用的答案background-image具有动态高度,即无论高度如何,图像都将填充圆形空间,我认为这是OP的目标。关于最佳实践,我认为有些人可能会认为,不使用JS进行样式设计将是最佳实践。不过,您对SEO有一点看法,因此我将编辑您的回复。随时进行进一步编辑。
abettermap '17
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.