[英]z-index not working for blur effect
假設我有一個結構
<div class=A>
<div class=B>
<dialog/>
</div>
</div>
現在我想在 A 上應用模糊效果,一旦我打開對話框,我就可以使用不透明度來查看模糊背后的所有內容並實現 Mac 不透明窗口效果。 所以,到目前為止我所做的是
.B {
opacity: 0.9;
padding: 20px;
position: relative;
z-index: 5;
}
.A{
-webkit-filter: blur(7px);
-moz-filter: blur(15px);
-o-filter: blur(15px);
-ms-filter: blur(15px);
filter: blur(15px);
z-index: 1;
}
如您所見,我為 B 提供了更多的 z-index 以超過 A,並且我將不透明度設置為看到 A 后面的模糊效果。
但目前還沒有好的結果。 一切都很模糊,所以 z-index 無法正常工作。
知道我做錯了什么嗎?
我想實現將 A 置於模糊狀態並讓我的對話清晰但添加一些不透明度。 這是我正在尋找的效果
問候。
-webkit-filter: blur(7px);
-moz-filter: blur(15px);
-o-filter: blur(15px);
-ms-filter: blur(15px);
filter: blur(15px);
Filter 是一個繼承屬性,也會影響A
的內容。
您需要這樣的結構來避免A
:
<div class=A></div>
<div class=B>
<dialog/>
</div>
使用box-shadow
進行blur
效果可能更好嗎?
使用@Manoz jsfiddle 我實現了你想要的。
但是你需要改變結構。 就像之前的答案告訴您這樣做一樣。 唯一的區別是我在.A
添加了絕對位置,因此它位於.B
下方
.B { opacity: 0.9; padding: 20px; position: relative; z-index: 5; background:white; height:250px; width:250px; } .A { -webkit-filter: blur(1px); -moz-filter: blur(1px); -o-filter: blur(1px); -ms-filter: blur(1px); filter: blur(1px); background:blue; z-index: 1; height:250px; width:250px; position: absolute; }
<div> <div class=A> </div> <div class=B> <h2>SAMPLE</h2> </div> </div>
如果需要,您可以為文本添加不透明度。 相應調整。
所以你想保持相同的結構..那么這里是:
.B { opacity: 0.9; padding: 20px; z-index: 5; background:white; height:250px; width:250px; position: relative; } .A:before { content: ''; display: block; -webkit-filter: blur(1px); -moz-filter: blur(1px); -o-filter: blur(1px); -ms-filter: blur(1px); filter: blur(1px); background:blue; z-index: 1; height:250px; width:250px; position: absolute; top:0 ; }
<div class="A"> <div class="B"> <h2>SAMPLE</h2> </div> </div>
您可以相應地調整:before
的大小..
您需要做的第一件事是將兩個 div 分開
<div class="A">
</div>
<div class="B">
</div>
接下來,您必須為將在模糊部分前面的 div 賦予 position 屬性
.B {
position: absolute;
}
.main-wrapper { position: relative; /* It must be given to parent element */ width: 300px; /* setting width as 300px */ height: 300px; /* setting height as 300px */ } .A { position: absolute; width: 100%; height: 100%; display: flex; /* to center the box */ align-items: center; /* to make vertically center */ justify-content: center; /* to make horizontally center */ -webkit-filter: blur(1px); -moz-filter: blur(1px); -o-filter: blur(1px); -ms-filter: blur(1px); filter: blur(1px); /* blurs at 1px */ background: url('https://via.placeholder.com/300'); /* setting dummy image at background */ } .B { opacity: 0.9; }
<div class="main-wrapper"> <div class="A"> <div class="B"> Dummy text </div> </div> </div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.