前面的話
層疊上下文z-index只是解決兩個元素覆蓋,誰離用戶更近的問題。而CSS混合模式,則是處理兩個元素覆蓋部分如何混合的問題。如果了解photoshop的話,對這種現象應該不陌生。CSS3有兩個與混合模式相關的屬性:mix-blend-mode
和background-blend-mode
,本文將詳細介紹CSS混合模式
元素混合
元素混合mix-blend-mode應用于兩個元素之間的混合
mix-blend-mode
初始值: normal
應用于: 所有元素
繼承性: 無
值: normal(正常) | multiply(正片疊底) | screen(濾色) | overlay(疊加) | darken(變暗) | lighten(變亮) | color-dodge(顏色減淡) | color-burn(顏色加深) | hard-light(強光) | soft-light(柔光) | difference(差值) | exclusion(排除) | hue(色相) | saturation(飽和度) | color(顏色) | luminosity
(亮度) | initial(初始) | inherit(繼承) | unset(復原)
兼容性: IE瀏覽器、android4.4-不支持,safari和IOS需要添加-webkit-前綴
[注意]該元素會創建層疊上下文,z-index屬性有效
背景混合
背景混合background-blend-mode應用于一個元素的多背景圖或背景圖與背景顏色之間的混合
background-blend-mode
初始值: normal
應用于: 所有元素
繼承性: 無
值: normal(正常) | multiply(正片疊底) | screen(濾色) | overlay(疊加) | darken(變暗) | lighten(變亮) | color-dodge(顏色減淡) | color-burn(顏色加深) | hard-light(強光) | soft-light(柔光) | difference(差值) | exclusion(排除) | hue(色相) | saturation(飽和度) | color(顏色) | luminosity
(亮度) | initial(初始) | inherit(繼承) | unset(復原)
兼容性: IE瀏覽器、android4.4-不支持,safari和IOS需要添加-webkit-前綴
隔離
隔離isolation的作用是創建一個堆疊上下文stacking context,主要用于與mix-blend-mode屬性一起使用時,將混合模式只應用于某一個元素或某一組元素
isolation
初始值: auto
應用于: 所有元素
繼承性: 無
值: auto | isolate(創建新的堆疊上下文) | initial | inherit | unset
<style>
body{background-color: gray;}
.test1,.test2{display: inline-block;width: 100px;height: 100px;border:1px solid black;}
.test2{isolation: isolate;}
.in{width: 50px;height: 50px;background-color: red;mix-blend-mode: screen;}
</style>
</head>
<body>
<div class="test1">
<div class="in"></div>
</div>
<div class="test2">
<div class="in"></div>
</div>
如果不使用isolation: isolate
創建堆疊上下文,由于.test1
和.test2
背景顏色透明,則.in
會和<body>
背景顏色混合,成為粉色。使用isolation: isolate
后,.test2
從<body>
中隔離出來,不與<body>
的背景進行混合,從而保留其原先的紅色
[注意]由于isolation: isolate
的作用就是創建堆疊上下文,所以只要能創建堆疊上下文,就可以實現隔離的效果,所以,類似地,relative、filter等樣式也可以實現類似效果
文章列表