Home Web Front-end CSS Tutorial How to set css transparency? Summary of setting methods for various transparency in css

How to set css transparency? Summary of setting methods for various transparency in css

Nov 01, 2018 pm 03:50 PM

This article will introduce to you how to set transparency in CSS. Let’s take a look at the specific content.

Opacity and Transparency

By definition, opacity and transparency in CSS define the visibility of an element, whether it is an image, table, or RGBA (Red Green Blue alpha) color value. According to their meaning, opacity is a measure of how opaque or solid an element is, while transparency measures how easily you can see what's present in the layers beneath it. Regardless, they work the same way - 100% opaque means the element is completely visible, while 100% transparent means it is completely invisible.

Instead of using expensive software, you can create these effects using CSS! With a few simple keystrokes, you can instantly change the appearance of an element on your page, or in some cases even how it reacts when the mouse pointer hovers over it.

Opaque and transparent images

When modifying the image, the opacity property accepts values ​​between 0.0 and 1.0, with the latter as the default value. Therefore, the lower the value, the more transparent the image becomes.

In the example below, we use 0.2, 0.5 and 1.0 for a side-by-side comparison:

How to set css transparency? Summary of setting methods for various transparency in css

img {
opacity: 0.5;
filter: alpha(opacity=50); /* For IE8 and earlier */
}
Copy after login

Note: We used the filter attribute because Internet Explorer Versions 8 and below do not yet recognize the opacity attribute.

Transparent Boxes and Tables

You can use the opacity property to add transparency to an element, including the background and all its child elements. For example, in the box below (using

instead of and its children), all text also becomes transparent.

How to set css transparency? Summary of setting methods for various transparency in css

The text will also push the background color to be transparent, so if that's what you want to achieve, there's no need to modify anything else. You can directly use the following code to achieve this effect:

div {
opacity: 0.3;
filter: alpha(opacity=30); /* For IE8 and earlier */
}
Copy after login

Use RGBA for transparency

However, if you only want to change the background, and the text or other child elements will Keep it opaque and you can use RGBA to bypass it. If you're used to working with hex codes, you can learn about other ways to define colors in CSS, namely, RGB/RGBA and HSL/HSLA colors.

RGBA represents red, green and blue alpha, and the alpha parameter specifies the opacity of the RGB color. So, using RGBA color values, we can set the opacity of the background while the text remains black:

How to set css transparency? Summary of setting methods for various transparency in css

In the above example we used the RGB values ​​171,205,239 and then alpha Parameters define its transparency or transparency. For example:

div { background:rgba(171,205,239,0.3)/ *蓝色背景,不透明度为30%* / }
Copy after login

css Opaque text in transparent background image

Another really cool thing you can do with opacity and transparency is to add text in a transparent box , probably to offset a really inappropriate or dark background image, as shown in the example below.

How to set css transparency? Summary of setting methods for various transparency in css

To create this simple yet effective style, use a
element and name its classes "background" and "transbox". The first category is a background image and border, while the second category is a separate background color and border. Finally, add text using paragraphs.

First, we create a
element (class="background") with a background image and border. Then we create another
(class="transbox") inside the first
. The
element has a background color and a border - the div is transparent. In the transparent
, we added some text inside the

element.

Here is the code we used, you can modify and test it with your own images and text:

<html>
<head>
<style>
div.background {
background: url(green-tile.jpg) repeat;
border: 2px solid black;
}
div.transbox {
margin: 30px;
background-color: #ffffff;
border: 1px solid black;
opacity: 0.6;
filter: alpha(opacity=60); /* For IE8 and earlier */
}
div.transbox p {
margin: 5%;
font-weight: bold;
font-family: Verdana;
font-size: 12px;
color: #000000;
}
</style>
</head>
<body>
<div>
<div>
<p>This is sample text placed in a transparent box.</p>
</div>
</div>
</body>
</html>
Copy after login
Finally

There are many possible uses for opacity and transparency, Whether it's purely aesthetic or to emphasize elements on the web page. It's certainly the cheapest alternative to relying on Photoshop or other photo editing software to do all of this for you!

The above is the detailed content of How to set css transparency? Summary of setting methods for various transparency in css. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1677
14
PHP Tutorial
1280
29
C# Tutorial
1257
24
A Comparison of Static Form Providers A Comparison of Static Form Providers Apr 16, 2025 am 11:20 AM

Let’s attempt to coin a term here: "Static Form Provider." You bring your HTML

Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Weekly Platform News: HTML Loading Attribute, the Main ARIA Specifications, and Moving from iFrame to Shadow DOM Apr 17, 2025 am 10:55 AM

In this week&#039;s roundup of platform news, Chrome introduces a new attribute for loading, accessibility specifications for web developers, and the BBC moves

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

Some Hands-On with the HTML Dialog Element Some Hands-On with the HTML Dialog Element Apr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I&#039;ve been aware of it for a while, but haven&#039;t taken it for a spin yet. It has some pretty cool and

Paperform Paperform Apr 16, 2025 am 11:24 AM

Buy or build is a classic debate in technology. Building things yourself might feel less expensive because there is no line item on your credit card bill, but

Where should 'Subscribe to Podcast' link to? Where should 'Subscribe to Podcast' link to? Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

Quick Gulp Cache Busting Quick Gulp Cache Busting Apr 18, 2025 am 11:23 AM

You should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser

Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading Indicator Weekly Platform News: Text Spacing Bookmarklet, Top-Level Await, New AMP Loading Indicator Apr 17, 2025 am 11:26 AM

In this week&#039;s roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook&#039;s

See all articles