CSS: Pass values/parameters through class names

Asked

Viewed 327 times

0

I would like to know if there is the possibility of informing values through class names to be used as values in the properties of the same. Example:

.text-(color) {
color: (color);
}

The result would be:

.text-blue {
color: blue;
}

I read about pre-processors, I had never used, but until then I found nothing that refers to the solution of this doubt. Only a similar solution that is not yet supported by any browser: https://css-tricks.com/css-attr-function-got-nothin-custom-properties/#article-header-id-2

  • Look at this CSS writing methodology <input type="text" class="focus:bg-red"> here you have more details https://answall.com/questions/432733/methodologia-de-escrita-css-usando-no-classe-tailwind-css

1 answer

6

With pure CSS you can do using custom variables, it’s not exactly how you want but can help you

If you want a special color for the :hover, enough in the :root vc tb declare a color that will use in :hover, in case the var(--red), and in the p.azul:hover you change the value of --azul for var(--red)

See the example to better understand:

:root {
  --azul: blue;
  --red: red; /* cor do hover */
}

p.azul {
  color: var(--azul);
}

p.azul:hover {
  --azul: var(--red); /* muda a var Azul para ser a var da cor do Hover */
}
<p>lorem lorem</p>
<p class="azul">lorem lorem 1</p>
<p class="azul">lorem lorem 2</p>

  • That’s cool, huh, I didn’t know.

  • 1

    @Leandrade well mass right, and is native to CSS, no need to compile, and already has a good support of browsers

  • Good, very good also did not know. But if I want to inform the name of the color dynamically? Type the name in the class take the value of the color property? That’s what I’d like to know.

  • @Rodrigoalves see the edition and the comments I left in the answer code. As I said this is a pure CSS approach

  • I found a similar solution but is not yet supported by any browser: https://css-tricks.com/css-attr-function-got-nothin-custom-properties/#article-header-id-2

  • @Rodrigoalves actually take a look here, it will surely interest you! https://answall.com/questions/330390/existe-alguma-forma-de-usar-um-custom-attribute-como-valor-de-uma-propriedade

  • 1

    thanks @hugocsl, thanks for the alternative!

  • @Rodrigoalves without problems my dear

Show 3 more comments

Browser other questions tagged

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