<div class="warning">
	<p>⚠️ Your browser does not support the CSS <code>:has()</code> selector so this demo won't work. Please try Safari TP 137.</p>
</div>

<main>
	<h1><a href="https://brm.us/css-has" target="_top"><code>:has()</code></a> Adjust label based on input validity</h1>
	<form onsubmit="return false;" autocomplete="off">
		<label for="email">E-mail</label>
		<input type="email" name="email" id="email" value="bramus" placeholder=" " autocomplete="off" autocorrect="off" autocapitalize="off" optional />

		<button type="submit" aria-label="Example button" onclick="event.preventDefault()">Submit</button>
		<p><em>💁‍♂️ In browsers that support <code>:has()</code>, the label will color red when the input is not valid. When it's valid, it'll color green. Additionally, (optional) is automatically injected after the label based on the input attribute.</p>
	</form>
</main>

<footer>
	<p>Demo for <a href="https://twitter.com/bramus/status/1473439384921944065" target="_top">https://twitter.com/bramus/status/1473439384921944065</a></p>
</footer>
/* Color the label red when the input is invalid */
label:has(+ :not(:focus):not(:placeholder-shown):invalid) {
	color: red;
}
label:has(+ input:not(:focus):not(:placeholder-shown):invalid)::before {
	content: "❌ ";
}

/* Color the label green when the input is valid */
label:has(+ :not(:focus):not(:placeholder-shown):valid) {
	color: green;
}
label:has(+ :not(:focus):not(:placeholder-shown):valid)::before {
	content: "✅ ";
}

/* Icon to show when no input is entered */
label:has(+ :not(:focus):placeholder-shown)::before {
	content: "✉️ ";
}

/* Default icon */
label::before {
	content: "💬 ";
}

/* Inject "optional" after optional fields */
label:has(+ :optional)::after {
  content: " (optional)";
	font-style: italic;
	font-size: 0.8em;
}

/*
  🤔 Why `:not(:focus):not(:placeholder-shown):invalid` and not simply `:invalid`?
  👉 https://www.bram.us/2021/01/28/form-validation-you-want-notfocusinvalid-not-invalid/
*/




/* Non-essentials below */



* {
	box-sizing: border-box;
}

html {
	max-width: 70ch;
	padding: 3em 1em;
	margin: auto;
	line-height: 1.75;
	font-size: 1.25em;
}

/* Borrowed from pico … */
:root {
	--typography-spacing-vertical: 1.5rem;
	--line-height: 1.5;
	--font-weight: 400;
	--font-size: 16px;
	--font-family: system-ui, -apple-system, "Segoe UI", "Roboto", "Ubuntu",
		"Cantarell", "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji",
		"Segoe UI Symbol", "Noto Color Emoji";
	--spacing: 1rem;
	--form-element-spacing-vertical: 0.25rem;
	--form-element-spacing-horizontal: 0.5rem;
	--border-radius: 0.25rem;
	--border-width: 1px;
	--primary: #1095c1;
	--primary-inverse: #fff;
	    --primary-hover: #08769b;
	--button-box-shadow: 0 0 0 rgba(0, 0, 0, 0);
	
	    --block-spacing-vertical: calc(var(--spacing) * 2);
	    --block-spacing-horizontal: calc(var(--spacing) * 1.75);
	--card-background-color: #f4f4f4;
	--card-box-shadow:0 0.125rem 1rem rgba(27, 40, 50, 0.04),0 0.125rem 2rem rgba(27, 40, 50, 0.08),0 0 0 0.0625rem rgba(27, 40, 50, 0.024);
	--transition: 0.2s ease-in-out;
}

body {
	font-family: var(--font-family);
	font-weight: var(--font-weight);
	font-size: var(--font-size);
	text-rendering: optimizeLegibility;
	line-height: var(--line-height);
}

form {
    margin: var(--block-spacing-vertical) 0;
    padding: var(--block-spacing-vertical) var(--block-spacing-horizontal);
    overflow: hidden;
    border-radius: var(--border-radius);
    background: var(--card-background-color);
    box-shadow: var(--card-box-shadow);
}

label {
	display: block;
	margin-bottom: calc(var(--spacing) * 0.25);
}

input {
	margin: 0;
	font-size: 1rem;
	font-family: inherit;
	letter-spacing: inherit;
	--border-color: currentcolor;
	border: var(--border-width) solid var(--border-color);
	border-radius: var(--border-radius);
	outline: 0;
	width: 100%;
	margin-bottom: var(--spacing);
	height: calc(
		1rem * var(--line-height) + var(--form-element-spacing-vertical) * 2 +
			var(--border-width) * 2
	);
	appearance: none;
	padding: var(--form-element-spacing-vertical)
		var(--form-element-spacing-horizontal);
}

input:focus {
    --border-color: var(--primary);
}

button {
    display: block;
    width: 100%;
    margin-bottom: var(--spacing);
    --background-color: var(--primary);
    --border-color: var(--primary);
    --color: var(--primary-inverse);
    --box-shadow: var(--button-box-shadow, 0 0 0 rgba(0, 0, 0, 0));
    padding: var(--form-element-spacing-vertical) var(--form-element-spacing-horizontal);
    border: var(--border-width) solid var(--border-color);
    border-radius: var(--border-radius);
    outline: 0;
    background-color: var(--background-color);
    box-shadow: var(--box-shadow);
    color: var(--color);
    font-weight: var(--font-weight);
    font-size: 1rem;
    line-height: var(--line-height);
    text-align: center;
    cursor: pointer;
    transition: background-color var(--transition),border-color var(--transition),color var(--transition),box-shadow var(--transition);
    -webkit-appearance: button;
}

button:hover {
	--background-color: var(--primary-hover);
    --border-color: var(--primary-hover);
    --box-shadow: var(--button-hover-box-shadow, 0 0 0 rgba(0, 0, 0, 0));
}

main {
	margin: calc(var(--spacing) * 3.5) 0;
}

footer {
	text-align: center;
	font-size: 0.8rem;
	font-style: italic;
}

/* Show/Hide Warning if no container Queries Support */
@supports (selector(:has(*))) {
	.info,
	.warning {
		display: none;
	}
}
.info,
.warning {
	padding: var(--spacing);
	border: var(--border-width) solid #123456;
	background: rgba(205 230 255 / 0.8);
	color: #123456;
	border-radius: var(--border-radius);
}
.warning {
	background: #ffe3003d;
	border-color: #ffc700;
	color: #333;
}

.info p,
.warning p {
	margin: 0;
	padding: 0;
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.