【问题标题】:How to "crop" a rectangular image into a square with CSS?如何使用 CSS 将矩形图像“裁剪”成正方形?
【发布时间】:2013-02-16 12:48:07
【问题描述】:

我知道实际上用 CSS 修改图像是不可能的,这就是为什么我将crop 放在引号中。

我想做的是拍摄矩形图像并使用 CSS 使它们看起来是方形的,而不会扭曲图像。

我基本上想转这个:

进入这个:

【问题讨论】:

  • 这些图片是 div 的背景图片还是保留在 标签中对 SEO 很重要?
  • 您尝试过什么了吗?使用 CSS3 background-position 或带有 overflow:hidden 的旧包装器 div 和具有相对定位的图像,这相当简单。
  • 它们肯定是背景图片
  • 看我的回答,我认为这是你整体上最好的选择。它避免了定位元素。

标签: html css image crop


【解决方案1】:

没有包装器div或其他无用代码的纯CSS解决方案:

img {
  object-fit: cover;
  width:230px;
  height:230px;
}

【讨论】:

  • 请注意,不幸的是,这不适用于 IE 和 Edge atm。有关详细信息,请参见此处:stackoverflow.com/a/37792830/1398056
  • 什么是我们不知道要使其与自动宽度成正方形的高度
  • 截至 2018 年 6 月,似乎只有 IE11 (2.71%) 不支持 object-fit,对我来说已经足够了。
  • 2019 年的支持现在非常好。只有 2.3% 的人仍在使用 IE 11。这个解决方案非常简单,我忍不住要使用它,因为其他解决方案太痛苦了,我已经浪费了好几个小时试图让它与后端代码一起工作。
  • 是时候忘记IE了
【解决方案2】:

假设它们不必在 IMG 标签中...

HTML:

<div class="thumb1">
</div>

CSS:

.thumb1 { 
  background: url(blah.jpg) 50% 50% no-repeat; /* 50% 50% centers image in div */
  width: 250px;
  height: 250px;
}

.thumb1:hover { YOUR HOVER STYLES HERE }

编辑:如果 div 需要链接到某个地方,只需调整 HTML 和样式,如下所示:

HTML:

<div class="thumb1">
<a href="#">Link</a>
</div>

CSS:

.thumb1 { 
  background: url(blah.jpg) 50% 50% no-repeat; /* 50% 50% centers image in div */
  width: 250px;
  height: 250px;
}

.thumb1 a {
  display: block;
  width: 250px;
  height: 250px;
}

.thumb1 a:hover { YOUR HOVER STYLES HERE }

请注意,这也可以修改为响应式,例如百分比宽度和高度等。

【讨论】:

  • 这是使用单个标签定位和裁剪的好方法。
  • 还要注意,在打印时,mast 浏览器会禁用背景图像,因此它们不会出现。
  • 它也可以处理 :hover 状态。如果 div 需要链接到某个地方,只需添加一个 a 标签。至于打印,可以用 print.css 修复吗?如果我错了,请纠正我?
  • 实际上,老实说,在这种情况下,带有 A 标记的 print 将有一个回退,无论如何,您都希望添加 CSS 样式来修复此问题以进行打印。
  • 没关系,我把它改成了height: 460px; width: 100%;,它就像一个魅力
【解决方案3】:
  1. 将您的图片放在一个 div 中。
  2. 为您的 div 提供明确的方形尺寸。
  3. 将 div 上的 CSS 溢出属性设置为隐藏 (overflow:hidden)。
  4. 将您的想象放在 div 中。
  5. 利润。

例如:

<div style="width:200px;height:200px;overflow:hidden">
    <img src="foo.png" />
</div>

【讨论】:

  • 必须确保居中或至少在其中调整图像的定位。 OP 示例看起来居中(尽管我只是提到了这一点,并不指望你会改变你的答案:P)。
  • @rlemon - 然后 OP 可以将 div 的位置设置为相对位置,将图像的位置设置为绝对位置,并调整顶部和左侧属性。
  • 我只是在有人之前提到它; “但现在一切都对齐了!” - :P
  • 是的,它居中很重要
【解决方案4】:

如果图像位于具有响应式宽度的容器中:

.rect-img-container {
  position: relative;
}

.rect-img-container::after {
  content: "";
  display: block;
  padding-bottom: 100%;
}

.rect-img {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
<div class="rect-img-container">
  <img class="rect-img" src="https://picsum.photos/id/0/367/267" alt="">
</div>

(编辑:从 sass 更新为纯 css) (编辑:添加虚拟图像以供参考)

【讨论】:

  • 这真是太好了。它也非常灵活,因为一旦图像被平方,您就可以将其圈起来并做其他很酷的事情。
  • 无法告诉您您刚刚为我节省了多少小时的工作时间。非常感谢你。对于任何困惑的人,这可能是 SASS/SCSS - 如果您使用的是直接 CSS,这篇文章会告诉您如何转换:stackoverflow.com/questions/26760776/…
  • 很好的答案。在我的例子中,我有一个锚标记作为容器,我只需要添加一个 display: block 属性,因为图像最初没有显示。
  • 从 SASS 转换为 CSS ` .img-container { position: relative; } .img-container::after { 内容:“”;显示:块;底部填充:100%; } .img 容器 img { 位置:绝对;宽度:100%;高度:100%;适合对象:封面;左:0;顶部:0; }`
【解决方案5】:

使用 background-size:cover - http://codepen.io/anon/pen/RNyKzB

CSS:

.image-container {
  background-image: url('http://i.stack.imgur.com/GA6bB.png');
  background-size:cover;
  background-repeat:no-repeat;
  width:250px;
  height:250px;
}  

标记:

<div class="image-container"></div>

【讨论】:

  • 结合mtronics答案的居中能力,它运行良好,即使在IE9上。
【解决方案6】:

我最近实际上遇到了同样的问题,并最终采用了稍微不同的方法(我无法使用背景图片)。虽然它确实需要一点点 jQuery 来确定图像的方向(我相信你可以使用纯 JS 代替)。

我写了一个blog post about it如果你有兴趣了解更多但是代码很简单:

HTML:

<ul class="cropped-images">
  <li><img src="http://fredparke.com/sites/default/files/cat-portrait.jpg" /></li>
  <li><img src="http://fredparke.com/sites/default/files/cat-landscape.jpg" /></li>
</ul>

CSS:

li {
  width: 150px; // Or whatever you want.
  height: 150px; // Or whatever you want.
  overflow: hidden;
  margin: 10px;
  display: inline-block;
  vertical-align: top;
}
li img {
  max-width: 100%;
  height: auto;
  width: auto;
}
li img.landscape {
  max-width: none;
  max-height: 100%;
}

jQuery:

$( document ).ready(function() {

    $('.cropped-images img').each(function() {
      if ($(this).width() > $(this).height()) {
        $(this).addClass('landscape');        
      }
    });

});

【讨论】:

  • 我认为这优于 CSS 背景替代方案,因为图像是内容,而不是“样式”,因此它们应该保留在 HTML 层上。将它们放在 CSS 而不是 HTML 上也会对您网页的 SEO 产生影响。谢谢!
  • 改善这一点的一个好主意是在每个循环中添加$(this).load(function(){...,这样 jQuery 会稍等片刻,直到图像加载并获得真实的图像尺寸。
【解决方案7】:

object-fit: cover 将完全满足您的需求。

但它可能不适用于 IE/Edge。按照如下所示使用 just CSS 修复它,使其适用于所有 浏览器

我采用的方法是使用 absolute 将图像放置在容器内,然后使用以下组合将其放在中心

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

一旦它在中心,我给图像,

// For vertical blocks (i.e., where height is greater than width)
height: 100%;
width: auto;

// For Horizontal blocks (i.e., where width is greater than height)
height: auto;
width: 100%;

这使得图像得到Object-fit:cover的效果。


这里是上述逻辑的演示。

https://jsfiddle.net/furqan_694/s3xLe1gp/

此逻辑适用于所有浏览器。


原图

垂直裁剪

水平裁剪


【讨论】:

    【解决方案8】:

    我遇到了类似的问题,无法“妥协”背景图片。 我想出了这个。

    <div class="container">
        <img src="http://lorempixel.com/800x600/nature">
    </div>
    
    .container {
        position: relative;
        width: 25%; /* whatever width you want. I was implementing this in a 4 tile grid pattern. I used javascript to set height equal to width */
        border: 2px solid #fff; /* just to separate the images */
        overflow: hidden; /* "crop" the image */
        background: #000; /* incase the image is wider than tall/taller than wide */
    }
    
    .container img {
        position: absolute;
        display: block;
        height: 100%; /* all images at least fill the height */
        top: 50%; /* top, left, transform trick to vertically and horizontally center image */
        left: 50%;
        transform: translate3d(-50%,-50%,0);
    }
    
    //assuming you're using jQuery
    var h = $('.container').outerWidth();
    $('.container').css({height: h + 'px'});
    

    希望这会有所帮助!

    示例: https://jsfiddle.net/cfbuwxmr/1/

    【讨论】:

      【解决方案9】:

      查看 CSS aspect-ratio

      https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio

      .square-image{
        width: 50%;
        background-image: url('https://picsum.photos/id/0/367/267');
        background-size: cover;
        background-position: center;
        aspect-ratio: 1/1;
      }
      &lt;div class="square-image"&gt;&lt;/div&gt;

      【讨论】:

        【解决方案10】:

        使用 CSS:溢出:

        .thumb {
           width:230px;
           height:230px;
           overflow:hidden
        }
        

        【讨论】:

        • 这些尺寸不是很方正。 :-)
        • 不,你是对的。呃。我只是从 OP 的图像大小中提升现有高度,就像某个星期五下午烧坏的机器人一样。
        • 嘿。我在那里和你在一起。 :-)
        【解决方案11】:

        要么使用带有 .testimg 类的正方形尺寸的 div,其中的图像:

        .test {
        width: 307px;
        height: 307px;
        overflow:hidden
        }
        
        .testimg {
            margin-left: -76px
        
        }
        

        或带有图像背景的方形 div。

        .test2 {
        width: 307px;
        height: 307px;
            background: url(http://i.stack.imgur.com/GA6bB.png) 50% 50%
        }
        

        这里有一些例子:http://jsfiddle.net/QqCLC/1/

        已更新至图像中心

        .test {
          width: 307px;
          height: 307px;
          overflow: hidden
        }
        
        .testimg {
          margin-left: -76px
        }
        
        .test2 {
          width: 307px;
          height: 307px;
          background: url(http://i.stack.imgur.com/GA6bB.png) 50% 50%
        }
        <div class="test"><img src="http://i.stack.imgur.com/GA6bB.png" width="460" height="307" class="testimg" /></div>
        
        <div class="test2"></div>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-12-12
          • 2012-08-13
          • 1970-01-01
          • 2020-08-27
          • 1970-01-01
          • 1970-01-01
          • 2023-03-21
          相关资源
          最近更新 更多