How to change the style of a <select>'s <optgroup> label?
Asked Answered
D

9

19

I have a simple select box with an option group in my application.

<select>
   <optgroup label="Swedish Cars">
     <option value="volvo">Volvo</option>
     <option value="saab">Saab</option>
   </optgroup>
   ----
   ----
   ----
</select>

When it gets displayed in browser, the option group label is displayed in bold and italic; I want it to be displayed without any of those styles.

Deutero answered 17/9, 2011 at 4:42 Comment(0)
L
13

Unfortunately select boxes are one of the few things that you can add very little style to with CSS. You are usually limited to how the browser renders it.

For example, it looks like this in chrome:

enter image description here

And this in Firefox:

enter image description here

Lundt answered 17/9, 2011 at 4:51 Comment(4)
So which means we cant override the style browser using for display optiongroup labels, in above screenshot Swedish cars and American cars?i wan to display those labels in Normal font weight for all browsers. I want to display that in normal font for all browsers.Deutero
Yeah, you cant do that with CSS, its part of the browser's chrome, unfortunately.Lundt
this is no longer the case, you should look at the latest answersFluoride
It is the case as of this writing; although, the display is not consistent across all browsers.Chrismatory
A
16

On most browsers (tested on latest IE and FF), you can easily change the optgroup's label with CSS only:

select optgroup{
    background: #000;
    color: #fff;
    font-style: normal;
    font-weight: normal;
}

Obviously, you can set any classname instead of the select html tag.

By the way, as other answers said, there are still few CSS options to use with select boxes and many webmasters override them using the method given by user949847. But this code above should be sufficient to match your needs.

Amontillado answered 19/7, 2012 at 8:19 Comment(3)
Working in IE10, FF21, Chrome28, and Opera12 as of this date. (Sorry, don't have Safari installed!) Note: On most of the mentioned browsers, the entire <optgroup> section is styled, not just the label.Chrismatory
If you apply styles to option tags as well as to optgroup tags, you can effectively control the presentation of the label without altering the standard presentation of the list items.Wyatan
FYI this is not working on Safari and Chrome under OSX. It still works on FF.Stereogram
L
13

Unfortunately select boxes are one of the few things that you can add very little style to with CSS. You are usually limited to how the browser renders it.

For example, it looks like this in chrome:

enter image description here

And this in Firefox:

enter image description here

Lundt answered 17/9, 2011 at 4:51 Comment(4)
So which means we cant override the style browser using for display optiongroup labels, in above screenshot Swedish cars and American cars?i wan to display those labels in Normal font weight for all browsers. I want to display that in normal font for all browsers.Deutero
Yeah, you cant do that with CSS, its part of the browser's chrome, unfortunately.Lundt
this is no longer the case, you should look at the latest answersFluoride
It is the case as of this writing; although, the display is not consistent across all browsers.Chrismatory
R
7

Firefox style the label using this rule :

optgroup:before {
    content: attr(label);
    display: block;
}

You can override it.

Rant answered 6/2, 2013 at 14:13 Comment(0)
U
3

For a different approach to circumvent the problems with styling optgroups i suggest using disabled options.

<option disabled>[group label]</option>

you can have a shot on styling it via eg.

<style> [disabled] { color:#000; background-color:#CCC } </style>
Unrelenting answered 8/11, 2013 at 10:2 Comment(1)
I noticed that this doesn't style on mobile devices. Is there a way to do that?Feil
G
0

You can style a select box using only css, it requires a sort of work around:

First, you surround it with a div and give that a class:

<div class="selectStyle">
    <select>
        <option>First Option</option>
        <option>Second Option</option>
    </select>
</div>

Then you make sure the select elements are styled a certain way using css:

.selectStyle select {
   background: transparent;
   width: 250px;
   padding: 4px;
   font-size: 1em;
   border: 1px solid #ddd;
   height: 25px;
}

And you style the div:

.selectStyle {
   width: 235px;
   height: 25px;
   overflow: hidden;
   background: url(yourArrow.png) no-repeat right #ccc;
}
Gomuti answered 17/9, 2011 at 5:3 Comment(3)
i want to style only optgroup LabelDeutero
Then you simply target the optgroup: optgroup[label="Swedish Cars"]Gomuti
Gnanz is talking about the label attribute. Being specific about the optgroup doesn't really address his problem.Trothplight
B
0

You can style the <optgroup> label only for Firefox like following

optgroup[label] {
   color: grey;
   font-style: inherit;
   font-weight: 300;
   text-shadow: none
}
Beal answered 28/1, 2020 at 10:1 Comment(0)
I
0

You can renounce to the label attribute and set instead a disabled <option> as first element within your <optgroup>, and then style this option.

<select>
  <option>1. One</option>
  <option>2. Two</option>
  <optgroup>
    <option disabled style="background:#dfb;color:#555;position:relative;top:-0.7em">3. Three</option>
    <option>3.1 Three dot one</option>
    <option>3.2 Three dot two</option>
  </optgroup>
  <option>4. Four</option>
</select>

additional tips:

If you don't need the <optgroup> in itself, a variant is just using the disabled option(s), which up to a certain extent will visually will emulate the optgroups by creating sections in your list.

For aesthetic reason, if you use the disabled option within an optgroup, I recommend slighly moving it upward with position:relative;top:-0.7em.

Incognito answered 27/1 at 18:51 Comment(0)
G
-2
<style>
.select2-container--bootstrap .select2-results__group {
    color: inherit;
    font-size: inherit;
    font-weight:bold;
    padding: 6px 4px;
}

Gastrostomy answered 27/7, 2018 at 15:8 Comment(2)
Please improve your answer by adding some explanation of your code.Consentient
This code does not do anything as is. It needs an explanation.Escaut
B
-3
.select2-results__group{
  /* put your style here */
}
Bosky answered 30/1, 2021 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.