【问题标题】:Problem with responsive CSS circle div containing image包含图像的响应式 CSS 圆形 div 的问题
【发布时间】:2019-01-06 12:56:19
【问题描述】:

我在连续创建 9 个响应式 CSS 圆圈时遇到问题,其中每个圆圈内部都包含一个 img 标签(不是背景图像)。 img 将根据父圆 div 的大小居中和调整大小。这 9 个圆圈连续显示并包含在一个网格容器内,每个圆圈都分配给它自己的单元格。这 9 个(数字 5)的中心是其他圆圈的两倍。

对于我较小的圆形容器,我使用以下 CSS:

border: blue 1px solid;
border-radius: 50%;
width: 100%;
height: 0%;
padding-top: 100%;
margin:auto;
overflow: hidden;

这是基于我在网上找到的各种教程和其他堆栈溢出帖子中的建议。

img 标签的 CSS 如下:

width: 100%;
height: 100%;
object-fit: cover;

此处基于本教程:https://medium.com/@chrisnager/center-and-crop-images-with-a-single-line-of-css-ad140d5b4a87

图像未显示 - 我相信是因为圆形容器中的填充。但是我不知道如何改变这一点。如果我删除填充并改为向容器添加高度,它们将保持圆形并且图像按预期显示,但它们不再响应 - 这意味着如果页面大小发生变化,它们会变为椭圆形。

这里是有问题的组件:https://jsfiddle.net/jth3rb6m/

【问题讨论】:

  • 你有演示可以给我们看吗?
  • @StevenB。我刚刚用 JSFiddle 进行了编辑
  • height: 0%; padding-top: 100%; 这是不正确的,没有定义高度就无法填充图像并且填充不正确,填充是创建内部空间,正确的应该是边距-顶部,因为您要删除顶部组件。也不建议让圆圈的大小以百分比表示,会根据分辨率而失真。
  • @AndersonMendes 那么如何在不使用百分比的情况下让圆圈做出响应?此外,填充是在调整浏览器大小时保持圆形形状的技巧。我在几个帖子中发现了这一点,甚至是其他堆栈溢出帖子。您可以在这里查看例如:codeitdown.com/css-circles 您是否有建议添加到我的 JSfiddle 以达到预期的结果?

标签: html css responsive


【解决方案1】:

图片不是圆形的问题是由于图片不是方形的。

.friend-cont {
  display: grid;
  justify-content: space-around;
  grid-template-columns: calc(10% - 5px) calc(10% - 5px) calc(10% - 5px) calc(10% - 5px) calc(20% - 5px) calc(10% - 5px) calc(10% - 5px) calc(10% - 5px) calc(10% - 5px);
  grid-column-gap: 5px;
  grid-template-rows: auto;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  margin: auto;
}

.friend-pic {
  border: blue 1px solid;
  border-radius: 50%;
  width: 100%;
  height: auto;
  /* height: 0%; */
  /* padding-top: 100%; */
  margin: auto;
  overflow: hidden;
}

.friend-photo {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.profile-pic {
  border: blue 1px solid;
  border-radius: 50%;
  /* height: 0%; */
  /* padding-top: 100%; */
  margin: auto;
  overflow: hidden;
}
<div class="friend-cont">

  <div class="profile-pic">
    <img class="friend-photo" src="https://i.stack.imgur.com/l60Hf.png">
  </div>

  <div class="friend-pic">
    <img class="friend-photo" src="https://i.stack.imgur.com/l60Hf.png">
  </div>

  <div class="friend-pic">
    <img class="friend-photo" src="https://i.stack.imgur.com/l60Hf.png">
  </div>

  <div class="friend-pic">
    <img class="friend-photo" src="https://i.stack.imgur.com/l60Hf.png">
  </div>

  <div class="friend-pic">
    <img class="friend-photo" src="https://i.stack.imgur.com/l60Hf.png">
  </div>

  <div class="friend-pic">
    <img class="friend-photo" src="https://i.stack.imgur.com/l60Hf.png">
  </div>

  <div class="friend-pic">
    <img class="friend-photo" src="https://i.stack.imgur.com/l60Hf.png">
  </div>

  <div class="friend-pic">
    <img class="friend-photo" src="https://i.stack.imgur.com/l60Hf.png">
  </div>

  <div class="friend-pic">
    <img class="friend-photo" src="https://i.stack.imgur.com/l60Hf.png">
  </div>

</div>

【讨论】:

  • 嗯,这很有帮助。我希望最终让用户上传自己的图像以放置在容器中,因此手动调整图像大小似乎是不可能的。您对使用 CSS 调整图片的尺寸有什么建议吗?或者可能会裁剪掉容器圆形之外的任何内容?
  • @JonathanHinds 我花了很多时间研究这个问题,这一切都归结为网格系统。如果我使用固定高度宽度,我可以让它工作。我建议使用不同的“css 框架”并避免使用 grid-template-columns: calc().
猜你喜欢
  • 2019-11-16
  • 2016-05-22
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 2014-06-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多