HomeWeb DevelopmentHow to Use HTML Color Picker
💻 Developer Tutorial

How Do I Use a Color Picker in HTML?

By Color Picker Web · Updated September 2026 · 16 min read

You know that HTML5 has a built-in color picker, but adding an <input type="color"> to your webpage is only the first step. The real magic happens when you figure out how to use it—how to extract the color the user just selected and use it to instantly change the background of a div, update text colors, or completely re-theme your entire website in real-time.

To use an HTML color picker effectively, you need a basic understanding of three things: The HTML input tag, Vanilla JavaScript event listeners (specifically the 'input' event), and DOM manipulation.

In this comprehensive tutorial, we will walk you through the exact steps to implement a functioning color picker. We will start with simple background changes and move to advanced CSS Variable (`--var`) injections. Want to see what we are building? Play with the Live Theme Customizer below!

🧪 Try It: Live Theme Customizer Sandbox

Click the color bar below to open your device's native color picker. As you drag the color wheel, watch how the JavaScript instantly updates the CSS Variable (`--dynamic-theme`), completely changing the styling of the mock UI Card in real-time.
⚙️ Interactive UI Demo
HEX Output:
#9333ea

Welcome Back!

This entire card's border and button color are linked to a single CSS variable. Change the color picker to see the magic happen.

HTML
CSS
JavaScript
<!-- 1. The Color Picker -->
<input type="color" id="themePicker" value="#9333ea">

<!-- 2. The UI Element -->
<div class="mock-card" id="mockCard">
  <h4>Welcome Back!</h4>
  <button class="mock-btn">Confirm Action</button>
</div>
/* 1. Define a CSS Variable with a default color */
:root {
  --dynamic-theme: #9333ea;
}

/* 2. Apply the variable to your elements */
.mock-card {
  border-top: 6px solid var(--dynamic-theme);
}
.mock-btn {
  background-color: var(--dynamic-theme);
}
// 1. Select the color picker and the root element
const picker = document.getElementById('themePicker');
const root = document.documentElement;

// 2. Listen for the 'input' event (fires in real-time)
picker.addEventListener('input', function(event) {
  
  // 3. Extract the HEX code from the picker
  const selectedColor = event.target.value;
  
  // 4. Update the CSS Variable globally
  root.style.setProperty('--dynamic-theme', selectedColor);
});

🛠️ Step 1: The Basic HTML Tag

The foundation of using a color picker is dropping the HTML5 element into your markup. You don't need any external libraries for this.

<input type="color" id="myColorPicker" value="#ff0000">

Rules for the `value` attribute:

  • It dictates the starting color when the page loads.
  • It must be a 7-character hexadecimal string (e.g., `#ff0000`).
  • If you try to use `#f00`, `red`, or `rgb(255,0,0)`, the browser will completely ignore it and force the picker to default to Black (`#000000`).

⚡ Step 2: Capturing the Color with JavaScript

To actually use the color, you must use JavaScript to "listen" to the picker and grab its value whenever the user interacts with it. We do this using an Event Listener.

There are two primary events you can listen for: input and change. If you want the background color of your website to update smoothly and continuously as the user drags the mouse across the color spectrum, you must use the input event.

// Target the input element
const picker = document.getElementById('myColorPicker');

// Add an event listener for real-time updates
picker.addEventListener('input', function(event) {
  
  // Extract the 6-digit HEX code
  let hexCode = event.target.value; 
  
  // Apply it directly to the body's background
  document.body.style.backgroundColor = hexCode; 
});

🎨 Step 3: The Modern Way (Changing CSS Variables)

Targeting individual elements (like document.body.style.backgroundColor) works fine for small projects. However, if you want to build a "Dark Mode" toggle or a "Theme Customizer" where changing the color picker updates the buttons, headers, and borders all at once, you should use CSS Variables.

Instead of updating 50 different HTML elements in your JavaScript, you update one single CSS variable at the :root level. All the elements connected to that variable will instantly change color (exactly how our interactive Sandbox tool above works!).

// JavaScript: Update a CSS variable globally
const root = document.documentElement;

picker.addEventListener('input', (e) => {
  // Set the CSS variable '--theme-color' to the new HEX code
  root.style.setProperty('--theme-color', e.target.value);
});

🚀 Need to Convert HEX to RGB?

The native HTML color picker only gives you HEX codes. If your CSS requires RGB or HSL strings, use our free online Color Converter to automatically translate the code.

Open Color Converter →

🚫 Common Mistakes When Using the HTML Picker

  1. Using the `change` event for Live Previews: The change event only fires after the user clicks away or closes the color picker window. If you want a smooth, real-time preview while dragging, you must use the input event.
  2. Trying to get Opacity (Alpha): The native <input type="color"> is strictly locked to solid, 6-digit HEX codes. It is physically impossible to get an 8-digit HEX or RGBA (transparent) color out of it, even if the Mac color wheel shows an opacity slider.
  3. Forgetting the `#` symbol: When extracting event.target.value, the string will always include the hash (e.g., `#1A2B3C`). If you are passing this to an API that expects just the 6 letters, you must strip the hash using JavaScript: event.target.value.replace('#', '').

📦 Advanced: When to Use External Libraries

While the native HTML5 picker is fantastic, lightweight, and requires no dependencies, there are times when you absolutely must use an external JavaScript library (like Pickr, Spectrum, or React Color).

You should upgrade to a JavaScript library if your project requires:

  • Alpha / Transparency Support: If users need to pick semi-transparent colors for overlays or shadows.
  • Consistent Cross-Browser UI: The native HTML picker looks like an Apple wheel on Mac, a grid on Windows, and a bottom-sheet on iPhone. If you want the picker to look exactly the same on every device, you must use a library.
  • Preset Palettes: If you want to offer the user a grid of 5 "Recommended Brand Colors" beneath the color picker to restrict their choices.
  • Direct RGB/HSL Output: If you want the picker to output rgba(255, 0, 0, 0.5) automatically without writing your own HEX-to-RGB conversion math.

❓ Frequently Asked Questions

How do I get the value from an HTML color picker?

You can get the value using Vanilla JavaScript. Select the input element and read its .value property inside an event listener (event.target.value). It will always return a string formatted as a 7-character HEX code (e.g., #00FF00).

How do I change the default color of the HTML color picker?

You change the default color by setting the value attribute directly in your HTML tag. For example: <input type="color" value="#0088ff">. The value must be a valid 6-digit hex code with the hash symbol included.

Can I use the HTML color picker to change CSS variables?

Yes! Inside your JavaScript input event listener, you can use document.documentElement.style.setProperty('--your-variable', event.target.value) to dynamically update CSS custom properties across your entire site instantly.

Why is my HTML color picker always black?

If you set the value attribute to an invalid format (like "blue", "rgb(0,0,255)", or a 3-digit shorthand hex like "#00f"), the browser safely rejects it and defaults to black (#000000). You must use the full 7-character hex format (#0000FF).

Do I need jQuery to use the HTML color picker?

No. The <input type="color"> element is a native HTML5 feature and can be fully controlled using modern, lightweight Vanilla JavaScript (like document.querySelector and addEventListener). No external libraries like jQuery are required.

← Previous Article Does HTML Have a Built-in Color Picker? (2026 Guide) Next Article → How to Use Eyedropper on Google? (Chrome, Docs & Search) 2026