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.
You can use <optgroup> to create a single level of nesting... Note that the group labels are not selectable options. In that case, I would recommend using the text-indent solution that is mentioned in the top answer to the question that home linked to in his comment.
You can't format line breaks into an option; however, you can use the title attibute to give a mouse-over tooltip to give the full info. Use a short descriptor in the option text and give the full skinny in the title.
The <optgroup> HTML element creates a grouping of options within a <select> element.
The disabled option approach seems to look the best and be the best supported. I've also included an example of using the optgroup.
optgroup (this way kinda sucks):
<select>
<optgroup>
<option>First</option>
</optgroup>
<optgroup label="_________">
<option>Second</option>
<option>Third</option>
</optgroup>
</select>
disabled option (a bit better):
<select>
<option>First</option>
<option disabled>_________</option>
<option>Second</option>
<option>Third</option>
</select>
And if you want to be really fancy, use the horizontal unicode box drawing character.
(BEST OPTION!)
<select>
<option>First</option>
<option disabled>──────────</option>
<option>Second</option>
<option>Third</option>
</select>
http://jsfiddle.net/JFDgH/2/
Try:
<optgroup label="----------"></optgroup>
This is an old thread, but since no one posted a similar response, I'll add this as it's my preferred way of separation.
I find using dashes and such to be somewhat of an eyesore since it could fall short of the width of the selection box. So, I prefer to use CSS to create my separators.. a simple background coloring.
<select>
<option style="background-color: #cccccc;" disabled selected>Select An Option</option>
<option>First Option</option>
<option>Second</option>
<option style="font-size: 1pt; background-color: #000000;" disabled> </option>
<option>Third</option>
<option>Fourth</option>
<option style="font-size: 1pt; background-color: #000000;" disabled> </option>
<option>Fifth</option>
<option>Sixth</option>
</select>
If you don't want to use the optgroup element, put the dashes in an option element instead and give it the disabled attribute. It will be visible, but greyed out.
<option disabled>----------</option>
Instead of the regular hyphon I replaced it using a horizontal bar symbol from the extended character set, it won't look very nice if the user is in another country that replaces that character but works fine for me. There is a range of different chacters you could use for some great effects and there is no css involved.
<option value='-' disabled>――――</option>
Define a class in CSS:
option.separator {
margin-top:8px;
border-top:1px solid #666;
padding:0;
}
Write in HTML:
<select ...>
<option disabled class="separator"></option>
</select>
I'm making @Laurence Gonsalves' comment into an answer because it's the only one that works semantically and doesn't look like a hack.
Try adding this to your stylesheet:
optgroup + optgroup { border-top: 1px solid black }
Much less cheesy looking than a bunch of dashes.
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