4 回答

TA貢獻1818條經驗 獲得超7個贊
您不能為元素的某些屬性賦予不同的z-index
值。但是對于像 a 這樣的某些元素,div
您可以使用::before
偽::after
元素。你可以z-index
在上面設置一個,有效地創建三個層。更多信息在這里。
在這種情況下,您可以創建一個div
中間img
在里面的。然后添加一個::before
and::after
到那個div
。給一個背景顏色和z-index
-1。另一個是背景圖像和z-index
1。
在下面的示例中,我還在首字母周圍添加了一些邊距和邊框div
,以便您可以更好地了解發生了什么。
.image {
margin: 20px 0 0 20px;
position: relative;
border: 3px solid coral;
width: 200px;
height: 300px;
}
.image::before,
.image::after {
content: '';
display: block;
width: 200px;
height: 300px;
position: absolute;
}
.image::before {
z-index: -1;
background: cornflowerblue;
top: 20px;
left: 20px;
}
.image::after {
z-index: 1;
background: url("https://www.fillmurray.com/200/300");
top: -20px;
left: -20px;
}
<div class="image"><img src="https://www.fillmurray.com/200/300" /></div>

TA貢獻1878條經驗 獲得超4個贊
最后解決方案很簡單。在樣式中是第二個圖像的雙重定義。其中第一個只是部分評論。所以我的第一篇文章是這樣工作的:
.secondimage img{
max-width: 100%;
height: auto;
position: relative;
top: -75px;
margin: 5px;
margin-bottom: 10px;
}
現在只需要找出如何關閉這個問題...謝謝 :)

TA貢獻1783條經驗 獲得超4個贊
如果我理解你想要實現的目標,你可能應該將圖像放在背景 div 中并將第二個圖像放置在position: absolute:
<style>
.mainRunner {
position: relative;
}
.firstimage {
position: relative;
z-index: 2;
}
.secondimage {
position: absolute;
z-index: 3;
top: 20px; /* use top and left values to place the image exactly where you want it over the first image */
left: 20px
}
.background {
position: relative;
z-index: 1;
background-color: #f2e5df;
}
</style>
<div class="mainRunner">
<div class="background">
<img src="image1.png" class="firstimage" />
<img src="image2.png" class="secondimage " />
</div>
</div>
它將背景顏色設置為最后面的元素,然后在其頂部設置secondimage和firstimage。

TA貢獻1880條經驗 獲得超4個贊
答案是否定的……沒有辦法將 z-index 專門定位到元素的背景,z-index 和所有其他 CSS 屬性都作用于整個元素,而不僅僅是作用于它的背景。
你將不得不找到另一種方法來做到這一點,你有沒有想過使用一個沒有內容但圖像大小相同的 div,然后只為那個特定的 div 設置背景顏色?
添加回答
舉報