如何定义和使用CSS变量
.demo{
background:var(--color);
}
:root{
--color:red;
}
.demo{
background:var(--color);
}
p{
color:var(--color);
}
浏览器支持CSS变量?
CSS变量的实质应用
示例1 - 管理颜色
/*css_vars.css*/
:root {
--primary-color: #ed6564;
--accent-color: #388287;
}
html {
background-color: var(--primary-color);
}
h3 {
border-bottom: 2px solid var(--primary-color);
}
button {
color: var(--accent-color);
border: 1px solid var(--accent-color);
}
p {
color: var(--accent-color);
}
/*base.css*/
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
padding: 30px;
font: normal 13px/1.5 sans-serif;
color: #546567;
background-color: var(--primary-color);
}
.container {
background: #fff;
padding: 20px;
}
h3 {
padding-bottom: 10px;
margin-bottom: 15px;
}
p {
background-color: #fff;
margin: 15px 0;
}
button {
margin:0 5px;
font-size: 13px;
padding: 8px 12px;
background-color: #fff;
border-radius: 3px;
box-shadow: none;
text-transform: uppercase;
font-weight: bold;
cursor: pointer;
opacity: 0.8;
outline: 0;
}
button:hover {
opacity: 1;
}
.center {
text-align: center;
}
<div class="container">
<h3>就业协议书</h3>
<p>就业协议书,全称是《全国普通高等学校毕业生就业协议书》,是由教育部高校学生司统一制订的。根据国家规定,在达成就业意向后,毕业生、用人单位、学校三方必须签订《全国普通高等学校毕业生就业协议书》。就业协议书是具有一定的广泛性和权威性,是学校制订就业方案派遣毕业生、用人单位申请用人指标的主要依据,对签约的三方都有约束力。</p>
<div class="center">
<button>查看详情</button><button>取消</button>
</div>
</div>
示例2 - 删除重复的代码
.btn {
border: 2px solid black;}
.btn:hover {
background: black;}
.btn.red {
border-color: red
}
.btn.red:hover {
background: red
}
<button class="btn">Hello</button>
<button class="btn red">Hello</button>
.btn{
border-radius:4px;
text-align:center;
padding:.5em;
margin-bottom:0.5em;
background:#fff;
border:1px solid var(--color, black);
}
.btn:hover{
color:#fff;
cursor:pointer;
background:var(--color, black);
}
.btn.red{
--color:red;
}
.btn.green{
--color:green;
}
.btn.blue{
--color:blue;
}
<div class="btn">HMOE</div>
<div class="btn red">HMOE</div>
<div class="btn green">HMOE</div>
<div class="btn blue">HMOE</div>
示例3 - 使一些属性可读
:root {
--tiny-shadow: 4px 4px 2px 0 rgba(0, 0, 0, 0.8);
--animate-right: translateX(20px);
}
li {
box-shadow: var(--tiny-shadow);
}
li:hover {
transform: var(--animate-right);
}
例4 - 级联变量
.orange-container {
--main-text: 18px;
}
.orange-container:hover {
--main-text: 22px;
}
.red-container:hover {
--main-text: 26px;
}
.title {
font-size: var(--title-text);
}
.content {
font-size: var(--main-text);
}
.container:hover {
--main-text: 18px;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
background: #eee;
padding: 30px;
font: 500 14px sans-serif;
color: #333;
line-height: 1.5;
}
.orange-container {
background: orange;
}
.red-container {
background: red;
}
.red-container,
.orange-container {
padding-top: 10px;
padding-left: 50px;
}
.container {
background: blue;
padding: 20px;
color: white;
}
p {
transition: 0.4s;
}
.title {
font-weight: bold;
}
<html>
<head>
<link rel="stylesheet" type="text/css" href="base.css">
<link rel="stylesheet" type="text/css" href="css_vars.css">
</head>
<body>
<div class="orange-container">
Hover orange to make blue bigger.
<div class="red-container">
Hover red to make blue even bigger.
<div class="container">
<p class="content">Hover on the different color areas to change the size of this text and the title.</p>
</div>
</div>
</div>
</body>
</html>
示例5 - 具有CSS变量的主题切换器
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>具有CSS变量的主题切换器</title>
<style>
body {
background-color: var(--bg, #b3e5fc);
color: var(--bg-text, #37474f);
font-family: sans-serif;
line-height: 1.3;
}
.toolbar {
text-align: center;
}
</style>
</head>
<body>
<div class="toolbar">
<button value="dark">dark</button>
<button value="calm">calm</button>
<button value="light">light</button>
</div>
<h2>Stackoverflow Question</h2>
<p>I would like to use an external javascript file in another javascript file. For example, I could store all my global variables
in a globals.js file and then call then from the website logic logic.js. Then in the index.html, i would insert the tag.
How do I use the globals.js inside the logic.js?
</p>
<script>
var root = document.documentElement;
var themeBtns = document.querySelectorAll(".toolbar > button");
themeBtns.forEach(function (btn){
btn.addEventListener("click", handleThemeUpdate);
});
function handleThemeUpdate(e) {
switch (e.target.value) {
case "dark":
root.style.setProperty("--bg", "black");
root.style.setProperty("--bg-text", "white");
break;
case "calm":
root.style.setProperty("--bg", "#B3E5FC");
root.style.setProperty("--bg-text", "#37474F");
break;
case "light":
root.style.setProperty("--bg", "white");
root.style.setProperty("--bg-text", "black");
break;
}
}
</script>
</body>
</html>
CSS变量的使用提示
:root {
--color: blue;
--COLOR: red;
}
width: var(--custom-width, 50%);
<!--HTML-->
<html style="--size: 600px">
<!--CSS-->
body {
max-width: var(--size)
}
--base-red-color: #f00;
--background-gradient: linear-gradient(to top, var(--base-red-color), #222);
:root {
--padding: 15px
}
@media screen and (min-width: 750px) {
--padding: 30px
}
--text-input-width: 5000px;
max-width: calc(var(--text-input-width) / 2);