Skip to content

[css-forms-1] Pseudo-element, structure and styles for <input type="color"> #11837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lukewarlow opened this issue Mar 5, 2025 · 9 comments
Closed

Comments

@lukewarlow
Copy link
Member

lukewarlow commented Mar 5, 2025

CSS Forms 1 currently doesn't define a structure or pseudo-elements for <input type="color">, this issue is to discuss what these should be.


There's currently two different structures across engines:

Firefox:

└─ '::-moz-color-swatch'

Chrome:

└─ '::-webkit-color-swatch-wrapper'
   └─ '::-webkit-color-swatch'

WebKit:

└─ '::-webkit-color-swatch-wrapper'
   └─ '::-webkit-color-swatch'
      └─ no-pseudo but used for alpha value display

All of these are web exposed and can be styled (at least with a background-color per my testing).

The use case of the ::-webkit-color-swatch-wrapper element is unclear to me. Given this I suspect we should align on the Firefox model of a single ::color-swatch pseudo element.

This can potentially be defined to have a background-color: control-value(<color>) if we extend control-value to work for color inputs?

@nt1m
Copy link
Member

nt1m commented Mar 5, 2025

We definitely something at least for the swatch, especially since it now supports alpha (and displays an indicator) in WebKit.

@svgeesus
Copy link
Contributor

svgeesus commented Mar 5, 2025

The swatch may need support for out-of-gamut warning, too.

@lukewarlow lukewarlow marked this as a duplicate of #11853 Mar 6, 2025
@lukewarlow
Copy link
Member Author

lukewarlow commented Mar 6, 2025

<input type=color> has a color well inside the button in browsers today. Should there be a new pseudo-element for this, or should it be using ::before or ::after or content: on the element itself?

From Simon's duplicate issue.

@svgeesus
Copy link
Contributor

svgeesus commented Mar 6, 2025

a a single ::color-swatch pseudo element sounds like a higher-level way to standardize this, while ::before sounds like a pre-standardization work around.

I agree that the button and the color well are separate, which may well be what the wrapper element was for.

@tabatkins
Copy link
Member

The button is just the element itself, tho. ::color-swatch would be the colored block inside of it.

@lukewarlow
Copy link
Member Author

Something like this for the swatch styles looks okay. Would need to think more on how to handle alpha.

::color-swatch {
    background-color: control-value(<color>);
    width: 1.5em;
    border: 1px solid currentColor;
}

Per the current spec styles + my idea above you end up with:

Image

@lukewarlow lukewarlow changed the title [css-forms-1] Pseudo-elements and structure for <input type="color"> [css-forms-1] Pseudo-element, structure and styles for <input type="color"> Mar 6, 2025
@nt1m nt1m added the Agenda+ label Mar 6, 2025
@tabatkins
Copy link
Member

For alpha, give it the ol' standard checkerboard background?

<!DOCTYPE html>
<style>
button::before {
 content: "";
 display: block;
 background: linear-gradient(var(--color) 0 0), linear-gradient(45deg, #808080 26%, transparent 0 74%, #808080 0), linear-gradient(45deg, #808080 26%, transparent 0 74%, #808080 0);
 background-size: 100% 100%, 10px 10px, 10px 10px;
 background-position: 0 0, 0 0, 5px 5px;
 width: 1.5em;
 height: 1em;
 border: 1px solid currentColor;
}
</style>
<button style="--color: rgba(255 0 0 / .5)"></button>

@lukewarlow
Copy link
Member Author

lukewarlow commented Mar 6, 2025

This seems to replicate the WebKit approach of a single white and black triangle background (looks a bit nicer on smaller area than the checkers imo).

<!doctype html>
<style>
  button::before {
    content: "";
    display: block;
    background: linear-gradient(var(--color)),
      linear-gradient(to bottom right, black 50%, white 50%);
    width: 1.5em;
    height: 1em;
    border: 1px solid currentColor;
  }
</style>
<button style="--color: rgba(255 0 0 / 0.5)"></button>

So styles like:

::color-swatch {
    background: linear-gradient(control-value(<color>)),
      linear-gradient(to bottom right, black 50%, white 50%);
    width: 1.5em;
    border: 1px solid currentColor;
}

@astearns astearns moved this to Regular agenda in CSSWG April 2025 meeting agenda Mar 27, 2025
@astearns astearns moved this from Regular agenda to By Topic in CSSWG April 2025 meeting agenda Mar 27, 2025
@astearns astearns moved this from By Topic to Tuesday Afternoon in CSSWG April 2025 meeting agenda Mar 27, 2025
@css-meeting-bot
Copy link
Member

The CSS Working Group just discussed [css-forms-1] Pseudo-element, structure and styles for `<input type="color">` , and agreed to the following:

  • RESOLVED: Use the name ::color-swatch
The full IRC log of that discussion <TabAtkins> ntim: pseudo-elements for color input
<TabAtkins> ntim: there's a proposal for ::color-swatch
<TabAtkins> ntim: is everyone happy with this?
<TabAtkins> TabAtkins: this represents the little rectangle of color in the input
<lea> q+
<TabAtkins> ntim: yeah. we can't use the element itself because we support alpha on the color, and want to be able to display the checkerboard behind the swatch to make that visible
<astearns> ack lea
<TabAtkins> lea: in a color input you can link to a data list
<TabAtkins> lea: would these swatches have the same pseudo element?
<TabAtkins> ntim: this is just the main input element, not the picker
<fantasai> The in-page component of the control
<TabAtkins> ntim: not all the browser have the same UI for the picker right now too
<TabAtkins> astearns: so resolution is to use ::color-swatch as the name for the in-page representation of the chosen color
<TabAtkins> astearns: comments or questions?
<TabAtkins> astearns: objections?
<TabAtkins> RESOLVED: Use the name ::color-swatch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Tuesday Afternoon
Development

No branches or pull requests

5 participants