Is it possible to create predefined css values?

Asked

Viewed 113 times

2

How can I add predefined CSS values? As if it were a variable to store.

For example, a color (Hex or RGB) so that I could use in the rest of the document, so instead of typing the color value, I would just type the name of the "variable".

3 answers

3

So far it is not possible to define values in the same way that we are used to declaring variables, at least not in a way cross-browser. One way to simulate this is to create classes that contain the properties that would be applied to the element, as shown by Chun.

Another alternative is to make use of preprocessors, but then I’d be running away from the theme of the question which is to declare values in the css file itself.

Well, there is one design in specification with the aim of making it possible to declare variables in sheets of styles, similar to the form that Less and Sass work. And the assignment of the value will be on account of the attribute var(), e. g: background-color: var(--meu-bg).

Firefox and Chrome already have implemented resource, but remember that it is still an experimental technology. In that article of the MDN there are more objective explanations about the reason for implementation and what problems it aims to solve.


Note: In Chrome this feature needs to be enabled by accessing the tab chrome://flags.


If you are using Firefox, the snippet below will work and you can see the use of variables in action:

:root {
  --cor-links: red;
  --borda: 2px solid blue;
  --sombra: 0 0 10px 5px rgba(0,0,0,.5);
}

a {
  color: var(--cor-links)
}

div {
  border: var(--borda);
  box-shadow: var(--sombra);
}
<a href='#'>Meu Link vermelho</a>

<br><br>

<div>Meu div com borda azul e sombra.</div>

  • 1

    Excellent answer Renan, just think that maybe to complement it would be nice to add a list with the types of preprocessor (Ruby, JS, etc). Of course it is only a suggestion and does not interfere with the quality of your answer. + 1

3

You can do this by creating specific classes for it.
For example, in your CSS create a class with the name preto, and with the value hex black:

.preto {color:#000;}

and in HTML just call the class as follows:

.preto {color:#000;}
.vermelho {color:#ff0000;}
/* e por aí em diante ... */
<div class="preto">Olá, eu sou uma classe com o texto com a cor preta</div>
<div class="vermelho">Olá, eu sou uma classe com o texto com a cor vermelha</div>

  • 1

    Chun great answer, just add the one example can use two classes, because this is the reason for the attribute class="" exist and its greatest "advantage". + 1

0

Yes, but not directly in CSS.

You must implement {LESS} or Saas to get.

These technologies support you to create variables and logics where these generate a css from predefined values.

  • Don’t get me wrong, I would like to know what your answer has that @Renan has not? I hope you understand as a constructive criticism ;)

  • 1

    Wow! Absolutely nothing more. Gafe my, I did not read Renan’s answer. I even gave him a +1. Thanks @Guilhermenascimento

Browser other questions tagged

You are not signed in. Login or sign up in order to post.