element {
--main-color: brown;
}
var( --custom-name, value )
element {
color: var(--main-color, orange); /* orange if --main-color is not defined */
}
element {
background-color: var(--main-color, var(--main-background, blue)); /* blue if --main-color and --main-background are not defined */
}
element {
background-color: var(--main-color, --main-background, gray); /* Invalid*/
}
<!DOCTYPE html>
<html>
<head>
<title>CSS Variables</title>
<style>
:root {
--bg-color: lightgreen;
--text-color: red;
}
body {
background-color: var(--bg-color);
text-align: center;
}
h1 {
color: var(--text-color);
}
div {
color: var(--text-color);
font-size: 30px;
}
</style>
</head>
<body>
<h1>Welcome to the bianchenghao6.com</h1>
<div>
This is an example of CSS variables
<h3>--bg-color: lightgreen;</h3>
<h3>--text-color: red;</h3>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>CSS Variables</title>
<style>
:root {
--bg-color: lightgreen;
}
body {
background-color: var(--bg-color);
text-align: center;
}
h1 {
color: var(--text-color, blue);
}
div {
color: var(--text-color, blue);
font-size: 30px;
}
</style>
</head>
<body>
<h1>Welcome to the bianchenghao6.com</h1>
<div>
This is an example of CSS variables
<h3>--bg-color: lightgreen;</h3>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>CSS Variables</title>
<style>
:root {
--bg-color: lightgreen;
--extra-padding: 1.2em;
--txt-size: 90px;
}
body {
background-color: var(--bg-color);
text-align: center;
}
h1 {
color: var(--text-color, blue);
font-size: calc(var(--txt-size) - 20px);
}
div {
color: var(--text-color, blue);
font-size: 30px;
border: 8px ridge red;
padding: calc(var(--extra-padding) + 20px);
}
</style>
</head>
<body>
<h1>Welcome to the bianchenghao6.com</h1>
<div>
This is an example of using the calc() function with the var() function
</div>
</body>
</html>