【IE9/8/7/6/5】RGBA(background-color: rgba(R, G, B, A);)背景透明兼容性(含“条件注释判断浏览器版本”介绍与例子)


关于RGBA的兼容,我百度过后,整理如下(真正的兼容方法在最下面)



在不兼容情况下

RGBA的属性,对于IE8以下是无效的:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

<html>
<head>
<meta charset="utf-8" />
<title>IE8/7/6/5不兼容rgba</title>
<style type="text/css">
.box {
	width: 300px;
	height: 300px;
	border: 2px solid #000;
	float: left;
	margin: 50px;
}

.img {
	width: 300px;
	height: 300px;
	font-size: 20px;
	line-height: 300px;
	text-align: center;
	color: #000;
	background-color: rgba(255, 0, 0, 0.5);
}
</style>
</head>

<body>
	<div class="box">
		<div class="img"></div>
	</div>
</body>
</html>

所以,下面开始介绍各种兼容方法

一、添加IE的filter属性:(兼容IE8以及以下版本,却不兼容IE9)

filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7Fff0000, endColorstr=#7Fff0000);
注释: #7Fff0000。7F 对应RGBA不透明度的值 0.5 , ff0000 这个就是十六进制颜色值对应 RGB(255, 0, 0)
RGBA不透明度和IE下filter数值的转换
rgbafilter
0.119
0.233
0.34C
0.466
0.57F
0.699
0.7B2
0.8C8
0.9E5

在这里插入图片描述
在这里插入图片描述

🚫🚫🚫但是!!!你以为可以了?回滚 IE9 版本,发现它却不行了!!!

在这里插入图片描述

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>IE9冲突异常,出错</title>
<style type="text/css">
.box {
	width: 300px;
	height: 300px;
	border: 2px solid #000;
	float: left;
	margin: 50px;
}

.img {
	width: 300px;
	height: 300px;
	font-size: 20px;
	line-height: 300px;
	text-align: center;
	color: #000;
	background-color: rgba(255, 0, 0, 0.5);
	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7Fff0000, endColorstr=#7Fff0000);
}
</style>
</head>

<body>
	<div class="box">
		<div class="img"></div>
	</div>
</body>
</html>

这是因为:IE9同时支持RGBA和filter,会导致两个重叠,透明效果异常。
所以!你得另外再声明,在IE9版本下,不使用filter属性!或者,只在IE8及以下添加filter属性!
那就要找关于IE版本css的hack了。


二、真正的兼容方法:同时设置IE9的hack(兼容所有IE版本)

先介绍“条件注释判断浏览器版本”,但真正简洁且适合局部兼容的方法放最后。
  1. 设置条件注释判断浏览器版本——仅IE9版本才生效
    在这里插入图片描述
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>[if IE 9]</title>
<style type="text/css">
.box {
	width: 300px;
	height: 300px;
	border: 2px solid #000;
	float: left;
	margin: 50px;
}

.img {
	width: 300px;
	height: 300px;
	font-size: 20px;
	line-height: 300px;
	text-align: center;
	color: #000;
	background-color: rgba(255, 0, 0, 0.5);
	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7Fff0000, endColorstr=#7Fff0000);
}
</style>
<!--[if IE 9]>
<style type="text/css">
.img {
	filter: none;
}
</style>
<![endif]-->
</head>

<body>
	<div class="box">
		<div class="img"></div>
	</div>
</body>
</html>
  1. 设置条件注释判断浏览器版本——仅IE9以下版本才生效(不含IE9)在这里插入图片描述
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>[if lt IE 9]</title>
<style type="text/css">
.box {
	width: 300px;
	height: 300px;
	border: 2px solid #000;
	float: left;
	margin: 50px;
}

.img {
	width: 300px;
	height: 300px;
	font-size: 20px;
	line-height: 300px;
	text-align: center;
	color: #000;
	background-color: rgba(255, 0, 0, 0.5);
}
</style>
<!--[if lt IE 9]>
<style type="text/css">
.img {
	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7Fff0000, endColorstr=#7Fff0000);
}
</style>
<![endif]-->
</head>

<body>
	<div class="box">
		<div class="img"></div>
	</div>
</body>
</html>
  1. 设置条件注释判断浏览器版本——仅IE8及以下版本才生效(含IE8)在这里插入图片描述
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>[if lte IE 8]</title>
<style type="text/css">
.box {
	width: 300px;
	height: 300px;
	border: 2px solid #000;
	float: left;
	margin: 50px;
}

.img {
	width: 300px;
	height: 300px;
	font-size: 20px;
	line-height: 300px;
	text-align: center;
	color: #000;
	background-color: rgba(255, 0, 0, 0.5);
}
</style>
<!--[if lte IE 8]>
<style type="text/css">
.img {
	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7Fff0000, endColorstr=#7Fff0000);
}
</style>
<![endif]-->
</head>

<body>
	<div class="box">
		<div class="img"></div>
	</div>
</body>
</html>

但是!又来但是了!如果使用条件注释判断浏览器版本来处理这些小细节的兼容,未免太大材小用了!为何?你就这么一条语句的兼容,你花费这么大功夫?还得套一层结构标签?套多一层<style></style>?不麻烦?到时再维护,也会烦死。
所以,条件注释判断浏览器版本,只适合,整套或者大范围的CSS的兼容维护。

条件注释判断浏览器版本
判断符举例说明
![if !IE]The NOT operator. This is placed immediately in front of the feature, operator, or subexpression to reverse the Boolean meaning of the expression. NOT运算符。这是摆立即在前面的功能,操作员,或子表达式扭转布尔表达式的意义。
lt[if lt IE 5.5]The less-than operator. Returns true if the first argument is less than the second argument. 小于运算符。如果第一个参数小于第二个参数,则返回true。
lte[if lte IE 6]The less-than or equal operator. Returns true if the first argument is less than or equal to the second argument. 小于或等于运算。如果第一个参数是小于或等于第二个参数,则返回true。
gt[if gt IE 5]The greater-than operator. Returns true if the first argument is greater than the second argument. 大于运算符。如果第一个参数大于第二个参数,则返回true。
gte[if gte IE 7]The greater-than or equal operator. Returns true if the first argument is greater than or equal to the second argument. 大于或等于运算。如果第一个参数是大于或等于第二个参数,则返回true。
()[if !(IE 7)]Subexpression operators. Used in conjunction with boolean operators to create more complex expressions. 子表达式运营商。在与布尔运算符用于创建更复杂的表达式。
&[if (gt IE 5)&(lt IE 7)]The AND operator. Returns true if all subexpressions evaluate to true AND运算符。如果所有的子表达式计算结果为true,返回true。
|[if (IE 6)|(IE 7)] The OR operator. Returns true if any of the subexpressions evaluates to true. OR运算符。返回true,如果子表达式计算结果为true。

嗯…太复杂太深奥不看,简单来说就是:

IE外都可识别:
<!--[if !IE]> html代码 <![endif]-->

所有的IE可识别 :
<!--[if IE]> html代码 <![endif]-->IE8可识别:
<!--[if IE 8]> html代码 <![endif]-->

IE8以下版本可识别(不含IE8):
<!--[if lt IE 8]> html代码 <![endif]-->

IE8以及IE8以下版本可识别 (含IE8):
<!--[if lte IE 8]> html代码 <![endif]-->

👉👉👉不推荐向上兼容,貌似IE10以上条件注释就无效了👈👈👈
IE8以上版本可识别(不含IE8):
<!--[if gt IE 8]> html代码 <![endif]-->

IE8以及IE8以上版本可识别(含IE8):
<!--[if gte IE 8]> html代码 <![endif]-->


👇👇👇真正的兼容方法,也是适合局部兼容的方法:

  1. 通过CSS3的@media媒体查询,来设置hack在这里插入图片描述
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>@media媒体查询</title>
<style type="text/css">
.box {
	width: 300px;
	height: 300px;
	border: 2px solid #000;
	float: left;
	margin: 50px;
}

.img {
	width: 300px;
	height: 300px;
	font-size: 20px;
	line-height: 300px;
	text-align: center;
	color: #000;
	background-color: rgba(255, 0, 0, 0.5);
}

@media \0screen\,screen\9 {
	.img {
		filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7Fff0000, endColorstr=#7Fff0000);
	}
}
</style>
</head>

<body>
	<div class="box">
		<div class="img"></div>
	</div>
</body>
</html>
  1. CSS3:root选择器,IE浏览器仅支持IE9在这里插入图片描述
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>:root选择器</title>
<style type="text/css">
.box {
	width: 300px;
	height: 300px;
	border: 2px solid #000;
	float: left;
	margin: 50px;
}

.img {
	width: 300px;
	height: 300px;
	font-size: 20px;
	line-height: 300px;
	text-align: center;
	color: #000;
	background-color: rgba(255, 0, 0, 0.5);
	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7Fff0000, endColorstr=#7Fff0000);
}

:root .img {
	filter: none;
}
</style>
</head>

<body>
	<div class="box">
		<div class="img"></div>
	</div>
</body>
</html>
  1. CSS3:not()选择器
    ps::not(string)仅仅在IE9+下生效。IE9会自动忽略:not以及之后的内容并生效与当前元素,但IE10会产生实际作用。(IE10及以上string一般是元素或者选择符,而IE9不会在乎,也就说只兼容IE9,括号的内容不重要,但不能为空,也不能是符号。英文字母随便写) 在这里插入图片描述
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8" />
<title>:not()选择器</title>
<style type="text/css">
.box {
	width: 300px;
	height: 300px;
	border: 2px solid #000;
	float: left;
	margin: 50px;
}

.img {
	width: 300px;
	height: 300px;
	font-size: 20px;
	line-height: 300px;
	text-align: center;
	color: #000;
	background-color: rgba(255, 0, 0, 0.5);
	filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=#7Fff0000, endColorstr=#7Fff0000);
}

.img:not(string) {
	filter: none;
}
</style>
</head>

<body>
	<div class="box">
		<div class="img"></div>
	</div>
</body>
</html>

👉传送门:
①IE8下实现兼容rgba
②HTML条件注释
③用条件注释判断浏览器版本解决页面兼容问题
⑥IE9透明filter和opacity同时生效的解决办法 IE9 hack only
⑦CSS实现背景透明,文字不透明,兼容所有浏览器
⑤常见浏览器兼容性问题及解决办法总结
⑥史上最全的CSS hack方式一览
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* 全局css变量 */ $--color-primary: #409EFF; .primary-color { color: #409EFF; } .background-opacity { background: rgba(64, 158, 255, 0.6); } .form-widget-list { .ghost{ content: ''; font-size: 0; height: 3px; box-sizing: border-box; background: #409EFF; border: 2px solid #409EFF; outline-width: 0; padding: 0; overflow: hidden; } } .el-form-item--medium { .el-radio { line-height: 36px !important; } .el-rate{ margin-top: 8px; } } .el-form-item--small { .el-radio { line-height: 32px !important; } .el-rate{ margin-top: 6px; } } .el-form-item--mini { .el-radio { line-height: 28px !important; } .el-rate{ margin-top: 4px; } } .el-card { margin-top: 3px; margin-bottom: 3px; } input[type="password"]::-ms-reveal { /* 隐藏IE/Edge原生的密码查看按钮 */ display: none; } /* 滚动条样式 begin */ ::-webkit-scrollbar { width: 8px; height: 8px; } ::-webkit-scrollbar-track { width: 8px; background: rgba(#101F1C, 0.1); -webkit-border-radius: 2em; -moz-border-radius: 2em; border-radius: 2em; } ::-webkit-scrollbar-thumb { background-color: rgba(#101F1C, 0.35); background-clip: padding-box; min-height: 28px; -webkit-border-radius: 2em; -moz-border-radius: 2em; border-radius: 2em; } ::-webkit-scrollbar-thumb:hover { background-color: rgba(#101F1C, 0.85); } * {//Firefox浏览器滚动条样式 scrollbar-color: #e5e5e5 #f7f7f9; //滚动条轨道颜色、滚动条滑块的颜色 scrollbar-width: thin; //thin模式下滚动条两端的三角按钮会消失 } /* body {//IE浏览器滚动条样式 scrollbar-shadow-color: #e5e5e5; scrollbar-face-color: #e5e5e5; scrollbar-base-color: #ffffff; scrollbar-arrow-color: #444040; } */ /* 滚动条样式 end */
最新发布
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值