CSS Variables

Size
4,759 Kb
Views
8,096

How do I make an css variables?

Playing with CSS Variables for a blog post (http://jamessteinbach.com/css/css-variables/). Firefox only. What is a css variables? How do you make a css variables? This script and codes were developed by James Steinbach on 05 November 2022, Saturday.

CSS Variables Previews

CSS Variables - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>CSS Variables</title> <script src="https://s.codepen.io/assets/libs/modernizr.js" type="text/javascript"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css"> <link rel="stylesheet" href="css/style.css">
</head>
<body> <header> <h1 class="site-title">CSS Variables</h1> <nav class="site-menu"> <ul> <li><a href="#colors">Color</a></li> <li><a href="#grids">Grids</a></li> <li><a href="#columns">Columns</a></li> <li><a href="#font-sizes">Font Sizes</a></li> <li><a href="#js">JS</a></li> </ul> </nav>
</header>
<div class="main"> <div class="content"> <section id="colors"> <h2 class="section-title">CSS Variables for Colors</h2> <p>You can change the color of an element by setting its color property to <code>var(--color-primary)</code>, then scoping that variable inside a parent element. For example, paragraphs are normally <span style="color:#2980b9">#2980b9</span>, but inside of this section, the <code>--color-primary</code> variable is scoped to #27ae60.</p> </section> <section id="grids"> <h2 class="section-title">CSS Variables for Grids</h2> <p>All the grid items in the following sections have the same CSS. Their width is controlled by the <code>--grid-width</code> variable, scoped to their <code>.four-cols</code> and <code>.three-cols</code> classes.</p> <h3>Four Columns</h3> <div class="four-cols"> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> </div> <h3>Three Columns</h3> <div class="three-cols"> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> <div class="grid-item">Grid Item</div> </div> </section> <section id="columns"> <h2 class="section-title">CSS Variables for Column Proportions</h2> <div class="column-block"> <div class="wide-content"> <p> You can change the proportions of columns & sidebars by scoping <code>--content-width</code> and <code>--sidebar-width</code> to their containers. </p> </div> <aside class="narrow-content"> <div class="widget">Widget</div> <div class="widget">Widget</div> </aside> </div> <div class="column-block wide-sidebar"> <div class="wide-content"> <p> <code>--content-width</code> and <code>--sidebar-width</code> have been changed in the <code>.wide-sidebar</code> element to change proportions here. </p> </div> <aside class="narrow-content"> <div class="widget">Widget</div> <div class="widget">Widget</div> </aside> </div> </section> <section id="font-sizes"> <h2 class="section-title">CSS Variables for Font Sizes</h2> <p>The fonts in this section are larger, because the <code>--font-size</code> variable is larger in this section.</p> </section> <section id="js"> <h2 class="section-title">CSS Variables with JS</h2> <p> Change the color variables by clicking the button below: </p> <button onClick="change_vars()" id="change">Color Changer</button> </section> </div>
</div>
<footer><a href="https://jdsteinbach.com/css/use-cases-css-variables/">CSS Variables</a> Blog Post by James Steinbach</footer> <script src="js/index.js"></script>
</body>
</html>

CSS Variables - Script Codes CSS Codes

:root { --grid-width: 20%; --color-primary: #2980b9; --color-secondary: #c0392b; --color-accent: #16a085; --wide: 75%; --narrow: 25%; --font-size: 18px;
}
body { font-family: Gotham, Proxima Nova, Lucida Casual, sans-serif; box-sizing: border-box;
}
*,
*::before,
*::after { box-sizing: inherit; -webkit-transition: color .6s, background .6s; transition: color .6s, background .6s;
}
code { font-family: monospace; background: #eee; padding: .25em;
}
h1, h2, h3 { color: var(--color-secondary);
}
h1 { margin: .5em 0; font-size: 2em;
}
h2 { font-size: 1.6em; font-weight: bold; margin: 1.5em 0 .5em;
}
h3 { font-size: 1.3em; margin: 1em 0 .5em;
}
p { color: var(--color-primary); line-height: 1.5;
}
code { font-size: 100%; border-radius: .25em;
}
header { background: #ddd; padding: 0 1em;
}
header::after { visibility: hidden; display: block; font-size: 0; content: " "; clear: both; height: 0;
}
header .site-title { float: left; width: 33%;
}
header .site-menu { float: right; width: 66%;
}
header .site-menu ul { margin: 0; list-style: none; text-align: right;
}
header .site-menu li { display: inline-block; padding: 1em .25em;
}
header .site-menu li a { line-height: 2; text-decoration: none; background: var(--color-accent); padding: .25em 1em; color: #fff; border-radius: .25em; -webkit-transition: opacity; transition: opacity;
}
header .site-menu li a:hover { opacity: .9;
}
.main { clear: both; padding: 1em;
}
section { border-bottom: 2px solid #ccc; padding-bottom: 2em; font-size: var(--font-size);
}
section:last-child { border-bottom: 0;
}
section::after { visibility: hidden; display: block; font-size: 0; content: " "; clear: both; height: 0;
}
#colors { --color-primary: #27ae60;
}
[class*="-cols"] { font-size: 0;
}
.four-cols { --grid-width: 25%;
}
.three-cols { --grid-width: 33%;
}
.grid-item { font-size: 16px; display: inline-block; vertical-align: middle; border: .5em solid #fff; width: var(--grid-width); background: var(--color-accent); color: #fff; padding: 2em 1em; text-align: center;
}
.column-block { margin-bottom: 1em;
}
.column-block::after { visibility: hidden; display: block; font-size: 0; content: " "; clear: both; height: 0;
}
.wide-content { float: left; width: var(--wide);
}
.narrow-content { float: right; width: var(--narrow);
}
.wide-sidebar { --wide: 62.5%; --narrow: 37.5%;
}
.widget { width: 100%; padding: 1em; text-align: center; color: #fff; background: var(--color-accent); margin-bottom: .25em;
}
#font-sizes { --font-size: 22px;
}
#js button { background: var(--color-accent); border: none; color: #fff; padding: 1em; margin: 1em 0; box-shadow: 2px 3px 0 0 var(--color-secondary); -webkit-transition: opacity; transition: opacity;
}
#js button:hover { opacity: .9;
}
footer { background: #ddd; padding: 1em;
}

CSS Variables - Script Codes JS Codes

function change_vars(){ var body = document.getElementsByTagName("body")[0]; if ( !body.classList.contains('changed') ) { body.style.setProperty('--color-primary', '#8e44ad'); body.style.setProperty('--color-secondary', '#16a085'); body.style.setProperty('--color-accent', '#e67e22'); body.classList.add('changed'); } else { body.style.setProperty('--color-primary', '#2980b9'); body.style.setProperty('--color-secondary', '#c0392b'); body.style.setProperty('--color-accent', '#16a085'); body.classList.remove('changed'); }
}
CSS Variables - Script Codes
CSS Variables - Script Codes
Home Page Home
Developer James Steinbach
Username jdsteinbach
Uploaded November 05, 2022
Rating 3
Size 4,759 Kb
Views 8,096
Do you need developer help for CSS Variables?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

James Steinbach (jdsteinbach) Script Codes
Create amazing love letters with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!