重叠/叠加多个内联图像

rat*_*kin 3 html css overlay sass overlap

我有一个要重叠的图片列表,因此它们看起来与此类似:

重叠的人

我的代码:

.avatar img {
    border-radius: 50%;
    position: relative;
    left: -5px;
    z-index: 1;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

但显然,我需要一个递增的left值,以及z-index化身img数量的递减。当然,我可以使用@for指令来执行此操作,但事实是,有大量的头像img。我当时在看length()函数,但是它不能像我打算的那样使用。

另一个想法是设置一个固定的div宽度,并适合其中的图像,但这有其自身的问题(如果有5张图像或20张图像,如何控制宽度)。我还可以在其他地方按需要合并图像,而不使用任何CSS。

Jon*_*n P 9

我更喜欢Temani,但如果你不能使用 flex 因为你必须支持 IE 9 或更早版本,我会把它留在这里。

请注意,文本方向现在是从右到左,因此您需要颠倒头像的顺序。

.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 */
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


Tem*_*fif 6

您可以使用弹性和反向顺序,然后不需要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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

这是一个具有规模的想法:

.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);
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)