直接胆红素偏高是什么原因| 兔子能吃什么| 白头翁是什么动物| 喝酒拉肚子是什么原因| 什么惊什么怪| 洋芋是什么| 透疹是什么意思| 喘息性支气管炎吃什么药| 63年属什么| 40岁男人学什么乐器好| 圆脸适合什么发型| 孕妇手麻是什么原因引起的| 菜花炒什么好吃| 戴玉手镯有什么好处| 单元剧是什么意思| 1963年属什么生肖| 小米是什么米| 女性体毛多是什么原因| 手术后吃什么营养品好| 萎缩是什么意思| 尿潜血是什么原因造成的| 三界牌是什么| 疣长什么样子| 咳嗽干呕是什么原因| 结晶果糖是什么| 疮疡是什么意思| 塔塔粉是什么粉| 八字桃花是什么意思| 管型偏高说明什么问题| 宝宝头发黄是缺什么| 汗斑用什么药| mfr是什么意思| 拘禁是什么意思| 四川代表什么生肖| 是什么部首| 生粉和淀粉有什么区别| 看扁桃体挂什么科| 父亲坐过牢对孩子有什么影响| 簸箕是什么意思| 猫咪来家里是什么寓意| 什么是无期徒刑| 什么防晒霜效果最好| 卵黄囊回声是什么意思| 抹茶色是什么颜色| 新疆有什么民族| 中元节是什么节日| 锦是什么意思| 生肖蛇五行属什么| 泡脚用什么东西泡最好| 女同学过生日送什么礼物比较好| dx是什么意思| 2d是什么意思| 压脚背有什么好处| 各位同仁用在什么场合| 处女座是什么星座| 两肺纹理增多是什么意思| 空孕囊是什么原因造成的| 经期吃凉的东西有什么影响| mrsa是什么细菌| 拉肚子低烧是什么原因| 什么时候种玉米| 万条垂下绿丝绦是什么季节| 胆固醇低吃什么| 喘不上来气是什么原因| 儿时是什么意思| 半夏是什么| 六月二十五号是什么星座| 婆家是什么意思| 海纳百川什么意思| 岔气吃什么药最管用| pr在医学上是什么意思| 苦瓜干泡水喝有什么功效| 小学教师需要什么学历| 孕妇吃鸽子蛋对胎儿有什么好处| 人工荨麻疹是什么原因引起的| lov是什么意思| 南字五行属什么| 蔗糖脂肪酸酯是什么| 中国的八大菜系是什么| 检点是什么意思| 班门弄斧什么意思| 暑假让孩子学点什么好| 鸡的祖先是什么| 骨钙素是什么| 着床出血是什么样子的| 一诺千金什么意思| 臭鳜鱼是什么菜系| 是否是什么意思| 什么叫奢侈| hg是什么意思| 焦虑症吃什么药好得快| b族维生素什么时候吃最好| 恺字五行属什么| 梦见搬家是什么预兆| 鼻塞喉咙痛吃什么药| 胃酸烧心吃什么药可以根治| 喝什么胸会变大| 片仔癀为什么这么贵| 返图是什么意思| kcl是什么药| 脑膜炎有什么症状| 乳房皮肤痒是什么原因| 指尖脱皮是什么原因| 夏天脸上皮肤痒是什么原因| 头晕视力模糊是什么原因| 拉拉是什么意思| 养仓鼠需要注意什么| 吲哚美辛是什么药| 刘胡兰是什么样的人| 不知道饿是什么原因| 六月五日是什么日子| 开脸是什么意思| 12月24号是什么星座| 梦见小孩生病什么预兆| 肿瘤指标偏高什么意思| 上坟可以带什么水果| np是什么意思| 发热吃什么药| 什么什么泪下| 授人以鱼不如授人以渔什么意思| 梦见前男友是什么意思| 章鱼吃什么食物| 实蛋是什么| 葡萄糖高是什么原因| 白化病是什么能活多久| 昆仑雪菊有什么作用| 脚趾甲凹凸不平是什么原因| 胃痞病是什么病| 夏天什么面料的衣服最舒服| 细胞学检查是什么| 独一无二是什么生肖| 韧带拉伤用什么药好| 纵横四海是什么意思| 优越感是什么意思| 6.26什么星座| 什么叫制动| 小孩白细胞高是什么原因| 一般的意思是什么| 有什么小说| 什么是炎症| 左腿发麻是什么原因| 黄风怪是什么动物| 什么的云| 男命食神代表什么| 首鼠两端是什么意思| 早早孕是什么意思| 女性下面长什么样| 中性粒细胞计数偏高是什么意思| 什么成荫| 京酱肉丝是什么菜系| 痱子长什么样子图片| 晟怎么读什么意思| 越什么越什么的词语| 坐月子可以吃什么| 胃酸过多有什么症状| 996什么意思| m倾向是什么意思| 白癜风是什么症状| 今天什么节日| 肚脐下面是什么部位| 九夫痣是什么意思| 毛泽东的女儿为什么姓李| 粉瘤是什么东西| 巴不得是什么意思| 梦见在水里游泳是什么意思| 体寒的人吃什么食物好| 玛瑙对人体有什么好处| 生不如死什么意思| 浙江属于什么方向| 肺结节什么东西不能吃| 97年的属什么生肖| 五六天不拉大便是什么原因| 鸟是什么生肖| 健硕是什么意思| 劫财代表什么| 薄荷叶泡水喝有什么好处| 什么时候放假| 唇腺活检主要是看什么| 三色堇的花语是什么| 存在是什么| 晏字五行属什么| 耳仓为什么是臭的| 万能受血者是什么血型| 天目湖白茶属于什么茶| 堃什么意思| 是什么单位| 风寒感冒吃什么中成药| g点是什么| 红眼病不能吃什么东西| 前列腺增生用什么药好| 脸上长痘痘是什么原因引起的| 母亲o型父亲b型孩子是什么血型| 为什么长白头发| 孔子是什么家| 尿里加什么能冒充怀孕| 鼻子旁边长痘是什么原因| 高潮是什么感觉| 嘴唇干裂是什么原因引起的| 骄阳似火是什么意思| 荨麻疹能吃什么| 什么的母鸡| 憬五行属什么| 小腹胀痛是什么原因| 成吉思汗是什么意思| 明天代表什么生肖| 天性使然什么意思| 高烧吃什么药| 央企董事长什么级别| 强肉弱食是什么意思| 腔隙性脑梗死吃什么药| 榴莲和什么不能一起吃| 半夏生是什么意思| 篓子是什么意思| 为什么女人阴唇会变大| 两个火念什么| 成人男性尿床是什么原因造成的| 风口浪尖是什么意思| 腰酸是什么病的前兆| 反流性咽喉炎吃什么药| 没出息什么意思| 春雨绵绵是什么生肖| af是什么| 长命的动物是什么生肖| 天堂是什么意思| 腺瘤样增生是什么意思| 翡翠是什么意思| 每天都做梦是什么原因| 炖猪蹄放什么调料| 3价铁离子是什么颜色| 博文是什么意思| 屈光参差是什么意思| 天降横财什么意思| 为什么腹部隐隐作痛| 吃什么降血糖最快| 什么叫靶向治疗| 店长的工作职责是什么| 高同型半胱氨酸血症吃什么药| 山楂干泡水喝有什么功效和作用| 量贩什么意思| ca医学代表什么意思| 狗到家里是什么预兆| 鼻炎看什么科| 死去活来是什么生肖| kipper什么意思| 鱼油对眼睛有什么好处| 乳牙是什么| 吃狗肉不能和什么一起吃| 粤语骑马过海什么意思| 奢侈的近义词是什么| 梦见煎鱼是什么预兆| 菜花是什么病| 失败是成功之母是什么意思| 王字旁的字有什么| sansay是什么牌子| dwi是什么检查| 2.10是什么星座| 乳头有点痒是什么原因| 青黛色是什么颜色| 叶黄素是什么| 一直呕吐是什么原因| 猫来家门口有什么预兆| 是什么为什么怎么办| 天地不仁以万物为刍狗是什么意思| 做梦梦到地震预示着什么| 百度

崇德重礼 德法相依--从弘扬中华优秀传统文化说起

CSS variables, properly known as CSS Custom Properties, were added to WebKit in the fall of 2015. WebKit has always been intrigued by the idea of putting variables in CSS, ever since 2008 when Dave Hyatt added our first experimental implementation. Since then there were two more re-writes of CSS Custom Properties, which helped shape the standard that browser vendors are settling on today.

CSS variables allow exactly what developers expect, assigning a value to a custom CSS symbol that can be re-used in the stylesheets of a web page. CSS quickly becomes complicated and often requires a lot of repetitive code, so the benefits of variables are obvious. Variables make it possible to reduce the amount of duplicate code and simplify the maintenance of complex CSS systems.

Developers have enjoyed variable-like features in their CSS for years using pre-processors as part of a web development tool chain. Unlike pre-processors though, native CSS variables bring dynamic variables to the mostly static syntax of CSS. CSS variables are, in fact, custom CSS properties that have DOM context. This is an important characteristic that allows CSS Custom Properties to do things you can’t do with variables in pre-processors. These are live properties that can mutate depending on the cascade of rules. Understanding this nuanced difference unlocks the full potential that CSS Custom Properties provides.

Custom Property Syntax

At first glance the syntax may seem a little strange compared to variable syntax in other languages. It is designed to fit within the existing CSS grammar using a shorthand prefix. To define a custom property, create a CSS element rule and within it add a custom property name with a double-dash prefix:

#foo {
   --default-color: #08c;
}

Generally, you’ll define the default values for custom properties using the :root pseudo-selector so that they are available everywhere:

:root {
   --default-color: #08c;
}

To use the variable property, use the var() function passing the custom property name and optionally a fallback value in cases where the custom property may not be available:

#foo h1 {
    color: var(--default-color, black);
}

Since the var() function produces a value, it can be used in place of any value, such as inside of other CSS functions like calc() or linear-gradient().

.gradient {
    width: 66%;
    height: 240px;
    background: linear-gradient(120deg, rgb(100, 0, var(--less-blue, 255)), rgb(100, 255, var(--more-blue, 0)));
}

.gradient:hover {
    --less-blue: 100;
    --more-blue: 255;
}
Hover over the gradients to apply the custom properties

In the above figure, the custom properties are applied to only one gradient at a time following the normal rules of :hover behavior.

CSS custom properties can also be accessed in JavaScript:

element.style.getPropertyValue('--less-blue');

It’s worth noting that the above JavaScript provides the inherited custom property value for element, not necessarily the value set in a :root pseudo-selector rule. To get a custom property value set on the root element, get the styles applied to document.documentElement:

var rootStyles = getComputedStyle(document.documentElement);
var defaultColor = rootStyles.getPropertyValue('--default-color');

Since these are runtime properties, values can be updated dynamically:

element.style.setProperty('--less-blue', '75');
document.documentElement.style.setProperty('--default-color', '#c80');

Simplifying Web Inspector

For a real world example, look no further than Web Inspector in WebKit where using CSS Custom Properties made it possible to simplify styling, trim some complex CSS rules, and make future maintenance easier. That’s a lot of wins that wouldn’t have been possible to the same degree using CSS pre-processor variables.

Being a web app, the interface of Web Inspector is driven by web technologies, but it’s designed to look and work like an app native to the OS. To maintain the user experience when Web Inspector is in a window, the native window behaviors need to be emulated. For example, when the window changes state from active to inactive the look of the window controls and frame elements need to simulate what a native inactive window would do — diminish the window’s visual impact by reducing contrast.

webkit-windows-active-inactive

To achieve this effect, the CSS of Web Inspector keys off of a window-inactive class with rules that override the normal appearance. The override rules change colors to reduce the contrast. Before CSS Custom Properties, there was no single-rule approach to override the colors on all of the properties for all of the elements that need adjusted. The approach consisted of a rule for an element that sets a normal active-state color along with an override rule when a window-inactive class is set on the document body to subtly change the color.

.sidebar.left {
    border-right: 1px solid hsl(0, 0%, 70%);
}

.sidebar.right {
    border-left: 1px solid hsl(0, 0%, 70%);
}

body.window-inactive .sidebar.left {
    border-right-color: hsl(0, 0%, 85%);
}

body.window-inactive .sidebar.right {
    border-left-color: hsl(0, 0%, 85%);
}

What CSS Custom Properties allows instead is one override to rule them all. The default state has an initial custom property definition, then changes the custom property value for the new window state directly:

:root {
    --border-color: hsl(0, 0%, 70%);
}

body.window-inactive { 
    --border-color: hsl(0, 0%, 85%); 
}

This allows inheritance to do all of the work so that element-specific state overrides can be dropped in favor of simply referencing the variable custom property.

.sidebar.left {
    border-right: 1px solid var(--border-color);
}

.sidebar.right {
    border-left: 1px solid var(--border-color);
}

The 4-line change to declare CSS variables in Web Inspector replaced 32 duplicated color values with var(--border-color) and eliminated 23 rules at the same time!

Takeaways

CSS Custom Properties unlock better techniques for architecting stylesheets that can dramatically reduce CSS code used, and make management of large CSS systems far easier. Controlling thematic color changes is just a starting point. Imagine the possibilities of cascading dynamic variables for managing fonts, spacing, backgrounds, element positions, or naming custom transition timing curves.

Feedback

You can try out CSS Custom Properties in the WebKit Nightly Build or in Safari 9.1 on iOS 9.3, and OS X El Capitan 10.11.4, Yosemite, and Mavericks. Let us know how you’re using CSS Custom Properties and send feedback on Twitter (@webkit, @jonathandavis) or by filing a bug.

五月生日是什么星座 避孕药什么时候吃有效 属龙的本命佛是什么佛 什么的爬 青蛙长什么样
小腿肚酸胀是什么原因 属猪的五行属什么 袋鼠喜欢吃什么食物 雷特综合症是什么症状 就坡下驴什么意思
怀孕了挂什么科 世态炎凉什么意思 耳鸣脑鸣是什么原因引起的 什么叫透析 淑女气质给人什么感觉
掉马是什么意思 大红色配什么颜色好看 什么叫等离子 太妹是什么意思 挺拔的意思是什么
疯狗病症状都有什么hcv9jop2ns7r.cn 汽车抖动是什么原因hcv8jop1ns7r.cn 排便困难拉不出来是什么原因hcv9jop0ns8r.cn 维生素B3叫什么名字hcv7jop9ns9r.cn 光宗耀祖是什么意思hcv9jop5ns2r.cn
结节低回声是什么意思xinmaowt.com 月经要来之前有什么症状hcv9jop6ns3r.cn 鼻子两侧毛孔粗大是什么原因造成的jingluanji.com 姓黑的都是什么族hcv9jop3ns6r.cn 灵敏度是什么意思hcv8jop8ns0r.cn
什么叫静脉曲张yanzhenzixun.com 血清果糖胺测定是什么hcv8jop0ns6r.cn 日晡潮热是什么意思hcv7jop6ns7r.cn 梦见买楼房有什么预兆clwhiglsz.com 眼皮肿什么原因引起的jingluanji.com
谷氨酰基转移酶低是什么原因hcv9jop6ns8r.cn 什么是鸡皮肤图片hcv9jop3ns1r.cn 泡茶用什么杯子最好hcv7jop4ns8r.cn 舌头不舒服是什么原因引起的hcv8jop8ns7r.cn 生理期吃什么水果比较好hcv9jop6ns0r.cn
百度