Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 4 - Reference a color by root name in css [duplicate]

I'm using a nice Bootstrap 4 css theme which overrides default one with very nice colors but I'm not being able to "reference" a color by its root name from css.

Lets explain better, Bootstrap has some root colors which names are "success", "danger", "dark", "light" and so (and so the custom theme css template), and -of course- they have an hex representation and I'd like to apply one of this colors to a link hover calling it by its root name and not by its hex code.

To be more clear, I'd like to be able to do something like the next:

.dropdown-menu > a:hover {
  color: success; /*or dark or danger or whatever */
}

This way, if I change the template in the future, all colors will adjust autommatically to the new template values and all app appearence will stay in "harmony" with the new general template aspect.

I'm not sure this is possible using pure css but maybe there is a workaround.

As you see, my goal is to be able to have as much colors as I can referenced by name (let's say dynamically instead of statically using hex) so if I change the bootstrap css template all colors adjust autommatically to new template values.

Any help / ideas?

like image 514
Diego Perez Avatar asked Sep 26 '18 11:09

Diego Perez


People also ask

How do you call a root in CSS?

The :root CSS pseudo-class matches the root element of a tree representing the document. In HTML, :root represents the <html> element and is identical to the selector html , except that its specificity is higher.

How do I override Bootstrap CSS variables?

Override Variables Bootstrap has its own “_variables. scss” in the “scss” folder, which contains all variables used in Bootstrap. Now, add the “abstracts” name folder in your scss folder and create “_custom-variables. scss” in that folder.

Can you define colors in CSS?

The most common way to specify colors in CSS is to use their hexadecimal (or hex) values. Hex values are actually just a different way to represent RGB values. Instead of using three numbers between 0 and 255, you use six hexadecimal numbers.


1 Answers

They are CSS Variables. You can reference them like...

.dropdown-menu > a:hover {
  color: var(--success);
}

https://codeply.com/go/vFE9n0VrGz

Related: CSS use color from another class?

like image 90
Zim Avatar answered Oct 19 '22 22:10

Zim