Is it possible to style just the label of the optgroup in drop-down selector?
<select>
<optgroup label="Group Name">
<option>My option</option>
</optgroup>
</select>
optgroup { font-style: normal|italic|oblique; } styles the optgroup label and options within the optgroup. option { font-style: normal|italic|oblique; } has no effect. select { font-weight: normal; } has no effect but select { font-weight: bold; } will make all options in the select box bold.
The <optgroup> tag is used to group related options in a <select> element (drop-down list). If you have a long list of options, groups of related options are easier to handle for a user.
Description. The HTML <optgroup> tag creates a group of options within a dropdown list of values in the <select> control. These dropdown values are defined by a series of <option> tags and grouped using the <optgroup> tag.
Use attribute Selector
[label]
{
color: red;
}
EDIT
<select>
<optgroup label="Cars">
<option>Honda</option>
<option>Merc</option>
</optgroup>
<optgroup label="Bikes">
<option>Kawasaki</option>
<option>Yamaha</option>
</optgroup>
</select>
optgroup[label="Cars"]
{
color: red;
}
optgroup[label="Bikes"]
{
color: blue;
}
option
{
color: black;
}
I know that this thread is a bit old, but just in case someone stumbles on it as I have... It has been mentioned that when styling the optgroup tag that it styles the entire group. This is true, but if you style the option tag separately, the optgroup formatting will remain as set in the style, but the style formatting for the option which is a child of the optgroup will override the optgroup formatting that is inherited when no style is set for the option.
So, setting the following css style for optgroup will give you red text on a gray background.
optgroup {
background-color: gray;
color: red;
}
If nothing else is done, your entire list will be red text on a gray background. However, also setting the option style as below will keep the optgroup labels styled red on gray, but the options under the group will be black on white.
option {
background-color: white;
color: black;
}
Using the [label]
attribute selector as suggested everywhere is mostly irrelevant. You cannot apply styles to HTML attributes. It's merely a modifier to filter out optgroup
elements. If all your optgroup
have a label
then you'll be selecting them all anyway and if some do not have label
then they'll just opt-out from the style:
optgroup[label]{
color: red;
}
<select size="8">
<optgroup label="Cars">
<option>Honda</option>
<option>Merc</option>
</optgroup>
<optgroup>
<option>Not label</option>
<option>Not label either</option>
</optgroup>
<optgroup label="Bikes">
<option>Kawasaki</option>
<option>Yamaha</option>
</optgroup>
</select>
The <select>
tag has the problem of other complex form controls like <input type="file">
. Original specs assumed that it'd be up to the browser how to display it (in fact, it was typically a builtin GUI control provided by the underlying library) and don't provide comprehensive means to style it. Just look at this screen-shot found in the W3C HTML 4.01 Specification:
Back to our problem, there're two facts to take into account:
<optgroup>
is a block that includes child <option>
sThis snippet makes it more clear:
optgroup{
margin: 1em;
border: 1px solid green;
}
optgroup option{
margin: 1em;
border: 1px solid orange;
}
<select size="13">
<optgroup label="Cars">
<option>Honda</option>
<option>Merc</option>
</optgroup>
<optgroup label="Bikes">
<option>Kawasaki</option>
<option>Yamaha</option>
</optgroup>
</select>
So all you can do is to apply styles to <optgroup>
(understanding you can't tell the label apart from the rest of items) and then overwrite them as necessary for child options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With