css variables
play

CSS Variables @guilh https://sass-lang.com/guide - PowerPoint PPT Presentation

The Power and Flexibility of CSS Variables @guilh https://sass-lang.com/guide http://lesscss.org/features/#variables-feature https://caniuse.com/#feat=css-variables CSS variables hold references to values you can reuse --property-name:


  1. The Power and Flexibility of CSS Variables @guilh

  2. https://sass-lang.com/guide

  3. http://lesscss.org/features/#variables-feature

  4. https://caniuse.com/#feat=css-variables

  5. CSS variables hold references to values you can reuse

  6. --property-name: value;

  7. var(--property-name)

  8. :root { --color-primary: #278da4; --color-secondary: #b13c69; }

  9. :root { --color-primary: #278da4; --color-secondary: #b13c69; } .headline { color: var(--color-secondary); } .btn { background-color: var(--color-primary); }

  10. :root { --color-bg: #3acec2; --color-bg-light: #009fe1; } header { background-image: linear-gradient( var(--color-bg-light), var(--color-bg) ); }

  11. :root { --color-bg: #3acec2; --color-bg-light: #009fe1; --gradient: var(--color-bg-light), var(--color-bg); } header { background-image: linear-gradient( var(--gradient) ); }

  12. :root { /* Fonts */ --font-stack-primary: 'Raleway', sans-serif; --font-stack-secondary: 'Bree Serif', serif; /* Layout */ --max-width: 1000px; --margin-size: 10px; ¯\_( ツ )_/¯ }

  13. CSS variables do things preprocessor variables can’t

  14. Preprocessor Variables ● Static ● Do not run in the browser ● Not aware of the DOM structure

  15. CSS Variables ● Aware of the DOM structure ● Dynamic ● Update at computed value time

  16. :root { --margin-size: 0.5em; } .headline { margin-bottom: var(--margin-size); } .col + .col { margin-left: var(--margin-size); }

  17. @media (min-width: 576px) { :root { --margin-size: 2em; } } @media (min-width: 768px) { :root { --margin-size: 3em; } }

  18. :root { /* Colors */ --color-primary: #278da4; --color-secondary: #b13c69; /* Fonts */ --font-stack-primary: 'Raleway', sans-serif; --font-stack-secondary: 'Bree Serif', serif; /* Layout */ --max-width: 1000px; --margin-size: 10px; }

  19. Declare CSS variables in other places besides :root

  20. They inherit, cascade and can be scoped to selectors

  21. .btn { ... background-color: var(--button-bg); } .btn.callout { --button-bg: #1de9b6; } .btn.info { --button-bg: #0097bf; }

  22. <a class="btn callout" href="#">Callout Button</a> <a class="btn info" href="#">Info Button</a>

  23. .btn { ... background-color: var(--button-bg); } .btn.callout { --button-bg: #1de9b6; } .btn.info { --button-bg: #0097bf; }

  24. Style elements based on where they appear in the DOM

  25. .card .btn {...} .modal > .btn {...} .banner .btn {...}

  26. .btn { font-size: var(--btn-font-size); background-color: var(--btn-bg); ... }

  27. /* .card .btn */ .card { <div class="card"> --btn-font-size: 0.85em; <a class="btn" href="#">More</a> --btn-bg: #29abe6; </div> } /* .modal > .btn */ <div class="modal"> .modal { --btn-font-size: 1em; <a class="btn" href="#">Start</a> --btn-bg: #25abaa; </div> }

  28. Modal Scope Card Scope ×

  29. Perform calculations with CSS variables

  30. :root { --margin-size: 2; } .img-featured { margin-bottom: calc(var(--margin-size) * 10px); /* 20px */ } .headline { margin-bottom: calc(var(--margin-size) * 1em); /* 2em */ }

  31. Update CSS variables with JavaScript

  32. getPropertyValue() setProperty()

  33. .ball { background-color: var(--ball-bg); transform: translate( calc( var(--pos-x) * 1px), calc( var(--pos-y) * 1px) ); }

  34. // Select element const ball = document.querySelector('.ball'); // Update CSS custom property values body.addEventListener('click', e => { ball.style.setProperty( '--pos-x', e.clientX ); ball.style.setProperty( '--pos-y', e.clientY ); ball.style.setProperty( '--ball-bg', randomHex() ); });

  35. // Select element const ball = document.querySelector('.ball'); // Update CSS custom property values body.addEventListener('click', e => { ball.style.setProperty( '--pos-x', e.clientX ); ball.style.setProperty( '--pos-y', e.clientY ); ball.style.setProperty( '--ball-bg', randomHex() ); });

  36. Thanks! @guilh

  37. teamtreehouse.com

Recommend


More recommend