Skip to content
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

fix(css): update opacity example from ::placeholder #37480

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions files/en-us/web/css/_doublecolon_placeholder/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ input::placeholder {
color: red;
font-size: 1.2em;
font-style: italic;
opacity: 0.5;
}
```

Expand All @@ -98,30 +99,41 @@ input::placeholder {

### Opaque text

Some browsers (such as Firefox) set the default {{cssxref("opacity")}} of placeholders to something less than 100%. If you want fully opaque placeholder text, set `opacity` to `1`.
Some browsers make placeholder text less opaque. If you want fully opaque text, then set the {{CSSXref("color")}} property value explicitly. The [`currentColor`](/en-US/docs/Web/CSS/color_value#currentcolor_keyword) value can be used to have the same color as the corresponding input element.

#### HTML

```html
<input placeholder="Default opacity" />
<input placeholder="Full opacity" class="force-opaque" />
<input placeholder="Color set by browser" />
<input placeholder="Same color as input" class="explicit-color" />
OnkarRuikar marked this conversation as resolved.
Show resolved Hide resolved
<input placeholder="Semi-opaque text color" class="opacity-change" />
```

#### CSS

```css
::placeholder {
input {
font-weight: bold;
color: green;
}

.force-opaque::placeholder {
opacity: 1;
.explicit-color::placeholder {
/* use the same color as input element to avoid the browser set default color */
color: currentColor;
}

.opacity-change::placeholder {
/* less opaque text */
color: color-mix(in srgb, currentColor 70%, transparent);
}
```

#### Result

{{EmbedLiveSample("Opaque_text", 200, 60)}}
{{EmbedLiveSample("default_color", 200, 60)}}

> [!NOTE]
> Note that browsers use different default colors for placeholder text. For example, Firefox uses the input element's color with 54% opacity, and Chrome uses `darkgray` color. If you want consistent placeholder text color across the browsers, then set the `color` explicitly.

## Specifications

Expand Down
Loading