How to change the style of a <select>'s <optgroup> label? - html

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.

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.

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:
And this in Firefox:

Firefox style the label using this rule :
optgroup:before {
content: attr(label);
display: block;
}
You can override it.

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;
}

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>

<style>
.select2-container--bootstrap .select2-results__group {
color: inherit;
font-size: inherit;
font-weight:bold;
padding: 6px 4px;
}

You can style the <optgroup> label only for Firefox like following
optgroup[label] {
color: grey;
font-style: inherit;
font-weight: 300;
text-shadow: none
}

.select2-results__group{
/* put your style here */
}

Related

How to bold <optgroup> label for mac under Chrome Version 62.0.3202.94? [duplicate]

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.
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.
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:
And this in Firefox:
Firefox style the label using this rule :
optgroup:before {
content: attr(label);
display: block;
}
You can override it.
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;
}
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>
<style>
.select2-container--bootstrap .select2-results__group {
color: inherit;
font-size: inherit;
font-weight:bold;
padding: 6px 4px;
}
You can style the <optgroup> label only for Firefox like following
optgroup[label] {
color: grey;
font-style: inherit;
font-weight: 300;
text-shadow: none
}
.select2-results__group{
/* put your style here */
}

How to style optgroup labels and options? [duplicate]

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.
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.
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:
And this in Firefox:
Firefox style the label using this rule :
optgroup:before {
content: attr(label);
display: block;
}
You can override it.
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;
}
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>
<style>
.select2-container--bootstrap .select2-results__group {
color: inherit;
font-size: inherit;
font-weight:bold;
padding: 6px 4px;
}
You can style the <optgroup> label only for Firefox like following
optgroup[label] {
color: grey;
font-style: inherit;
font-weight: 300;
text-shadow: none
}
.select2-results__group{
/* put your style here */
}

Is there a way to select an open select box in css?

I set a background image (arrow down) to a select box after setting the webkit-appearance attribute to none. When the option list is opened I want to display another background image (arrow up). Is there a pseudo class or something for it? I couldn't find anything during my research...
you can use the :focus pseudo class.
But this will indicate the "open" state, also when the <select> is selected via tab or an item was just selected. To circumvent this, you maybe use something like select:hover:focus but this is rather ugly and is not a solid solution.
select:focus {
background-color: blue;
color: white;
}
<select>
<option>Click me</option>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
It's not possible to detect if a HTML select element is opened using CSS, or even javascript.
If you wish to customise the arrow symbol of a custom dropdown, your best option is to use a custom dropdown component which maps to a hidden select element.
This post is old, but as today; the ::before and ::after pseudo-class still don't work on select element.
Here's a good opportunity to use the :focus-within pseudo-class. I made this simple codepen demonstrating the use of it.
The codepen
The snippet :
#charset "UTF-8";
.wrapper {
position: relative;
display: inline-block;
}
.wrapper::after {
content: "";
font-family: "Font Awesome 5 Free";
font-weight: 900;
position: absolute;
right: 16px;
top: 22px;
font-size: 18px;
pointer-events: none;
transition: 0.2s ease;
}
.wrapper:focus-within::after {
transform: rotateX(0.5turn) translateY(-2px);
}
.wrapper select {
background: linear-gradient(180deg, #F8F9FB 0%, #F1F2F4 100%);
border: 1px solid var(--grayscale-400);
box-sizing: border-box;
border-radius: 3px;
width: 200px;
left: 2px;
cursor: pointer;
padding: 24px 12px;
font-size: 16px;
appearance: none;
}
.wrapper select:focus {
outline: unset;
}
.wrapper select > option:first-child {
display: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet"/>
<div class="wrapper">
<select>
<option>Choice Label First</option>
<option value="Choice 1">Choice 1</option>
<option value="Choice 2">Choice 2</option>
<option value="Choice 3">Choice 3</option>
<option value="Choice 4">Choice 4</option>
</select>
</div>

IE Select Box option element height not working

select item option lists are not showing properly in IE.
need to add padding and height for option element. Does any one knows how to fix this?
CSS
.reser_item_long select {
width: 106%;
width: 106.5%\9;
height: 33px;
*height: 33px;
float: left;
text-align: left;
border: none;
outline: none;
background: none;
cursor: pointer;
overflow: hidden;
text-transform: uppercase;
font-size: 10px;
color: #889099;
color: #889099\9 !important;
padding: 9px 0 0 7px;
padding: 0 0 0 7px\9 ;
padding: 0 0 0 7px\0/;
display:block;
(-bracket-:hack;
padding: 2px 0 0 7px ;
);
}
HTML
<div class="reser_item_long">
<div class="select-wrapper">
<select id="cmb-country" name="cmb-country">
<option value="-1">Please Select a Country</option>
<option value="0">Afghanistan</option>
<option value="0">Åland Islands</option>
<option value="0">Albania</option>
<option value="0">Algeria</option>
</select>
</div>
</div>
There is no work-around for this aside from ditching the select element
You should differently edit this question and add you browser version. But let me give you a solution after all.
The problem is that the Select, options, file inputs are rendered by the OS/Browser and not by the HTML itself and therefor give's problem in styling it.
You can via javascript simulate and style your own dropdown to make sure it looks the same way in every browser and OS. Basically it generates something that look and feels like a dropdown list but is just an interface for the hidden select in your code.
You could use something like http://formalize.me/ to style all forms elements on your page.

Option in italics

Is there any cross-browser way to italicize select options?
With the following CSS and HTML, FireFox shows the second option in italics, but not the third.
None of the options are italicized in IE 7 or Safari.
<style>
option.bravo
{
font-style: italic;
}
</style>
<select>
<option>Alpha</option>
<option class="bravo">Bravo</option>
<option><i>Charlie</i></option>
<select>
Am I correct in assuming this is not possible?
This page has a nice chart of browser support of styling select, option and optgroup: http://www.electrictoolbox.com/style-select-optgroup-options-css/
According to that, the like the only option style that's cross-browser is color.
#MikeWWyatt
I know it's been a while since you asked, but I created this:
Wrap in a paragraph that is position:relative
Create a element that is position absolute
Have your option be the same color as your select background color
Use jQuery to change the option color to white (or whatever color you want) using .change() as well as hide the
HTML
<p>I am a: <span class='pretend-option'>Please choose one</span>
<select name="example">
<option disabled="disabled" selected="selected">Please choose one</option>
<option value="consumer">Consumer</option>
<option value="supplier">Supplier</option>
<option value="retailer">Retailer</option>
</select>
</p>
jQuery
$('select').change(function () {
$(this).css('color', 'white');
$(this).parent('p').children('.pretend-option').css('z-index', -1);
});
CSS
select, option {
-webkit-appearance: none;
-webkit-border-radius: 0px;
border-radius: 0;
display: block;
}
p {
position: relative;
}
select {
padding: 7px;
background-color: blue;
color: blue;
}
.pretend-option {
position: absolute;
bottom: 0.5em;
color: #fff;
left: 0.5em;
pointer-events: none;
z-index: 1;
font-style: italic;
}
Here's a fiddle if you're interested: https://jsfiddle.net/ej34bea0/