search
HomeWeb Front-endCSS TutorialThe meaning and function of background:transparent in css

Transparent means transparent. In fact, the default color of the background is the transparent attribute.
General usage scenarios:
If an element covers another element and you want to display the following element, then You need to set the background of the above element to transparent

Sometimes when I look at css, I see some css attributes defined as background:transparent. It means the background is transparent. In fact, the default color of the background is the transparent attribute. So writing or not writing is the same thing. I haven’t written an article for a while. I have been in school. Although I have a computer with me, I can’t access the Internet! I am currently using JavaScript to write a web version of the operating system. I will definitely post it when I finish it. I encountered many problems during the writing process, many of which were detailed and troublesome. However, I have been working hard to solve them, which is also a kind of improvement for myself. Now let me talk about a problem I encountered recently. As you know , you use createelement to create a new div. By default, the backgroungcolor attribute in the style of this div is transparent. Haha, if I make a mask for a div, then please see the following code: Copy the code as follows:

div.style.position="absolute"; 
div.style.width=document.body.clientWidth+"px"; 
div.style.height=document.body.clientHeight+"px"; 
div.style.left="0px"; 
div.style.top="0px"; 
div.style.zIndex="2000"; 
div.style.backgroundColor="black"; 
div.style.filter="alpha(opacity="+0+");";

These are the settings of many masked divs, which means that users are shielded from operating other elements on the web page. We mainly look at the last two lines of code. As we have already mentioned, the default backgroundcolor attribute of div is transparent, that is is transparent, so why do we bother to set a black (it doesn’t matter, just set a color as long as it is not transparent, and it cannot be an empty string, the browser will treat the empty string as transparent by default), and set The filter made it transparent. I did the experiment myself. I was confused by the results at first, but after I calmed down and thought about it, I discovered the difference. If we didn’t have the last two sentences of the above code, then we could Operating on masked elements means that it does not have a shielding effect, but this operation is limited. Specifically, we can only operate on focused elements, such as text, pictures, buttons, etc. I For example: Copy the code as follows:

<div style="border:1px red solid;width:100px;height:100px" onclick="alert(1)"123</div >

Under normal circumstances, as long as you click anywhere on this div, it will alert, but when it is covered by an element with a background color of transparent, we only have The alert will only occur when you click 123, because the text can get focus, and the same goes for other elements. By the way, the event of this element with a background color of transparent will be intercepted by the element covered by it. If the object we trigger If there is no focus, then the event will be exchanged to the transparent element for processing and bubbled according to this element. Otherwise, the event with focus will be processed by the object with focus, without exchange, and bubbled according to the element with focus.( It doesn’t seem to be easy to understand, you can understand it by doing more experiments) When setting the style to a transparent color, sometimes the copy code written above is as follows: background:inherit; Then below, you may need to clear the background, so I wrote the copy code. The code is as follows: background:transparent; Transparent background, that is, removing the background. If one element covers another element, and you want to display the following element, then you need to set the background of the above element to transparent

Use createelement to create a new div. By default, the backgroungcolor attribute in the style of this div is transparent. Haha, if I make a mask for a div, then please see the following code:
This is There are many settings for masked divs, which means that users are shielded from operating other elements on the web page.
We mainly look at the last two lines of code. As mentioned before, the default backgroundcolor attribute of div is transparent, which is Transparent, then why do we need to go to the trouble of setting a black (it doesn’t matter, just set a color as long as it is not transparent, and it cannot be an empty string, the browser will treat the empty string as transparent by default), and set the filter The mirror made it transparent.
I did the experiment myself. I was confused by the results at first, but after I calmed down and thought about it, I discovered the difference. If we don’t have the last two sentences of the above code, then we You can operate on masked elements, which means that it does not have a shielding effect, but this operation is limited. Specifically, we can only operate on focused elements, such as text, pictures, buttons, etc. , Let me give you an example:
Under normal circumstances, as long as you click anywhere on this div, it will alert, but when it is covered by an element with a background color of transparent, we can only alert if we click 123, because Text can get focus, and the same goes for other elements. By the way, the event of this element with a transparent background color will be intercepted by the element covered by it. If the object we trigger does not have focus, the event will be swapped Process the transparent element and bubble according to this element. Otherwise, the event with focus will be processed by the object with focus, without exchange, and bubble according to the element with focus. (It seems not very easy to understand, but it can You will understand after you do more experiments)

The above is the detailed content of The meaning and function of background:transparent in css. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.