首頁  >  文章  >  web前端  >  如何在不進行 Z-Index 操作的情況下有效地重疊多個內聯影像?

如何在不進行 Z-Index 操作的情況下有效地重疊多個內聯影像?

Susan Sarandon
Susan Sarandon原創
2024-10-24 18:45:02200瀏覽

重疊多個內嵌影像

您的目標是呈現一組類似以下內容的重疊影像:

How to Efficiently Overlap Multiple Inline Images Without Z-Index Manipulation?

您目前的程式碼:

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

但是,由於您可能遇到的圖像數量不同,此方法變得不切實際。

解決方案:

利用Flexbox 並反轉排序以消除z-index 操作的需要:

<code class="css">.avatars {
  display: inline-flex;
  flex-direction: row-reverse;
}

.avatar {
  position: relative;
  border: 4px solid #fff;
  border-radius: 50%;
  overflow: hidden;
  width: 100px;
}

.avatar:not(:last-child) {
  margin-left: -60px;
}

.avatar img {
  width: 100%;
  display: block;
}</code>
<code class="html"><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>
</div></code>

此解決方案靈活對齊影像,無論影像數量為何,都能確保正確排列。

以上是如何在不進行 Z-Index 操作的情況下有效地重疊多個內聯影像?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn