【问题标题】:CSS Circular Cropping of Rectangle Image矩形图像的 CSS 圆形裁剪
【发布时间】:2014-12-12 19:35:05
【问题描述】:

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

代码

.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%;  
}
<div class="image-cropper">
   <img src="https://sf1.autojournal.fr/wp-content/uploads/autojournal/2012/07/4503003e3c38bc818d635f5a52330d.jpg" class="rounded" />
</div>

【问题讨论】:

  • 我坐得很好??? jsfiddle.net/7c9wjLy6/3
  • 是否有理由使用不同的值两次指定边距?
  • 可能想通过删除“通常”来纠正这个问题,如果是这样,那么没有一个 css 解决方案不会解决它。

标签: html css


【解决方案1】:

方法不对,需要将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>

【讨论】:

  • 明白。但是为什么图片不在圆圈的中心呢?
  • 是的。我认为 Hiral 的解决方案更适合您的需求,并且背景图像可以轻松地由 Wordpress 动态提供。出于某种原因,我无法使用 text-align: center; 解决我的解决方案。所以我目前无法真正帮助您进行居中:(
  • 此解决方案不适用于横向图像。
  • @49volro 如果你想让图像居中,给它一个 -25% 的左边距就可以了。
  • 这会使图像不平衡
【解决方案2】:

object-fit 属性提供了一种非骇客的方式来执行此操作(图像居中)。几年来,主流浏览器都支持它(Chrome/Safari 自 2013 年以来,Firefox 自 2015 年以来,Edge 自 2015 年以来),但 Internet Explorer 除外。

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

【讨论】:

  • 太棒了!我喜欢这个简单的一步解决方案。我必须从您提供的 css 中删除 img 并将其作为一个类添加到图像中,但它就像一个魅力!谢谢!
  • 2021 年依然有用,谢谢!!!
  • 这太好了,谢谢!应该是接受的答案 imo
【解决方案3】:

如果你可以不用&lt;img&gt;标签,我建议你使用这张照片作为背景图片。

.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);
}
&lt;div id="image1" class="cropcircle"&gt;&lt;/div&gt;

【讨论】:

    【解决方案4】:

    试试这个:

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

    DEMO here.

    或者:

    .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;
    }
    

    DEMO here.

    【讨论】:

    • 你的演示不是循环的。
    • 很好,但我不能使用background-image,因为图片的 URL 会生成 Wordpress。我可以写:&lt;div class="image-cropper"&gt; &lt;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;" /&gt; &lt;/div&gt; 吗?
    • @49volro 在这种情况下你可以这样写:<div class="rounded" style="background-image:url('image url')"></div>,从 css 中删除图片 url
    • 这个例子对我有用,但它破坏了我的形象。如果我将图像放在 div 标签中并在 div 中指定图像尺寸,它会起作用。
    • 如果你的例子不是圆形的,它是椭圆形的,但你不能用作背景图片怎么办
    【解决方案5】:

    Johnny 的解决方案很好。我发现添加 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>

    【讨论】:

      【解决方案6】:

      一个简单的班轮。

      clip-path: circle();
      

      【讨论】:

        【解决方案7】:

        我知道上面提到的很多解决方案都可以,你也可以试试 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 类时很有帮助。

        【讨论】:

          【解决方案8】:

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

          您需要一个围绕图像的包装元素。你可以使用任何你想要的东西,但我喜欢使用新的 HTML picture 标签。

          .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 Image size, how to fill, not stretch?

          2. Maintain the aspect ratio of a div with CSS

          【讨论】:

            【解决方案9】:

            接受的答案可能适用于某些情况,但这取决于矩形和任何预定样式的比例。

            我使用这种方法是因为它比仅使用object-fit 的解决方案更兼容:

            .image-cropper {
               width: 150px;
               height: 150px;
               position: relative;
               overflow: hidden;
               border-radius: 50%;
               border:2px solid #f00;
            }
            
            /* Common img styles in web dev environments */
            img {
               height: auto;
               max-width: 100%;
            }
            
            /* Center image inside of parent */
            img.center {
               position: absolute;
               top: 50%;
               left: 50%;
               transform: translate(-50%, -50%);
            }
            
            /* For horizontal rectangles */
            img.horizontal {
               height: 100%;
               width: auto;
               max-width: 9999px; /* max-content fall back */
               max-width: max-content;
            }
            <div class="image-cropper">
              <img src="https://via.placeholder.com/300x600" class="center" />
            </div>
            
            <div class="image-cropper">
              <img src="https://via.placeholder.com/600x300" class="horizontal center" />
            </div>

            如果你运行 sn-p 你可以看到,对于水平矩形,我们添加了另一个类.horizontal

            我们覆盖 max-width 以允许 img 大于 100% 的宽度。这样可以保留纵横比,防止图像被拉伸。

            但是,图像不会居中,这就是 .centered 类的用武之地。它使用了一个很好的居中技巧来将图像在垂直和水平方向上绝对定位在中心。

            More information on the centering at CSS Tricks

            您很可能并不总是知道图像的比例,所以这就是为什么我建议使用 javascript 来定位 img 并在需要时添加 .horizontal 类。

            Here is a stack overflow answer that would work

            【讨论】:

              【解决方案10】:

              您需要使用 jQuery 来执行此操作。这种方法使您能够拥有动态图像,并且无论大小如何,都可以进行圆形处理。

              我的演示现在有一个缺陷,我没有将容器中的图像居中,但我会在一分钟内返回它(需要完成我正在处理的脚本)。

              DEMO

              <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;    
              }
              

              【讨论】:

              • 绝对不需要 jQuery。查看接受的答案或@Tom 的
              • @abettermap 那些解决方案没有动态高度,如果您阅读问题,“通常它是一个矩形形式。”这并不总是意味着,因此我使用 javascript 获得图像高度。汤姆斯解决方案失去了SEO价值。因此,在投反对票之前也要阅读并思考最佳实践。
              • 使用background-image 的答案具有动态高度,因为无论高度是多少,图像都会填充圆形空间,我认为这是 OP 的目标。回复:最佳实践,我认为有些人可能会认为不使用 JS 进行样式设置是最佳实践。但是,您对 SEO 有意见,因此我将编辑您的回复。随意进一步编辑。
              【解决方案11】:

              插入图片,然后反手你只需要:

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

              **图片代码会自动出现在这里**

              【讨论】:

              • OP 想要一个“圆形”,您的示例只是对边缘进行四舍五入,如果图像是矩形,则结果将是椭圆形。
              猜你喜欢
              • 2013-10-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-05-01
              • 1970-01-01
              • 1970-01-01
              • 2021-06-12
              相关资源
              最近更新 更多