css背景色半透明的最佳实践
之前项目中遇到纯色的半透明背景,都是这么干:
<style>
.box {width:300px;height:300px;position:relative;}
.mask {width:300px;height:300px;position:absolute;z-index:;background-color:#;opacity:0.4;filter:alpha(opacity:);}
.content {width:300px;height:300px;position:absolute;z-index:;}
</style> <div class="box">
<div class="content">这里是内容</div>
<div class="mask"></div>
</div>
但是,最近项目中,遇到高度不是固定的,这种方法就不行了。我也想过用Png24图片实现半透明效果,但是要用到图片,而且还有狗日的ie6兼容问题,虽然也可以解决,但是麻烦。
那有木有更好的方法呢??答案是肯定的,不然我写这篇文章干嘛。
<style>
.box {width:300px;height:300px;background:rgba(,,,0.4);filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=,StartColorStr='#66000000',EndColorStr='#66000000');}
</style> <div class="box">
就不告诉你这里是内容。
<div>
我们得到的效果是这样的,IE9 的透明度竟然有 60%!! 这显然不是我们想法的。原因是:IE9 也支持 filter,使得 filter 的结果和 background-color 叠加,所以是 60%。
解决方法必须有的:
.box{filter:progid:DXImageTransform.Microsoft.gradient(enabled='true',startColorstr='#66000000', endColorstr='#66000000');background:rgba(,,,0.4);}
:root .box{filter:progid:DXImageTransform.Microsoft.gradient(enabled='true',startColorstr='#00000000', endColorstr='#00000000');}/*for IE9*/
或者ie9的hack使用:
:root .box{filter:none;}/*for IE9*/
简单解释一下:
background:rgba(0,0,0,0.4);
css3的RGBA,依次是red,green,blue,alpha,最后一个alpha透明度。
filter 渐变滤镜详细用法,[参见这里]
StartColorStr 和 EndColorStr:
#4c000000 是 30% 透明度的 #000000 的意思
组成: # + 透明度 + 颜色
算法: Math.floor(0.6 * 255).toString(16);
filter 渐变滤镜的用法看不懂???没关系,我也不太懂,网上有工具,转换一下就可以了:
背景透明,内容不透明CSS代码生成器
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>背景透明,内容不透明CSS代码生成器</title>
<script>
if(window.top!=window){
window.top.location.href = window.location.href;
}
</script>
</head>
<body>
<style type="text/css">
#hananBackgroundColorOpacity{margin:10px;}
#hananBackgroundColorOpacity span{margin-left:10px; color:#8a8c8e;}
#hananBackgroundColorOpacity textarea{width:650px; height:120px; padding:5px; color:#fff; background:#;}
#hananBackgroundColorOpacity strong{color:#;}
#hananBackgroundColorOpacity h3{color:#; border-bottom:1px solid #ccc; line-height:57px;}
#hananBackgroundColorOpacity h3 img{ float:right;}
#hananBackgroundColorOpacity h3 .c{ clear:both; height:0px;margin:;padding:;}
#hananBackgroundColorOpacity .hanan_introduction,#hananBackgroundColorOpacity .hanan_introduction a{ font-size:12px; color:#6E6E6E; }
#hananBackgroundColorOpacity img{border:none;}
</style>
<div id="hananBackgroundColorOpacity">
<p class="hanan_introduction ">
说明:为页面元素添加背景透明,内容不透明的效果,兼容IE6////、Chrome、Firefox、Safari、Opera。
</p>
<p>选择器:<input id="hananDomChooser" type="text" value="" /><span>id或者class等CSS选择器,比如:<strong>#abc</strong></span></p>
<p>颜色值:<input id="hananColorValue" type="text" value="" /><span>16进制颜色值,不带#号,比如:<strong>#c77eb5</strong>,请填:<strong>c77eb5</strong></span></p>
<p>透明度:<input id="hananOpacity" type="text" value="" /><span><strong></strong>到<strong></strong>之间,比如:<strong>0.5</strong></span></p>
<p><input id="hananGetCssCodeButon" type="button" value="生成CSS代码" /></p>
<p>
CSS代码:<br/>
<textarea id="hananCssCode" type="text"/></textarea>
</p>
<p class="hanan_introduction ">
实现原理参考:<a href="http://www.cssha.com/through-the-ie-private-filter-let-ie6-ie7-ie8-support-transparent-background" target="_blank">http://www.cssha.com/through-the-ie-private-filter-let-ie6-ie7-ie8-support-transparent-background</a>
</p>
</div>
<script type="text/javascript">
// <![CDATA[
function hananColorToRGB(col,opa){
var c = col
var R = parseInt(c.substring(,),), G = parseInt(c.substring(,),), B = parseInt(c.substring(),);
return 'rgba(' + R + ',' + G + ',' + B + ','+ opa +')' ;
}
function smallToBig(col){
var str = '';
if(col.length==){
str = col;
}else{
var _r = col.substring(,), _g = col.substring(,), _b = col.substring();
_r += _r;
_g += _g;
_b += _b;
str = _r + _g + _b;
}
return str;
}
function hananGetCssCode(){
var chooser = document.getElementById('hananDomChooser').value;
var col = smallToBig(document.getElementById('hananColorValue').value);
var opa = document.getElementById('hananOpacity').value;
var rgb = hananColorToRGB(col,opa);
var iecol = parseInt((opa*)).toString() + col;
if(chooser=='' || col=='' || opa==''){
alert('选择器、颜色值、透明度都要填完哦亲!');
}else{
document.getElementById('hananCssCode').value = chooser +'{background:' + rgb + ';filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#' + iecol + ',endColorstr=#' + iecol + ');zoom:1;}'+
'\r\n:root ' + chooser +'{filter:none\\9;}/*for IE9*/';
}
}
window.onload = function (){
document.getElementById('hananGetCssCodeButon').onclick = function(){
hananGetCssCode();
}
}
// ]]>
</script>
</body>
</html>
css背景色半透明的最佳实践的更多相关文章
- 背景半透明rgba最佳实践
by 一丝 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- CSS非ASCII字符最佳实践
问题场景 在写样式时经常需要用到非ASCII字符的属性值,如下: ? 1 2 3 4 5 6 7 8 9 10 11 .hot_list .sign_discount:before { cont ...
- 完美CSS文档的8个最佳实践
在css的世界,文档没有被得到充分的利用.由于文档对终端用户不可见,因此它的价值常常被忽视.另外,如果你第一次为css编写文档,可能很难确定哪些内容值得记录,以及如何能够高效完成编写. 然而,为C ...
- 【27前端】背景半透明rgba LESS实践
今天有看到司徒正美<背景半透明rgba最佳实践>的文章和里面推荐的一个在线工具CSS背景颜色属性值转换 . 于是联系到自己的less库,新技能Get. 内容如下: /*在你的less库中 ...
- <读书笔记>《Web前端开发最佳实践》
P77 P89 CSS Reset P94 给CSS样式定义排序 排序工具:CSScomb P97 什么是CSS的权重?权重是指选择符的优先级 P100 工具:Sass Less P101 框架 ...
- 前端代码标准最佳实践:CSS
前端工程师对写标准的前端代码的重视程度很高.这些最佳标准实践并不是那个权威组织发布的,而是由大量的前端工程师们在实践过程中的经验总结,目的在于提高代码的可读性,可维护性和性能.那么接着上一篇,我们再来 ...
- SMACSS:一个关于CSS的最佳实践-3.Layout Rules
本篇笔者要介绍的是Layout Rules.看完本篇,大家将会知道Layout Rules的作用,以及哪些CSS应该归类为Layout Rules. 什么是Layout Rules? Layout R ...
- SMACSS:一个关于CSS的最佳实践-2.Base Rules
回顾 在上一篇SMACSS:一个关于CSS的最佳实践-Overview中,讲到SMACSS将CSS Rules分为5个Categories: Base Layout Module State Them ...
- SMACSS:一个关于CSS的最佳实践-1.Overview
什么是SMACSS? SMACSS(发音"smacks"),全称Scalable and Modular Architecture for CSS.顾名思义,SMACSS是一个可扩 ...
随机推荐
- (华中科大)江南雨烟 C++ STL 专栏
本文转载来自,华中科技大学江南雨烟的C/C++专栏部分STL剖析文章,以作学习之用. [1] [C++ STL学习之一]容器的共通能力和共通操作总结 [2] [C++ STL学习之二]容器vect ...
- 自己对war包解压的误区
ss.war解压后不会出现ss而是startup.sh运行后会出现
- 用vue的自定义组件写了一个拖拽 组件,局部的 只能在自定义元素内的
简单实现 没有做兼容<!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- Knockout.Js官网学习(创建自定义绑定)
前言 你可以创建自己的自定义绑定 – 没有必要非要使用内嵌的绑定(像click,value等).你可以你封装复杂的逻辑或行为,自定义很容易使用和重用的绑定.例如,你可以在form表单里自定义像grid ...
- Tomcat服务器配置https双向认证(使用keytool生成证书)
一,HTTPS原理 1,HTTP.HTTPS.SSL.TLS介绍与相互关系 (1)HTTP:平时浏览网页时候使用的一种协议.HTTP协议传输的数据都是未加密的(明文),因此使用HTTP协议传输隐私 ...
- Windows XP Ghost系统安装
一.双击Ghost系统安装工具,进入Ghost界面 二.依次单击[Local]-[Partition]-[From Image],可以简单记作1-2-3. 弹出对话框,选择.GHO文件,比如XP.GH ...
- AB Test 是什么
关于AB Test是什么 一种灰度发布方式. ps:什么是灰度发布 每个灰度对象都是0%(白色)到100%(黑色)的中间值,灰度发布是指在黑白之间,能够平滑过度的一种发布方式. 实现方式 让一部分用户 ...
- .NET 11 个 Visual Studio 代码性能分析工具
原文地址 软件开发中的性能优化对程序员来说是一个非常重要的问题.一个小问题可能成为一个大的系统的瓶颈.但是对于程序员来说,通过自身去优化代码是十分困难的.幸运的是,有一些非常棒的工具可以帮助程序员进行 ...
- 微软BI 之SSIS 系列 - 导出数据到 Excel 2013 的实现
开篇介绍 碰到有几个朋友问到这个问题,比较共性,就特意写了这篇小文章说明一下如何实现在 SSIS 中导出数据到 Office Excel 2013 中.通常情况下 2013 以前的版本大多没有问题,但 ...
- 【Windows】Windows中解析DOS的for命令使用
目录结构: contents structure [+] 简介 for /d ... in ... 案例 案例:打印C://根目录下所有的文件夹名称 案例:打印当前路径下,只有1-3个字母的文件夹名 ...