Overlapping/overlaying multiple inline images

拥有回忆 提交于 2019-12-18 09:03:33

问题


I have a list of images I'm trying to overlap so that they look similar to this:

My code:

.avatar img {
    border-radius: 50%;
    position: relative;
    left: -5px;
    z-index: 1;
}
<div class="avatars">
    <span class="avatar">
        <img src="https://lorempixel.com/50/50/" width="25" height="25"/>
    </span>
    <span class="avatar">
        <img src="https://lorempixel.com/50/50/" width="25" height="25"/>
    </span>
    <span class="avatar">
        <img src="https://lorempixel.com/50/50/" width="25" height="25"/>
    </span>
    <span class="avatar">
        <img src="https://lorempixel.com/50/50/" width="25" height="25"/>
    </span>
    <!-- Variable amount more avatars -->
</div>
<p>4 People</p>

But obviously, I need an incrementing left value, and a decrementing z-index for the number of avatar imgs. Sure, I could do this with the @for directive, but the thing is, there's a variable amount of avatar imgs. I was looking at the length() function, but it doesn't work the way I was going to use it.

Another idea, is to have a set width div, and fit the images inside that, but that comes with its own issues (what if there are 5 images, or 20, how do control the width). I could also combine the images how I want them, elsewhere and not use any CSS.


回答1:


You can use flex and reverse order then no need z-index:

.avatars {
  display: inline-flex;
  flex-direction: row-reverse;
  padding-left:50px;
}

.avatar {
  margin-left: -25px;
  position: relative;
  border:1px solid #fff;
  border-radius: 50%;
  overflow:hidden;
  width:50px;
  height:50px;
}

.avatar img {
  width:50px;
  height:50px;
}
<div class="avatars">
  <span class="avatar">
        <img   src="https://picsum.photos/70">
    </span>
  <span class="avatar">
        <img  src="https://picsum.photos/80">
    </span>
  <span class="avatar">
        <img   src="https://picsum.photos/90">
    </span>
  <span class="avatar">
       <img  src="https://picsum.photos/100">
    </span>
  <!-- Variable amount more avatars -->
</div>
<p>4 People</p>

Here is another idea with scale:

.avatars {
  display: inline-block;
  transform:scale(-1,1);
  padding-left:50px;
}

.avatar {
  margin-left: -25px;
  position: relative;
  display:inline-block;
  border:1px solid #fff;
  border-radius: 50%;
  overflow:hidden;
  width:50px;
  height:50px;
}

.avatar img {
  width:50px;
  height:50px;
  transform:scale(-1,1);
}
<div class="avatars">
  <span class="avatar">
        <img   src="https://picsum.photos/70">
    </span>
  <span class="avatar">
        <img  src="https://picsum.photos/80">
    </span>
  <span class="avatar">
        <img   src="https://picsum.photos/90">
    </span>
  <span class="avatar">
       <img  src="https://picsum.photos/100">
    </span>
  <!-- Variable amount more avatars -->
</div>
<p>4 People</p>



回答2:


I like Temani's better, but if you can't use flex because you have to support IE 9 or earlier, I'll leave this here.

Note that the text direction is now right to left, so you'll need to reverse the order of your avatars.

.avatar img {
  border-radius: 50%;
  position: relative;
  left: -5px;
  margin-left: -25px;
  z-index: 1;
}

.avatars {
  direction: rtl;  /* This is to get the stack with left on top */
  text-align: left;  /* Now need to explitly align left */
  padding-left: 25px;  /* Same value as the negative margin */
}
<div class="avatars">
  <span class="avatar">
        <img src="https://www.fillmurray.com/50/50" width="50" height="50"/>
    </span>
  <span class="avatar">
        <img src="https://www.fillmurray.com/100/100" width="50" height="50"/>
    </span>
  <span class="avatar">
        <img src="https://www.fillmurray.com/200/200" width="50" height="50"/>
    </span>
  <span class="avatar">
        <img src="https://www.fillmurray.com/150/150" width="50" height="50"/>
    </span>
  <span class="avatar">
        <img src="https://www.fillmurray.com/50/50" width="50" height="50"/>
    </span>
  <!-- Variable amount more avatars -->
</div>


来源:https://stackoverflow.com/questions/48916431/overlapping-overlaying-multiple-inline-images

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