Home Web Front-end CSS Tutorial Detailed explanation of css opacity property

Detailed explanation of css opacity property

Sep 17, 2018 pm 05:24 PM
css

This chapter brings you a detailed explanation of css opacity opacity, so that everyone can understand what the opacity attribute is and some characteristics of the opacity attribute. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Opacity attribute

1. Opacity

It is customary to say "transparency", In fact, it should be called "opacity". Opacity means: opaque, while the default value of the background color: transparent means "transparent". So opacity is used to set the opacity. The value ranges from [0.0~1.0], which means from completely transparent to completely opaque. 0.0 is the same as transparent. It cannot be seen but it exists.

Default value: 1, completely opaque.

Inherited: Inherited by default. So when you set opacity to the parent element, all child elements will also inherit the opacity attribute.

<style>
div{
    background-color: red;
}
.opacity{
    opacity: 0.5;
}
</style>
---------------------------
<div>
文本和背景色都受到不透明度级别的影响。
</div>
<br/>
<div class="opacity">
文本和背景色都受到不透明度级别的影响。
</div>
Copy after login

Rendering:

Detailed explanation of css opacity property

#All browsers support the opacity attribute, and IE8 and earlier versions support the alternative filter attribute.

filter:alpha(opacity=number), number value is [0~100], 0 is completely transparent, 100 is opaque.

So for compatibility, it can be written as:

.opacity{
    opacity: 0.5;
    filter:alpha(opacity=50);
}
Copy after login

2. Set opacity for child elements

The parent element sets opacity, and the child elements also set opacity. The opacity set by the child element does not work under IE. Under ff and Chrome, the final opacity of the child element = parent element opacity * child element opacity.

This can explain why after the parent element sets the opacity, the child element sets the opacity to 1 [that is, completely opaque] but it does not take effect.

Example:

<!DOCTYPE html>
<html>
  <meta charset="utf-8">
<head>
  <title>透明度by starof</title>
  <style>
div{
    background-color: red;
}
.opacity{
    opacity: 0.5;
}
.sonOpacity{
    opacity: 0.3;
}
</style>
<body>
<div>
<p>不设置opacity效果<p>
</div>
<div class="opacity">
  <p>父元素(div)设置opacity:0.5效果</p>
</div>
<div class="opacity">
  <p class="sonOpacity">父元素(div)设置opacity:0.5,同时子元素(p)设置opacity:0.3效果</p>
</div>
</body>
</html>
Copy after login

Rendering:

Detailed explanation of css opacity property

##3. Effect display

1) Semi-transparent background effect

can produce the following effects:

<!DOCTYPE html>
<html>
<head>
    <title>b.html</title>
    <meta charset="UTF-8">
    <style>
.background{
    width: 400px;
    height: 250px;
    margin:15px;
    background:url(img/wl.jpg) no-repeat;
    border: 1px solid red;
}
.opacity{
  width: 350px;
  height: 204px;
  margin:20px;
  padding:0;
  background-color: #ffffff;
  border: 1px solid black;
  /* for IE */
  filter:alpha(opacity=60);
  /* CSS3 standard */
  opacity:0.6;
}
P{
  margin: 30px 40px;
  line-height:1.5;
}
</style>
</head>
<body>
    <div class="background">
        <div class="opacity">
            <p>一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字一段文字</p>
        </div>
    </div>

</body>
</html>
Copy after login

2) The parent element and the child element set opacity at the same time. In ff and chrome, the opacity of the child element is the opacity*child of the parent element. The element opacity

removes the background color and only looks at the text, the effect is more obvious.

<!DOCTYPE html>
<html>
  <meta charset="utf-8">
<head>
  <title>透明度by starof</title>
  <style>
.opacity{
    opacity: 0.5;
}
.sonOpacity{
    opacity: 0.5;
}
.sonOpacity2{
    opacity: 0.25;
}
</style>
<body>
<div>
<p>不设置opacity效果<p>
</div>
<div class="opacity">
  <p>父元素(div)设置opacity:0.5效果</p>
</div>
<div class="opacity">
  <p class="sonOpacity">父元素(div)设置opacity:0.5,同时子元素(p)设置opacity:0.5效果</p>
</div>
<div>
  <p class="sonOpacity2">父元素(div)不设置opacity,子元素(p)设置opacity:0.25效果,和上面的文字透明度一样</p>
</div>
</body>
</html>
Copy after login

Rendering:


Detailed explanation of css opacity property

##2. Comparison of opacity and rgba modes Syntax:

rgba(r,g,b,a);
Copy after login

[R:red,G:green,B:blue,A:alpha]

The first three parameters are rgb values, the value is [0~255] , the last parameter represents transparency, value [0~1].

background-color: rgba(255,0,0,0.5);

Look at an example of comparing opacity and rgba:

<style type="text/css">
    div{
      display: inline-block;
      width: 200px;
      height: 100px;
      text-align: center;
      font-size: 20px;
    }
   .opacity{
    background-color: rgb(255,0,0);
    opacity: 0.5;
    }
    .rgba{
    background-color: rgba(255,0,0,0.5);
    }
</style>
----------------------------------------------
<body >
  <div class="opacity">
    <p>opacity效果</p>
  </div>
  <div class="rgba">
    <p>transparent效果</p>
  </div>
</body>
Copy after login

Rendering:

The above is the detailed content of Detailed explanation of css opacity property. 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

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)

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

See all articles