繁体   English   中英

CSS:如何让div适应图片大小?

[英]CSS: How to make div adapt to image size?

我已经实现了 CSS 状态,允许查看用户是否在线:
在此处输入图像描述
这是 CSS 代码:

.status-circle-online {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: rgb(255, 60, 0);
  border: 2px solid white;
  bottom: 10px;
  right: 10px;
  position: absolute;
}

这是 html 代码:

  <div>
    <img
      src={profileImageSrc}
      className="rounded-circle ml-3"
    />
    <div
      className={`status-circle-${online_or_offline_signal} cursor-pointer`}
    />
  </div>

问题是当我减小 window 大小时,状态信号不再是它应该在的位置:
在此处输入图像描述

在此处输入图像描述

知道如何让它“粘”在图像上吗?

如果没有看到更多的代码,很难准确地说出来。 但是您可以尝试添加position: relative; 到包含的 div,然后对于红色圆圈,使用百分比而不是rightbottom的像素。

Position 父 div relative ,然后状态指示器的绝对值将相对于该容器:

 .status-photo { position: relative; width: 100px; height: 100px; }.status-photo img { width: 100%; height: 100%; border-radius: 100%; object-fit: cover; }.status-photo.status { width: 20px; height: 20px; border-radius: 50%; background-color: rgb(255, 60, 0); border: 2px solid white; bottom: 0; right: 10px; position: absolute; }
 <div class="status-photo"> <img src="https://images.unsplash.com/photo-1438761681033-6461ffad8d80?ixid=MnwxMjA3fDB8MHxzZWFyY2h8MXx8cGVyc29ufGVufDB8fDB8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=800&q=60" /> <div class="status" /> </div>

上面的答案很有帮助。 但是,一种更具响应性的方法是对用户百分比:

.status-photo {
  display: block;
  position: relative;
  width: auto;
  height: auto;
}

.status-photo img {
  width: 100%;
  height: 100%;
  border-radius: 100%;
  object-fit: cover;
}

.status-photo .status-online {
  width: 20%;
  height: 20%;
  border-radius: 50%;
  background-color: rgb(255, 60, 0);
  border: 1px solid white;
  bottom: 0%;
  right: 0%;
  position: absolute;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM