<input> elements of type color provide a user interface element that lets a user specify a color, either by using a visual color picker interface or by entering the color into a text field in #rrggbb hexadecimal format. Only simple colors (without alpha channel) are allowed though CSS colors has more formats, e.g. color names, functional notations and a hexadecimal format with an alpha channel.

The element's presentation may vary substantially from one browser and/or platform to another—it might be a simple textual input that automatically validates to ensure that the color information is entered in the proper format, or a platform-standard color picker, or some kind of custom color picker window.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

A 7-character DOMString specifying a <color> in lower-case hexadecimal notation
事件 change and input
Supported common attributes autocomplete and list
IDL attributes list and value
方法 select()

value of an <input> element of type color is always a DOMString which contains a 7-character string specifying an RGB color in hexadecimal format. While you can input the color in either upper- or lower-case, it will be stored in lower-case form. The value is never in any other form, and is never empty.

注意 : Setting the value to anything that isn't a valid, fully-opaque, RGB color in hexadecimal notation will result in the value being set to #000000 . In particular, you can't use CSS's standardized color names, or any CSS function syntax, to set the value. This makes sense when you keep in mind that HTML and CSS are separate languages and specifications. In addition, colors with an alpha channel are not supported; specifying a color in 9-character hexadecimal notation (e.g. #009900aa ) will also result in the color being set to #000000 .

Using color inputs

Inputs of type color are simple, due to the limited number of attributes they support.

Providing a default color

You can update the simple example above to set a default value, so that the color well is pre-filled with the default color and the color picker (if any) will also default to that color:

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

If you don't specify a value, the default is #000000 , which is black. The value must be in seven-character hexadecimal notation, meaning the "#" character followed by two digits each representing red, green, and blue, like this: #rrggbb . If you have colors that are in any other format (such as CSS color names or CSS color functions such as rgb() or rgba() ), you'll have to convert them to hexadecimal before setting the value .

Tracking color changes

As is the case with other <input> types, there are two events that can be used to detect changes to the color value: input and change . input is fired on the <input> element every time the color changes. The change event is fired when the user dismisses the color picker. In both cases, you can determine the new value of the element by looking at its value .

Here's an example that watches changes over time to the color value:

colorPicker.addEventListener("input", updateFirst, false);
colorPicker.addEventListener("change", watchColorPicker, false);
function watchColorPicker(event) {
  document.querySelectorAll("p").forEach(function(p) {
    p.style.color = event.target.value;
  });
}

Selecting the value

<input> element's implementation of the color type on the user's browser doesn't support a color well, but is instead a text field for entering the color string directly, you can use the select() method to select the text currently in the edit field. If the browser instead uses a color well, select() does nothing. You should be aware of this behavior so your code can respond appropriately in either case.

colorWell.select();

Appearance variations

As previously mentioned, when a browser doesn't support a color picker interface, its implementation of color inputs will be a text box that validates the contents automatically to ensure that the value is in the correct format. For example, in Safari 10.1, you would see something that looks looks like this:

Screenshot of the example taken in Safari.

The same content looks like this in Firefox 55:

Screenshot of the example taken in Firefox 55 for macOS

In this case, clicking on the color well presents the platform's color picker for you to choose a color from (in this case, the macOS picker):

Screenshot of the element with the color picker open in Firefox Mac.

验证

A color input's value is considered to be invalid if the 用户代理 is unable to convert the user's input into seven-character lower-case hexadecimal notation. If and when this is the case, the :invalid pseudo-class is applied to the element.

范例

Let's create an example which does a little more with the color input by tracking the change and input events to take the new color and apply it to every <p> element in the document.

HTML

The HTML is fairly straightforward — a couple of paragraphs of descriptive material with an <input> 类型 color with the ID colorWell , which we'll use to change the color of the paragraphs' text.

<p>An example demonstrating the use of the <code><input type="color"></code>
   control.</p>
<label for="colorWell">Color:</label>
<input type="color" value="#ff0000" id="colorWell">
<p>Watch the paragraph colors change when you adjust the color picker.
   As you make changes in the color picker, the first paragraph's
   color changes, as a preview (this uses the <code>input</code>
   event). When you close the color picker, the <code>change</code>
   event fires, and we detect that to change every paragraph to
   the selected color.</p>

JavaScript

First, there's some setup. Here we establish some variables, setting up a variable that contains the color we'll set the color well to when we first load up, and then setting up a load handler to do the main startup work once the page is fully loaded.

var colorWell;
var defaultColor = "#0000ff";
window.addEventListener("load", startup, false);

初始化

Once the page is loaded, our load event handler, startup() , is called:

function startup() {
  colorWell = document.querySelector("#colorWell");
  colorWell.value = defaultColor;
  colorWell.addEventListener("input", updateFirst, false);
  colorWell.addEventListener("change", updateAll, false);
  colorWell.select();
}

This gets a reference to the color <input> element in a variable called colorWell , then sets the color input's value to the value in defaultColor . Then the color input's input event is set up to call our updateFirst() function, and the change event is set to call updateAll() . These are both seen below.

最后,调用 select() to select the text content of the color input if the control is implemented as a text field (this has no effect if a color picker interface is provided instead).

Reacting to color changes

We provide two functions that deal with color changes. The updateFirst() function is called in response to the input event. It changes the color of the first paragraph element in the document to match the new value of the color input. Since input events are fired every time an adjustment is made to the value (for example, if the brightness of the color is increased), these will happen repeatedly as the color picker is used.

function updateFirst(event) {
  var p = document.querySelector("p");
  if (p) {
    p.style.color = event.target.value;
  }
}

When the color picker is dismissed, indicating that the value will not be changing again (unless the user re-opens the color picker), a change event is sent to the element. We handle that event using the updateAll() function, using Event.target.value to obtain the final selected color:

function updateAll(event) {
  document.querySelectorAll("p").forEach(function(p) {
    p.style.color = event.target.value;
  });
}

This sets the color of every <p> block so that its color attribute matches the current value of the color input, which is referred to using event.target .

结果

The final result looks like this:

规范

规范 状态 注释
HTML 实时标准 实时标准 初始定义
HTML5 推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
type="color" Chrome 完整支持 20 Edge 完整支持 14 Firefox 完整支持 29 注意事项
完整支持 29 注意事项
注意事项 Firefox doesn't yet support inputs of type color on Windows Touch.
IE 不支持 No Opera 完整支持 12 Safari 完整支持 12.1 WebView Android 完整支持 4.4 Chrome Android 完整支持 25 Firefox Android 完整支持 27 Opera Android 完整支持 12 Safari iOS 完整支持 12.2 Samsung Internet Android 完整支持 1.5
autocomplete Chrome 完整支持 20 Edge 完整支持 14 Firefox 不支持 No 注意事项
不支持 No 注意事项
注意事项 bug 960989 for the status of support for the autocomplete attribute in Firefox.
IE 不支持 No Opera ? Safari ? WebView Android ? Chrome Android ? Firefox Android 不支持 No 注意事项
不支持 No 注意事项
注意事项 bug 960984 for the status of support for the list attribute in Firefox.
Opera Android ? Safari iOS ? Samsung Internet Android ?
list Chrome 完整支持 20 Edge 完整支持 14 Firefox 不支持 No 注意事项
不支持 No 注意事项
注意事项 bug 960984 for the status of support for the list attribute in Firefox.
IE 不支持 No Opera ? Safari 完整支持 12.1 WebView Android ? Chrome Android ? Firefox Android 不支持 No 注意事项
不支持 No 注意事项
注意事项 bug 960984 for the status of support for the list attribute in Firefox.
Opera Android ? Safari iOS 完整支持 12.2 Samsung Internet Android ?

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

见实现注意事项。

见实现注意事项。

另请参阅

元数据

  • 最后修改:
  1. <button>
  2. <datalist>
  3. <fieldset>
  4. <form>
  5. <input>
  6. <label>
  7. <legend>
  8. <meter>
  9. <optgroup>
  10. <option>
  11. <output>
  12. <progress>
  13. <select>
  14. <textarea>
  15. <input> 类型
    1. <input type="button">
    2. <input type="checkbox">
    3. <input type="color">
    4. <input type="date">
    5. <input type="datetime">
    6. <input type="datetime-local">
    7. <input type="email">
    8. <input type="file">
    9. <input type="hidden">
    10. <input type="image">
    11. <input type="month">
    12. <input type="number">
    13. <input type="password">
    14. <input type="radio">
    15. <input type="range">
    16. <input type="reset">
    17. <input type="search">
    18. <input type="submit">
    19. <input type="tel">
    20. <input type="text">
    21. <input type="time">
    22. <input type="url">
    23. <input type="week">
  16. HTML 元素
    1. A
      1. <a>
      2. <abbr>
      3. <acronym>
      4. <address>
      5. <applet>
      6. <area>
      7. <article>
      8. <aside>
      9. <audio>
    2. B
      1. <b>
      2. <base>
      3. <basefont>
      4. <bdi>
      5. <bdo>
      6. <bgsound>
      7. <big>
      8. <blink>
      9. <blockquote>
      10. <body>
      11. <br>
      12. <button>
    3. C
      1. <canvas>
      2. <caption>
      3. <center>
      4. <cite>
      5. <code>
      6. <col>
      7. <colgroup>
      8. <content>
    4. D
      1. <data>
      2. <datalist>
      3. <dd>
      4. <del>
      5. <details>
      6. <dfn>
      7. <dialog>
      8. <dir>
      9. <div>
      10. <dl>
      11. <dt>
    5. E
      1. <em>
      2. <embed>
    6. F
      1. <fieldset>
      2. <figcaption>
      3. <figure>
      4. <font>
      5. <footer>
      6. <form>
      7. <frame>
      8. <frameset>
    7. G H
      1. <h1>
      2. <h2>
      3. <h3>
      4. <h4>
      5. <h5>
      6. <h6>
      7. <head>
      8. <header>
      9. <hgroup>
      10. <hr>
      11. <html>
    8. I
      1. <i>
      2. <iframe>
      3. <img>
      4. <input>
      5. <ins>
      6. <isindex>
    9. J K
      1. <kbd>
      2. <keygen>
    10. L
      1. <label>
      2. <legend>
      3. <li>
      4. <link>
      5. <listing>
    11. M
      1. <main>
      2. <map>
      3. <mark>
      4. <marquee>
      5. <menu>
      6. <menuitem>
      7. <meta>
      8. <meter>
    12. N
      1. <nav>
      2. <nobr>
      3. <noframes>
      4. <noscript>
    13. O
      1. <object>
      2. <ol>
      3. <optgroup>
      4. <option>
      5. <output>
    14. P
      1. <p>
      2. <param>
      3. <picture>
      4. <plaintext>
      5. <pre>
      6. <progress>
    15. Q
      1. <q>
    16. R
      1. <rp>
      2. <rt>
      3. <rtc>
      4. <ruby>
    17. S
      1. <s>
      2. <samp>
      3. <script>
      4. <section>
      5. <select>
      6. <shadow>
      7. <slot>
      8. <small>
      9. <source>
      10. <spacer>
      11. <span>
      12. <strike>
      13. <strong>
      14. <style>
      15. <sub>
      16. <summary>
      17. <sup>
    18. T
      1. <table>
      2. <tbody>
      3. <td>
      4. <template>
      5. <textarea>
      6. <tfoot>
      7. <th>
      8. <thead>
      9. <time>
      10. <title>
      11. <tr>
      12. <track>
      13. <tt>
    19. U
      1. <u>
      2. <ul>
    20. V
      1. <var>
      2. <video>
    21. W
      1. <wbr>
    22. X Y Z
      1. <xmp>

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1