Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TailwindCSS use @apply with placeholder color

Tags:

tailwind-css

I am trying to use @apply together with placeholder color in TailwindCSS, but for some reason, it does not seem to work although I am able to use @apply together with other properties. I am also able to use the placeholder color options as a CSS class. It just doesn't work with @apply.

@tailwind base;

input {
  @apply placeholder-gray-900;
}

@tailwind components;

@tailwind utilities;

By trying this I end up with this error:

`@apply` cannot be used with `.placeholder-gray-900` because `.placeholder-gray-900` either cannot be found, or its actual definition includes a pseudo-selector like :hover, :active, etc. If you're sure that `.placeholder-gray-900` exists, make sure that any `@import` statements are being properly processed *before* Tailwind CSS sees your CSS, as `@apply` can only be used for classes in the same CSS tree.
like image 204
Tore Avatar asked Dec 04 '19 21:12

Tore


People also ask

How do you style placeholder tailwind?

In Tailwind CSS, you can style the placeholder text of an input field (or textarea) by using the placeholder modifier (a modifier is a prefix word that can be added to the beginning of a utility class). That's it.

Can we give color to placeholder?

In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholder selector. Note that Firefox adds a lower opacity to the placeholder, so we use opacity: 1 to fix this.

How do you style placeholder text?

Use the ::placeholder pseudo-element to style your placeholder text in an <input> or <textarea> form element. Most modern browsers support this, but for older browsers, vendor prefixes will be required.

How do I change placeholder color in Wordpress?

1) Go to the plugin settings page and open "Appearance" tab; 2) Find and enable the "Style options" checkbox. 3) Find the "Text color" section and make necessary changes in the "Placeholder color" field. 4) Save changes.


1 Answers

This is because the placeholder text is changed with a pseudo-selector, ::placeholder. If you look at the placeholder docs it's shown in light gray after each class definition.

As you can't @apply classes with a pseudo-selector, you'll need to add the pseudo-selector to your code, something like this (note you'll need to use the text color utilities here):

input::placeholder {
  @apply text-gray-900;
}
like image 160
Laurens Avatar answered Oct 10 '22 12:10

Laurens