css use variable code example

Example 1: css set variable

:root {
  --main-bg-color: coral;
}

#div1 {
  background-color: var(--main-bg-color);
}

#div2 {
  background-color: var(--main-bg-color);
}

Example 2: css variable

:root {
  --main-bg-color: pink;
}
body {
  background-color: var(--main-bg-color);
}

Example 3: variables css

:root {
    --main-bg-color: brown;
  }
  
.uno {
    color: white;
    background-color: var(--main-bg-color);
    margin: 10px;
    width: 50px;
    height: 50px;
    display: inline-block;
}

Example 4: css variables

:root {
  --background-color: #333;
  --text-color: #fff;
}
body {
  background-color: var(--background-color);
  color: var(--text-color);
}

Example 5: css variables

/* "css variables" */
/* not for ie but good for Edge and other browser :D */

/*
 By declaring a custom property on the :root pseudo-class
 and using it where needed throughout the document
*/
:root {
  --primary-bg: #8a2be2;
  --btn-font-size: 18px;
  --btn-padding: 10px 15px;
}

.btn-primary {
  background-color: var(--primary);
  font-size: var(--btn-font-size);
  padding: var(--btn-padding);
  color: #E2E2E2;
}

Example 6: css variable

...
<style>
	:root{
  		--couleur-principale: brown;
	}
	#test1{
		color:var(--couleur-principale);
	}
</style>
...

Tags:

Misc Example