Customizable select example

Customizable select example with appearance: base-select. Native picker, options that can hold images, styled in CSS.

The problem

The challenge with traditional select elements is that they offer limited customization options and can be difficult to consistently across different browsers.

The method

By using the appearance: base-select property, we can create a customizable select element that allows for more control over its appearance and behavior. This enables us to include additional elements, such as SVG flags, alongside the option names, providing a richer user experience.

The stage

The arrow sits at the end of the control. The list opens underneath.

The example

The button clones the selected option. The arrow is pushed to the end of the row.

<select>
  <button>
    <selectedcontent></selectedcontent>
  </button>
  <option value="ES">
    <svg viewBox="0 0 18 12">
      <rect width="18" height="3" fill="#aa151b"/>
      <rect y="3" width="18" height="6" fill="#f1bf00"/>
      <rect y="9" width="18" height="3" fill="#aa151b"/>
    </svg>
    <span>Spain</span>
  <option value="DE">
    <svg viewBox="0 0 18 12">
      <rect width="18" height="4" fill="#000"/>
      <rect y="4" width="18" height="4" fill="#dd0000"/>
      <rect y="8" width="18" height="4" fill="#ffce00"/>
    </svg>
    <span>Germany</span>
  </option>
</select>
.country,
.country::picker(select) {
  appearance: base-select;
}

.country {
  display: flex;
  align-items: center;
}

.country button {
  flex: 1;
}

.country selectedcontent,
.country option {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}

.country::picker-icon {
  margin-inline-start: auto;
}

.flag {
  width: 1.5rem;
  height: auto;
}

All Web API labs

  • CSS overflow carousel example
  • CSS Anchor Positioning example
  • CSS Custom Highlight API example