1. Requirements description For a certain element, you want the background If it is a solid color background or a CSS gradient background, it is easy to handle. Just use However, if it is If it is an inline I guess many people would think of using the .box { position: relative; z-index: 0; } .box::before { content: ''; position: absolute; left: 0; right: 0; top: 0; bottom: 0; background: url(xxx.jpg) no-repeat center / contain; z-index: -1; opacity: .5; } The real-time effect is as follows (if there is no effect, please visit the original author Zhang Xinxu): by-zhangxinxu However, this method is too verbose and costly (it creates a lot of stacking contexts and the size needs to be adjusted), so it cannot be used on a large scale. Is there any good way to implement it? Try using 2. cross-fade() makes the background image semi-transparent For example: <div class="cross-fade-image"></div> .cross-fade-image { width: 300px; height: 300px; background: no-repeat center / contain; background-image: -webkit-cross-fade(url(1.jpg), url(2.jpg), 50%); background-image: cross-fade(url(1.jpg), url(2.jpg), 50%); } The effect will be as shown in the figure below. Image The above example uses the traditional syntax of <dfn id="ltimage-combination"> <image-combination> </dfn> = cross-fade( <image>, <image>, <percentage> ) Here Regarding the percentage value in The screenshots are as follows: Although the original intention of The implementation principle is very simple. Use a transparent image for the first image and a target image for the second image. For example: A background image is too bright in dark mode. I want to adjust the brightness of the background image. .dark { /* Fallback, IE and Firefox browsers*/ background-image: url(2.jpg); --transparent: url(data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==); /* The latest version of Safari no longer requires a private prefix*/ background-image: cross-fade(var(--transparent), url(2.jpg), 40%); /* If you use custom properties, the -webkit- statement needs to be placed below the statement without the private prefix*/ background-image: -webkit-cross-fade(var(--transparent), url(2.jpg), 40%); background-size: cover; } The effect is as follows: Isn’t it simple? Isn’t it much more reliable than pseudo-element implementation? The corresponding demo can be found here. Therefore, using 3. Excellent mobile compatibility Therefore, you can use it with confidence on mobile devices. As for PC, you can use it with confidence if you don’t need to consider the IE browser. Even if you need to consider IE, there is nothing wrong with it. The only difference is that the background image is still completely opaque, and the visual experience is slightly lower. There are no such things as useless CSS properties in this world, it’s just that you haven’t encountered the right scenario; just like those of you who are still single, you just haven’t met the right one yet. In addition, the This concludes this article about using CSS cross-fade() to achieve background image translucent effect with sample code. For more information on CSS cross-fade() background image translucent effect, please search 123WORDPRESS.COM’s previous articles or continue browsing the related articles below. We hope that you will support 123WORDPRESS.COM in the future! |
<<: Disable autocomplete in html so it doesn't show history
>>: Solution to data duplication when using limit+order by in MySql paging
<br />I have summarized the annotation writi...
The so-called favicon, which is the abbreviation o...
What is Let’s first look at the concept of Docker...
The installation process is omitted (I installed ...
Execution problem between mysql max and where Exe...
This article mainly introduces the implementation...
1. Unzip the file to the current directory Comman...
Download tutorial of mysql-connector-java.jar pac...
Table of contents background: Nginx smooth upgrad...
1. Basic steps 1: Install yarn add vue-i18n Creat...
Recently, due to work needs, I need to format num...
illustrate: VMware IOInsight is a tool that helps...
Table of contents Common key aliases Key without ...
Table of contents Vue2 Writing Vue3 plugin versio...