Just some practice and examples with CSS variables.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.3 KiB

3 years ago
3 years ago
3 years ago
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CSS Variables</title>
  6. <meta name="author" content="Kenneth John Odle">
  7. <meta name="description" content="Examples of how to use variables in CSS">
  8. <meta name="viewport" content="width=device-width, initial-scale=1">
  9. <style>
  10. /* Reset */
  11. html, body, div, span, applet, object, iframe, img, fieldset, form, label, legend, caption {
  12. margin: 0;
  13. padding: 0;
  14. border: 0;
  15. outline: 0;
  16. vertical-align: baseline;
  17. background: transparent;
  18. font-family: Garamond, monospace;
  19. }
  20. #main {
  21. max-width: 900px;
  22. margin: 0 auto;
  23. padding: 0 2em;
  24. border-left: solid 1px grey;
  25. border-right: solid 1px grey;
  26. }
  27. .reset {
  28. max-height: 0;
  29. }
  30. pre {
  31. background: #efefef;
  32. padding: 0.5em;
  33. }
  34. code {
  35. font-size: 0.9em;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div id="main">
  41. <div class="reset">&nbsp;</div>
  42. <h1>The Before Times…</h1>
  43. <p>
  44. Before CSS was a thing, the only way to style HTML was to add styling information to each and every HTML element that you wanted to style. If you decided that you wanted all your H1 headings to be blue and all your H2 headings to be green, you had to add that information to each and every <code>h1</code> and <code>h2</code> element in your page. If you later decided to change your H1 headings to dark blue and your H2 headings to medium blue, you had to go through your code, line by line, and change each and every <code>h1</code> and <code>h2</code> element.
  45. </p>
  46. <p>
  47. CSS changed all that. Now it was possible to add a simple <code>&lt;style&gt;</code> element to your document's head and declare all your style choices there. By putting this information in a separate stylesheet, you could easily ensure that all the pages across a website would have identical styling.
  48. </p>
  49. <p>
  50. CSS is amazingly powerful. You can group selectors together, and you can use CSS classes to style multiple selectors or multiple elements, and use CSS ids to style a single element. However, it can still be difficult to change a group of elements.
  51. </p>
  52. <p>
  53. That's where CSS variables come into play.
  54. </p>
  55. <h1>Basic Usage</h1>
  56. <p>
  57. A css variable can be declared in either a page's <code>&lt;style&gt;</code> element, or in an external stylesheet. Its scope will extend to wherever that stylesheet is referenced. A basic definition looks like this:
  58. </p>
  59. <pre>
  60. element {
  61. --variable-name: value;
  62. }
  63. </pre>
  64. <p>
  65. A real-life defintion might look like this in the css for the definition:
  66. </p>
  67. <pre>
  68. ul {
  69. --bg-color: #000000;
  70. }
  71. </pre>
  72. <p>
  73. …and like this in the use of that variable:
  74. </p>
  75. <pre>
  76. .main {
  77. background-color: var(--bg-color);
  78. }
  79. </pre>
  80. <p>
  81. And like this in the html:
  82. </p>
  83. <pre>
  84. &lt;ul class="main"&gt;
  85. &lt;li&gt;First list item&lt;/li&gt;
  86. &lt;li&gt;Second list item&lt;/li&gt;
  87. &lt;/ul&gt;
  88. </pre>
  89. <p>
  90. The above variable works because we defined the <code>--bg-color</code> variable to operate under the scope of the <code>&lt;ul&gt;</code> element. To make a variable avaialable to <em>all</em> elements, define it under the <code>root</code> element:
  91. </p>
  92. <pre>
  93. :root {
  94. --bg-color: #000000;
  95. }
  96. </pre>
  97. <h2>What use is this?</h2>
  98. <p>
  99. While CSS classes make it easy to change the style of a number of different elements at once, CSS variables make it easy to change the style of a number of different selectors at once. This is valuable if you are still developing the overall appearance of a website, or if you know that the appearance will change later, and you want certain elements to share a particular style.
  100. </p>
  101. <h2>A Few Important Things to Note:</h2>
  102. <ol>
  103. <li>Variable names <strong>must</strong> be prefaced by two hyphens (<code>--</code>).</li>
  104. <li>Varialbe names are case-sensive. <code>--bg-Color</code> is not the same as <code>--bg-color</code>.</li>
  105. <li>Variables are invoked using the <code>var(--variable-name)</code> construct.</li>
  106. <li>Variables must be defined for a given element, which is then their scope.</li>
  107. <li>To make a variable available to multiple elements, define it under the <code>root</code> element (:root). Note the leading colon.</li>
  108. </ol>
  109. <hr />
  110. <h1>This is an H1 Element</h1>
  111. <h2>This is an H2 Element</h2>
  112. <h3>This is an H3 Element</h3>
  113. <h4>This is an H4 Element</h4>
  114. <h4>Below is a paragraph</h4>
  115. <p>
  116. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vulputate a erat id vehicula. Vestibulum viverra pulvinar velit non cursus. Curabitur euismod ullamcorper pellentesque. Quisque ac purus faucibus, dapibus diam suscipit, euismod enim. Curabitur aliquet enim sed metus rhoncus tempus. Etiam eget faucibus quam.
  117. </p>
  118. <h4>Below is another paragraph</h4>
  119. <p>
  120. Phasellus vitae convallis nibh. Quisque laoreet nulla tempus accumsan fermentum. Duis consequat velit vel enim cursus, efficitur lobortis enim ultricies. Aliquam vel lacinia turpis, vel eleifend libero. Aenean interdum eros tincidunt congue malesuada. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae.
  121. </p>
  122. <h4>This is an unordered list</h4>
  123. <ul>
  124. <li>puppies</li>
  125. <li>kittens</li>
  126. <li>bunnies</li>
  127. </ul>
  128. <h4>This is an ordered list</h4>
  129. <ul>
  130. <li>puppies</li>
  131. <li>kittens</li>
  132. <li>bunnies</li>
  133. </ul>
  134. </div>
  135. </body>
  136. </html>