第一个CSS变量:currentColor_html/css_WEB-ITnose

WBOY
发布: 2016-06-24 11:53:04
原创
1860 人浏览过

一、基本介绍

CSS变量正慢慢地从最初的草案到浏览器实现。但规范中有个已经存在多年的变量:currentColor。这个CSS特性具有良好的浏览器支持和一些实际的应用,本篇文章,我们来学习和了解它。 引用MDN中的描述:

The currentColor keyword represents the calculated value of the element's color property. It allows to make the color properties inherited by properties or child's element properties that do not inherit it by default. It can also be used on properties that inherit the calculated value of the element's color property and will be equivalent to the inherit keyword on these elements, if any.

我们知道如果你不指定border-color的值,它会默认填充color的值:

	<style type="text/css">	.parent{ 		width: 100px;		height: 100px; 		color: red;		border: 1px solid;		box-shadow: 5px 5px 5px;		text-shadow: 3px 3px 3px;	}	</style>	<div class="parent">		没有设置边框颜色	</div> 
登录后复制

这是一个相当巧妙的技巧:如果你改变了文本的颜色,边框颜色会自动跟着改变。这个技巧同样适用于outline,box-shadow,text-shadow等。

二、部分实例

<style type="text/css">.parent{ 	width: 100px;	height: 100px; 	color: #333;}.son {	border: 1px solid #333;	box-shadow: 2px 2px 2px #333;}</style><div class="parent">	<div class="son">没有设置边框颜色</div></div>
登录后复制

  

下面我们来使用currentColor修改上例:

<style type="text/css">.parent{ 	width: 100px;	height: 100px; 	color: #333;}.son {	border: 1px solid currentColor;	box-shadow: 5px 5px 5px currentColor;	text-shadow: 3px 3px 3px currentColor;}</style><div class="parent">	<div class="son">没有设置边框颜色</div></div>
登录后复制

当然,你也可以适用currentColor在所有你所期望的地方,像gradients、SVG、伪元素,例如:使inline svg sprites 显示像icon fonts,像下面这样:

svg {fill: currentColor;}
登录后复制

此时每个svg元素将被渲染为父元素的文本颜色,请戳view Demo

三、Support

IE9+及现代浏览器皆支持。

 

感谢您的阅读,文中不妥之处还望批评指正。

 

转载声明:

本文标题:第一个CSS变量:currentColor

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!