Responsive circle and image fit on circle

一笑奈何 提交于 2020-07-16 06:01:28

问题


I want to create a responsive circle and I want to fit the image. I want to use img tag not with css (background)

Here is what i've tried

.circular--portrait {
  position: relative;
  width: 200px;
  height: 200px;
  overflow: hidden;
  border-radius: 50%;
}

.circular--portrait img {
  width: 100%;
  height: auto;
}

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

回答1:


.circular--portrait {
  position: relative;
  width: 20vw;
  height: 20vw;
  overflow: hidden;
  border-radius: 50%;
}
.circular--portrait img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
<div class="circular--portrait">
  <img src="http://beerhold.it/500/300" />
</div>



回答2:


Here's a solution that has the image be responsive according to the width of its parent container.

.container {
  /* Feel free to adjust the values here so you can see it being responsive */
  width: 200px;
}

.circle {
  position: relative;
  width: 100%;
  height: 0;
  padding: 100% 0 0;
  border-radius: 50%;
  overflow: hidden;
  border: 1px solid gray;
}

.circle img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
<div class="container">
  <div class="circle">
    <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Chapultepec_Zoo_-_Jaguar_%2802%29.jpg/2560px-Chapultepec_Zoo_-_Jaguar_%2802%29.jpg" />
  </div>
</div>

You can try it out on CodePen




回答3:


You can try to do it with SCSS. You just need to create one variable.

$width: calc(100vw / 5);
.circle {
  width: $width;
  height: $width;
  background: url('https://images.pexels.com/photos/17679/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350');
  border-radius: 50%;
  float: left;
  margin: 5px;
}
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>
<div class="circle"></div>

please see the fiddle




回答4:


You can try to do it with SCSS. You just need to create one variable.

$width: calc(100vw / 5);
.circle {
  width: $width;
  height: $width;
  border-radius: 50%;
  float: left;
  margin: 5px;
  background: red;
  overflow: hidden;
}
<div class="circle">
  <img src="https://images.pexels.com/photos/17679/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350" alt="">
</div>
<div class="circle">
  <img src="https://images.pexels.com/photos/17679/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350" alt="">
</div>
<div class="circle">
  <img src="https://images.pexels.com/photos/17679/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350" alt="">
</div>
<div class="circle">
  <img src="https://images.pexels.com/photos/17679/pexels-photo.jpg?auto=compress&cs=tinysrgb&h=350" alt="">
</div>
Please see the fiddle

来源:https://stackoverflow.com/questions/41570348/responsive-circle-and-image-fit-on-circle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!