In this lesson we are going to use CSS variables to keep our application's colors consistent. This includes defining the variables inside our the pseudo class :root and using the var function within our classes.

We finish up the lesson using JavaScript to log and modify our defined CSS variables.

Define a css variable:

:root {
--main: red; /*Define a css variable*/
} .title {
color: var(--main, white); /*Use main color as default, if not defined, then fallback to white color*/
}

Access css variable by Javascript:

const title = document.getElementById('.title');

console.log(getComputedStyle(title).getPropertyValue('--mian')) // red

Set dynamicly value of css variable from Javascript:

document.documentElement.style.setProperty('--main', 'green');

[CSS3] Create Dynamic Styles with CSS Variables的更多相关文章

  1. Create Dynamic Modal Dialog Form in AdminLTE Bootstrap template

    原文地址 Create modal dialog form in jquery using bootstrap framework, slightly different from the usual ...

  2. CSS Variables

    CSS原生变量(CSS自定义属性) 示例地址:https://github.com/ccyinghua/Css-Variables 一.css原生变量的基础用法 变量声明使用两根连词线"-- ...

  3. [MST] Create Dynamic Types and use Type Composition to Extract Common Functionality

    Since MST offers a runtime type system, it can create and compose types on the fly, making it possib ...

  4. CSS Variables:css自定义属性的使用

    CSS Variables,一个并不是那么新的东西,但对css来说绝对是一场革命.之前使用变量的时候,需要借助sass.less等预处理工具来实现,现在我们可以直接使用css来声明变量. 一.兼容性 ...

  5. CSS3基础——笔记+实战案例(CSS基本用法、CSS层叠性、CSS继承性)

    CSS3基础——笔记 CSS是Cascading Style Sheet的缩写,翻译为"层叠样式表" 或 "级联样式表".CSS定义如何显示HTML的标签央视, ...

  6. css variables & CSS 变量

    css variables & CSS 变量 https://gist.github.com/xgqfrms-GitHub/5d022a13292c615d2730e84d909e1aba c ...

  7. [CSS3] Create a fixed-fluid-fixed layout using CSS calc()

    CSS calc() allows you to mix and match units to get real-time calculations. It's useful when you nee ...

  8. css3复杂选择器+内容生成+Css Hack

    1.复杂选择器2.内容生成3.多列4.CSS Hack(浏览器兼容性)=======================================1.复杂选择器 1.兄弟选择器 1.特点: 1.通过 ...

  9. CSS3条件判断——@supports/window.CSS.supports()(转)

    CSS3条件判断,听起来"不明觉厉",如果你对CSS稍为熟悉一点的话,你会发现CSS中的"@media"就是条件判断之一.是的,在CSS3的条件判断规范文档中包 ...

随机推荐

  1. Ubuntu14.04.1安装搜狗拼音输入法

    之前在Ubuntu16.04下安装过搜狗,在印象中与这次遇到的问题不一样,因此先说明一下这次Ubuntu的版本号: 参考博客http://blog.csdn.net/tao_627/article/d ...

  2. navicate11不能激活的问题

    navicate11不能激活的问题 学习了:http://blog.csdn.net/sanbingyutuoniao123/article/details/52589678 不要安装在系统盘,如果安 ...

  3. Cocos2d-x 2.2.3 使用NDK配置安卓编译环境问题之 Cannot find module with tag 'CocosDenshion/android' in import path

    1.当做安卓移植的时候遇到例如以下问题: Android NDK: jni/Android.mk: Cannot find module with tag 'CocosDenshion/android ...

  4. [SCOI 2005] 栅栏

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1082 [算法] 首先二分“最多得到的符合条件的木板数”,检验时可以使用DFS,但是, ...

  5. 本地PC安裝Centos 6.5 操作手冊

    http://www.xlgps.com/article/130038.html 一.准备工作 1.下载Centos6.5 ISO文件 我在官网上下的6.5版本CentOS-6.5-x86_64-bi ...

  6. js数组及数组对象的遍历

    一 数组遍历 方法一:for循环 方法二:forEach遍历 forEach遍历数组 性能低于for循环,且不可使用break中断循环,也不能使用return返回外层函数 arr.forEach(fu ...

  7. mac修改管理员权限命令

    我们在进行mac 命令行安装软件的时候,有的时候会遇见这样的问题:Please try running this command again as root/Administrator. 如图: 解决 ...

  8. 新物理AI将可能成为量子计算革命的关键

    新物理AI将可能成为量子计算革命的关键 据外媒报道,量子计算无疑是现在最令人兴奋的技术之一,但它的量子物理基础却让它成为了一个令人讨厌的概念理解甚至很难再展开其他事情.然而,最近物理学研究的一项突破可 ...

  9. Ubuntu14.0 + CUDA9.0 + cudnn7.0 + TensorFlow-gpu1.7.0

    在安装好nvidia驱动的基础上安装 CUDA9.0 + cudnn7.0 + TensorFlow-gpu1.7.0 这三个是匹配的版本 别的匹配(CUDA8.0 + cudnn6.0 + Tens ...

  10. ES8(2017)新特性学习

    字符串填充 (1)从前填充:str.padStart(targetLength [, padString]); 示例: 'es8'.padStart(7, '0'); // '0000es8' (2) ...