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
DNS (Domain Name Server) is a server that convert...
A long time ago, I summarized a blog post titled ...
1. Export the database using the mysqldump comman...
Table of contents Water wave effect Let's see...
Effect display: Environment preparation controlle...
The machines in our LAN can access the external n...
1. Achieve the effect 2 Knowledge Points 2.1 <...
Install the nvidia graphics card driver under Ubu...
Preface The electricity in my residence has been ...
Preface This article mainly summarizes some of th...
As a basic element of a web page, images are one ...
HTML to achieve simple ListViews effect Result: c...
1. Data backup 1. Use mysqldump command to back u...
This article mainly introduces the process of imp...
1. Get the mysql image docker pull mysql:5.6 Note...