CSS3中如何解决子元素继承父元素的opacity属性[通俗易懂]

CSS3中如何解决子元素继承父元素的opacity属性[通俗易懂]问题css3中的opacity属性是用来设置div元素的不透明级别的,但是我们往往会遇到因为父级元素设定opacity后,子元素也跟着透明了,但是有时候我们只是想让背景是透明的,这该如何解决呢?错误的示例我们常常想到的方法是直接给子元素的opacity设定为1,如下:<!DOCTYPEhtml><html><head><metacharset=”utf-8″><title>opacity</title&g

大家好,又见面了,我是你们的朋友全栈君。

问题

css3中的opacity属性是用来设置 div 元素的不透明级别的,但是我们往往会遇到因为父级元素设定opacity后,子元素也跟着透明了,但是有时候我们只是想让背景是透明的,这该如何解决呢?

错误的示例

我们常常想到的方法是直接给子元素的opacity设定为1,如下:

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>opacity</title>
</head>
<style type="text/css">
 .opacity{
  position: relative;
  width: 400px;
  height: 300px;
  color: black;
  background: red;
  opacity: 0.5;
  }
 .opacity-child{
  position: relative;
  opacity: 1;
 }
  .normal{
  width: 400px;
  height: 300px;
  background: red;
  color: black;
 }
 </style>
<body>
 <div class="opacity">
  <div class="opacity-child">子元素会继承父级元素的opacity属性</div>
</div>
 <div class="normal">子元素会继承父级元素的opacity属性</div>
</body>
</html>

这样我们得到的是无效的:

在这里插入图片描述

那我们应该如何解决呢?

解决方案

这里有两个方案,使用rgba()间接的设定opacity的值,这个属性不会向下继承,或者既然opacity会被子级元素继承,那就把opacity属性放到同级元素实现,下面通过示例具体说说这两种方式:

使用rgba()间接的设定opacity

rgba()有四个参数,最后一个参数就是opacity的值,和opacity单独设定效果一样,但是这个是有background属性来控制的,background不会向下继承,所以就解决这个问题啦,示例如下:

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <title>opacity</title>
</head>
<style type="text/css">
 .opacity{
  position: relative;
  width: 400px;
  height: 300px;
  color: black;
  background: rgba(255,0,0,0.5);
 }
 .opacity-child{
 }
 .normal{
  width: 400px;
  height: 300px;
  background: red;
  color: black;
 }
 </style>
<body>
 <div class="opacity">
  <div class="opacity-child">子元素会继承父级元素的opacity属性</div>
 </div>
 <div class="normal">子元素会继承父级元素的opacity属性</div>
</body>
</html>

效果如下:

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143768.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月20日 下午10:00
下一篇 2022年5月20日 下午10:20


相关推荐

  • iframe透明问题

    iframe透明问题1 与 iframe 元素一起使用的 allowTranspa 标签属性必须设置为 true 2 在 iframe 内容源文档 background color 或 body 元素的 bgColor 标签属性必须设置为 transparent 具体步骤 1 包含框架页的代码 2 transparent htm 页的代码 特别说明本例主要是 ifra

    2026年3月19日
    2
  • Winform控件:保存文件对话框(SaveFileDialog)[通俗易懂]

    Winform控件:保存文件对话框(SaveFileDialog)[通俗易懂]SaveFileDialog用于保存文件1、新建Winform窗体应用程序,命名为SaveFileDialogDemo。2、在界面上添加一个按钮的控件(用于打开保存文件对话框),添加文本控件,用于

    2022年7月1日
    72
  • mybatis-plus 在线激活码_在线激活

    (mybatis-plus 在线激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月26日
    247
  • OHEM算法

    OHEM算法版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/u014380165/article/details/73148073…

    2022年5月7日
    60
  • BootstrapValidator中文文档手册

    BootstrapValidator中文文档手册BootstrapVal 0 5 3 下载地址 https download csdn net download 目前支持 4 种大的校验方式 分别是 Input 针对 input textarea select 控件 CompareValid AjaxValidato RegexValidat FunctionVali

    2026年3月17日
    2
  • springboot的启动流程及原理_精馏的原理及流程

    springboot的启动流程及原理_精馏的原理及流程1.springboot的启动类入口@SpringBootApplication@ComponentScan(basePackages={“cn”})publicclassSpringBootDemo{publicstaticvoidmain(String[]args){SpringApplication.run(SpringBootDemo.class);}}可以看出,Annotation定义(@SpringBootApplicati

    2022年8月21日
    9

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号