【问题标题】:CSS circular image at any size任意大小的 CSS 圆形图像
【发布时间】:2020-01-21 08:10:06
【问题描述】:

我正在使用 CSS 创建一个圆形图像。

当我的图片是 200px x 300px 时,它不是一个完美的圆:

.circular {
  margin: auto;
  width: 200px;
  height: 300px;
  border-radius: 150px;
  -webkit-border-radius: 150px;
  -moz-border-radius: 150px;
  background: url(http://i.stack.imgur.com/S1ZCs.jpg) no-repeat;
}
<div class="circular"></div>

这里也是jsFiddle。

有没有办法在宽高不同的情况下做一个完美的圆?如果我将其设置为 300 x 300,则它是一个正圆,但当图像为 200 x 300 时,我需要它处于正圆中。

【问题讨论】:

  • 我不明白为什么如果你需要圆形,为什么需要不同的尺寸?
  • 有没有在jsfiddle中不调整宽度或高度的情况下做一个圆圈
  • 你能做到的最好是这样的:jsfiddle.net/sy48k3y5/3
  • 我认为这是不可能的,因为圆的半径相等。但我认为您应该尝试计算哪个值更大,即宽度或高度。无论哪个更大,使用该维度/2 作为您的半径。简单

标签: jquery html css


【解决方案1】:

如果值不是正方形,则无法将其设为圆形,就这么简单。但是,如果您将图像嵌套在其中,您可以将包装器设为方形并隐藏圆圈的剩余部分。应该是这样的:

.circular {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  position: relative;
  overflow: hidden;
}
.circular img {
  min-width: 100%;
  min-height: 100%;
  width: auto;
  height: auto;
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
<div class="circular">
  <img src="http://www.placehold.it/150" />
</div>

我通常使用:nth-child(n)selector 将position: 和transform: 之间的所有行包含在一个单独的CSS 定义中,因此居中仅适用于支持nth-child 的浏览器,但这只是微调。

【讨论】:

  • +1 - 请原谅编辑。这是一个很好的方法,Stack Snippet 很容易看到。
  • @misterManSam 很好(起初我不确定发生了什么变化)。我将对其进行编辑以添加一些前缀,因为这不起作用并且我正在使用 safari(如果它对我不起作用,那么它有点尴尬:p)
  • 什么是我们不知道宽度的确切像素,例如如果我使用宽度为 10% 那么您的代码将无法正常工作
  • @aravindreddy 是的,如果您使用百分比,则没有任何效果 - 这是可以理解的,因为高度百分比很少与宽度匹配。如果你想这样做,你可能想将包装器定义为width: 10vw、height: 10vw。如果你不能使用这些值,你最后的办法是在 JS 中计算它们。
【解决方案2】:

有一种方法,使用transform: scale,可以将其变形。我不知道你为什么需要它......无论如何它都在这里。比例值显然需要随着宽度/高度而变化。

另一个问题是使用此方法会扭曲背景图像。

div {
  width: 200px;
  height: 300px;
  background: #F00;
  border-radius: 50%;
  transform: scale(.76, .5)
}
&lt;div&gt;&lt;div&gt;

【讨论】:

  • 是的,这太复杂了,不明白为什么要使用它,因为您现在必须计算两件事!
  • +1 不错的方法,这里的问题是您需要知道比例才能设置变换值。
  • @Danko 是的,接受的解决方案相当简单。我没有在……圈子之外思考。
【解决方案3】:

我们可以通过稍微改变一下 HTML 结构来实现它。

<div class="circular">
   <img src="img/desktop.jpg">
</div>

.circular { /* need to give parent container equal width and height*/
    margin: auto;
    width: 200px;
    height: 200px;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    border-radius: 50%;
    overflow:hidden;
}
.circular img{
    width:100%;
}

演示http://jsfiddle.net/prakashup/2sb6srjp/

【讨论】:

    【解决方案4】:

    据我所知,要使用border-radius 属性实现圆形,您的宽度和高度值必须相同。

    【讨论】:

    • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方发表评论 - 您可以随时评论自己的帖子,一旦您有足够的reputation,您就可以comment on any post。
    • 理解并道歉
    【解决方案5】:

    您可以使用border-radius:100%。这可用于创建圆形或椭圆形。也可以随时使用border-radius与元素宽度直接相关

    【讨论】:

      【解决方案6】:

      在您的示例中,图像的大小无关紧要,因为它是背景图像。不适合的部分将不会被渲染。如果您需要一个 300 像素 x 300 像素的圆圈,那么只需将高度和宽度保留为 300。 您可以使用background-size:cover; 使图像拉伸以适应但保留纵横比。

      See here.

      .circular {
      margin: auto;
      width: 300px;
      height: 300px;
      border-radius: 50%;
      -webkit-border-radius: 50%;
      -moz-border-radius: 50%;
      background: url(https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSJVLN-vcVHCOOLWmvihkfex1OFW0ZLVhQ7JQfcc4EH4YoJLTn68x8EpDls) no-repeat;
          background-size: cover;
      }
      <div class="circular"></div>
      <br/><br/>
      <div>Original image: <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSJVLN-vcVHCOOLWmvihkfex1OFW0ZLVhQ7JQfcc4EH4YoJLTn68x8EpDls" /></div>

      【讨论】:

        【解决方案7】:

        您可以通过使高度和宽度值相同并将边框半径定义为 50% 来简单地实现此目的。这是代码..

        .circular {
        margin: auto;
        width: 300px;
        height: 300px;
        border-radius: 50%;
        -webkit-border-radius: 50%;
        -moz-border-radius: 50%;
        background: url(http://cdn.bavotasan.com/wp-content/uploads/2011/02/desktop.jpg) no-repeat center center;
        }
        

        【讨论】:

          【解决方案8】:

          你可以用伪元素来制作它

          .circular {
            margin: auto;
            width: 200px;
            height: 300px;
            overflow: hidden;
            background: red;
          }
          .circular:before {
            content: url('http://cdn.bavotasan.com/wp-content/uploads/2011/02/desktop.jpg');
            border-radius: 100%;
            -webkit-border-radius: 100%;
            -moz-border-radius: 100%;
            width: 200px;
            height: 200px;
            overflow: hidden;
            display: block;
          }
          &lt;div class="circular"&gt;&lt;/div&gt;

          【讨论】:

            【解决方案9】:

            如果我在 scss 而不是 css 中工作,以下是我拥有一个最大直径为 150 像素的圆形容器的方法,并且图像完全包含在这个圆形容器中,这样最终图像不会折叠成椭圆形:

            .circular-container {
                max-width: 150px;
                max-height: 150px;
                width: 100%;
                height: auto;
                border-radius: auto;
            
                img {
                    max-width: 150px;
                    width: auto;
                    max-height: 150px;
                    height: 100%;
                    border-radius: 50%;
                    border: 10px solid transparent;
                    margin-bottom: 0px;
                    object-fit: scale-down;
                    }
                }
            

            css 中的等价物是:

            .circular-container {
                max-width: 150px;
                max-height: 150px;
                width: 100%;
                height: auto;
                border-radius: auto;
            
            .circular-container img {
                max-width: 150px;
                width: auto;
                max-height: 150px;
                height: 100%;
                border-radius: 50%;
                border: 10px solid transparent;
                margin-bottom: 0px;
                object-fit: scale-down;
            }
            

            【讨论】:

              猜你喜欢
              • 2012-05-19
              • 1970-01-01
              • 1970-01-01
              • 2016-05-07
              • 2012-05-28
              • 2014-04-27
              • 2014-12-12
              • 1970-01-01
              • 2014-09-26
              相关资源
              最近更新 更多