CSS Terms Consent Checkbox

A required "I agree to the terms" checkbox with a link, which turns red with a message only after a failed submit.

  • terms
  • consent
  • agree
  • required
  • user-invalid
  • gdpr
HTML
<form class="consent">
  <label>
    <input type="checkbox" required aria-describedby="consent-error">
    <span>I agree to the <a href="#">Terms of Service</a> and <a href="#">Privacy Policy</a>.</span>
  </label>
  <p id="consent-error" class="consent-error">Please accept the terms to continue.</p>
  <button type="submit">Create account</button>
</form>
CSS
.consent {
  display: grid;
  gap: 10px;
  width: 260px;
  color: #1f2937;
  font: 14px/1.45 system-ui, sans-serif;
}

.consent label {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  cursor: pointer;
}

.consent input {
  flex: none;
  width: 18px;
  height: 18px;
  margin: 1px 0 0;
  accent-color: #4f46e5;
  cursor: pointer;
}

.consent input:focus-visible {
  outline: 2px solid #4f46e5;
  outline-offset: 2px;
}

.consent a {
  color: #4f46e5;
  font-weight: 600;
}

.consent a:focus-visible {
  outline: 2px solid #4f46e5;
  outline-offset: 2px;
  border-radius: 2px;
}

.consent-error {
  display: none;
  margin: -4px 0 0 28px;
  color: #dc2626;
  font-size: 13px;
}

.consent:has(input:user-invalid) .consent-error {
  display: block;
}

.consent input:user-invalid {
  outline: 2px solid #dc2626;
  outline-offset: 1px;
}

.consent button {
  padding: 10px;
  border: 0;
  border-radius: 10px;
  background: #4f46e5;
  color: #fff;
  font: 600 14px system-ui, sans-serif;
  cursor: pointer;
}

.consent button:focus-visible {
  outline: 2px solid #4f46e5;
  outline-offset: 2px;
}

About this CSS Terms Consent Checkbox

This CSS Terms Consent Checkbox is 70 lines of pure CSS (1.1 KB) with no JavaScript and no dependencies. It uses CSS Grid, Flexbox, the :has() selector and keyboard focus styles. It works in current versions of Chrome, Edge, Safari and Firefox. It works in any stack: plain HTML, React, Vue, Svelte, Astro, WordPress or Webflow.

CSS features used

Customize it

The colors in this snippet. Search and replace them to match your brand:

  • #1f2937
  • #4f46e5
  • #dc2626
  • #fff

How to use this terms consent checkbox

  1. Copy the HTML and paste it where you want the element to appear.
  2. Copy the CSS into your stylesheet, or inside a <style> tag.
  3. Keep the .consent class name, or rename it in both places. Then tweak colors, sizes and timing to match your design.
  4. Using React or Next.js? Change class to className in the HTML. The CSS works as is, including in Tailwind projects (put it in your global stylesheet).

Building with an AI tool?

“Copy for AI” copies a short prompt for Cursor, Claude Code, Lovable, Bolt or v0. Your AI tool opens the Markdown version of this snippet and reads the HTML, CSS and setup notes there. If your tool can't open links, with the code instead. More ways to use copy CSS with AI.

Feedback