255

How do you make a separator in a select tag?

New Window
New Tab
-----------
Save Page
------------
Exit
Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373
  • 1
    I usually add a disabled option with whitespace as the label. – Abhi Beckert Oct 08 '18 at 00:52
  • what most of the solutions here that employ `optgroup` are missing is the fact that a non-empty `label` attribute is *required* as per the html specification; therefore I would opt for one of the disabled option approaches – Christopher King Mar 17 '23 at 15:03

16 Answers16

413

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/

Alex K
  • 14,893
  • 4
  • 31
  • 32
  • 26
    The Unicode worked for me great in jsfiddle, but when I tried to copy/paste it into my code, it didn't properly translate. So for those with that problem, the HTML encoding for the horizontal unicode box drawing character is ─ http://www.fileformat.info/info/unicode/char/2500/index.htm and there is also a heavier option at ━ http://www.fileformat.info/info/unicode/char/2501/index.htm – ElJeffe Oct 10 '14 at 14:21
  • 2
    on Mobile Firefox (and possibly other mobile browsers) the `disabled` option is shown with a disabled radio button to the right... – GoTo Dec 08 '14 at 20:10
  • but optgroups are rendered differently on PC vs. mobile so the disabled option is still the best solution. – GoTo Dec 08 '14 at 20:18
  • 1
    The encoding of the html file is also vital for the "─" characters to show up as a line, instead of gibberish. UFT8 with BOM might be a good choice. – Andreas Jansson Dec 10 '14 at 15:19
  • 1
    Why does the optgroup solution sucks? It looks semantically more appropriate and it doesn't seem to have compatibility issues. – db0 Feb 07 '15 at 15:47
  • 1
    @db0 The appearance is inconsistent. http://grab.by/EzEi vs http://grab.by/EzEk (see the jsfiddle link). – Alex K Feb 09 '15 at 03:26
  • Worked like a charm! I ended up going with the first option actually - colgroup (the one you claimed sucked :)). I really like the indentation it gives you. Thanks for the very helpful answer! – Liran H Aug 25 '17 at 12:35
  • Personally I prefer either `""` or hyphens `--------------`. The anti-aliasing of the nice unicode character looks a bit thick to me on desktop and while it looks nice enough in mobile Safari to me I still feel it draws too much attention to itself. – Simon_Weaver Nov 26 '17 at 07:02
81

Try:

<optgroup label="----------"></optgroup>
Tina Orooji
  • 1,842
  • 3
  • 15
  • 14
  • 45
    Instead of putting a label on the optgroups, try adding this to your stylesheet: optgroup + optgroup { border-top: 1px solid black } Much less cheesy looking than a bunch of dashes. – Laurence Gonsalves May 22 '09 at 18:25
  • 2
    Optgroup labels should describe the group. If a browser implemented on as per the screenshot in the HTML 4.01 spec, then the user would be confronted with rows of dashes and would have to examine each one to find out what was behind it. – Quentin May 22 '09 at 18:41
  • 2
    @Laurence: IE7 does not support css styles on optgroup or option elements. At least not borders – grom Aug 20 '09 at 02:44
  • Cool. I had never heard of optgroup and always thought that the separators I see on many website forms are not standard html constructs. –  Nov 12 '10 at 21:33
  • 2
    -- looks cool at least in firefox! – pronebird Dec 16 '10 at 18:42
  • 1
    An empty optgroup like this is not legal html. You'll get validation errors if you use it. I prefer james.garriss's answer. – Ariel Sep 28 '11 at 11:58
  • @LaurenceGonsalves / Andy - Inconsistent results in different browsers. Andy's code looks great in FF, looks dungish in Chrome. I'd try IE but I don't want monitor all over the walls. – cssyphus Nov 14 '12 at 21:16
  • The optgroup with dashes in the label is nice visually, but I am afraid screenreaders will also read this to users, right? does anybody know a way to achieve this in an accessible way? – Julio Jan 10 '13 at 10:38
40

This is 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>&nbsp;</option>
  <option>Third</option>
  <option>Fourth</option>
  <option style="font-size: 1pt; background-color: #000000;" disabled>&nbsp;</option>
  <option>Fifth</option>
  <option>Sixth</option>
</select>
Dharman
  • 30,962
  • 25
  • 85
  • 135
Dexter Blake
  • 421
  • 5
  • 6
  • great ! I used min-height:1px; max-height:1px; padding:0; instead of font-size: 1pt; for a 1px line – kofifus Aug 25 '16 at 01:01
  • I tried using font-size as well as min-height/max-height/padding however am unable to change the height of the option item. Can anyone advise what may be the problem and/or how to adjust the option's height? – Green Jul 21 '23 at 19:26
17

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>
james.garriss
  • 12,959
  • 7
  • 83
  • 96
  • 3
    Hi James, I like this solution too but screenreaders will read "disable option dash dash dash dash..." which may be very confusing for disabled users... do you have an idea of how we could avoid this? thank you! – Julio Jan 10 '13 at 10:39
  • 1
    Is it possible to add a class to visual elements that you don't want spoken, then add an ARIA command that hides/disables the class? Perhaps something like one of the techniques used here? http://asurkov.blogspot.com/2012/02/aria-hidden-and-rolepresentation.html – james.garriss Jan 10 '13 at 11:27
  • Actually, @Julio, you should post this as a new question. I'm curious to know, but I've never programmed for screenreaders before. – james.garriss Jan 10 '13 at 11:31
10

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>
Klaus Heyne
  • 251
  • 3
  • 5
  • 2
    Answer could be improved with a jsfiddle example. This seems like the most built-in solution not relying on side effects or hacks, but would be nice to compare visuals of other answers. – raider33 Feb 24 '15 at 11:40
  • Doesn't work in many browsers. (as usual, inputs being a pain in the butt) – Forty Mar 13 '18 at 21:08
  • It doesn't work on Chrome. It seems that the border doesn't work in the options at all in Chrome. – Luis Rodriguez Mar 01 '22 at 17:12
10

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>
user511941
  • 159
  • 1
  • 3
7

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.

Noumenon
  • 5,099
  • 4
  • 53
  • 73
  • 3
    This is actually the right way to do it (importantly, it does not add meaningless content). I like to style it this way (adding some extra vertical space around the separator): `optgroup { padding-bottom: 8px; } optgroup:not(:first-child) { padding-top: 8px; border-top: solid 1px #666; }` – Nicolas Le Thierry d'Ennequin Feb 07 '17 at 13:41
6

If it's WebKit-only, you can use <hr> to create a real separator.

http://code.google.com/p/chromium/issues/detail?id=99534

reformed
  • 4,505
  • 11
  • 62
  • 88
Jon
  • 69
  • 1
  • 3
6

To build upon Jasneet's answer:

  • Style the background of a disabled option as gray (Jasneet)
  • Add disabled options above and below to pad the separator

Basically, use three disabled options to make one naturally-styled separator. I have selected a very thin gray line of 0.1px and pad heights of 0.25em because I think this combination looks the most natural:

Separator inside HTML select list

<select>
  <option :value="item1">Item 1</option>
  <option disabled style="font-size: 0.25em;"></option>
  <option disabled style="background: #c9c9c9; font-size: 0.1px;"></option>
  <option disabled style="font-size: 0.25em;"></option>
  <option :value="item2">Item 2</option>
</select>
mikeypie
  • 129
  • 1
  • 4
4

another way is to use a css 1x1 background image on option which only seems to work with firefox and have a "----" fallback

<option value="" disabled="disabled" class="SelectSeparator">----</option> 

.SelectSeparator
    {
      background-image:  url(data:image/gif;base64,R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==);
      color:black;
      background-repeat:repeat-x;
      background-position:50% 50%;
      background-attachment:scroll;
}

http://jsfiddle.net/yNecQ/6/

or to use javascript (jquery) to:

-hide the select element and 
-show a div which can be completely styled and 
-reflect the div state onto the select for the form submit

http://tutorialzine.com/2010/11/better-select-jquery-css3/


see also How do I add a horizontal line in a html select control?

Community
  • 1
  • 1
ebricca
  • 386
  • 3
  • 5
3

we can make use of optgroup tag without options

  • can set the font-size:1px to minimize the height, and
  • some pretty background for it

.divider {
  font-size: 1px;
  background: rgba(0, 0, 0, 0.5);
}

.divider--danger {
  background: red;
}
<select>
  <option value="option1">option 1 key data</option>
  <option value="option2">option 2 key data</option>
  <optgroup class="divider"></optgroup>
  <option value="option3">option 3 key data</option>
  <option value="option4">option 4 key data</option>
</select>

<select>
  <option value="option1">option 1 key data</option>
  <option value="option2">option 2 key data</option>
  <optgroup class="divider divider--danger"></optgroup>
  <option value="option3">option 3 key data</option>
  <option value="option4">option 4 key data</option>
</select>

Codepen.io: https://codepen.io/JasneetDua/pen/yLOYwaV?editors=1100

Jasneet Dua
  • 3,796
  • 2
  • 10
  • 15
2

This one is best always.

<option>First</option>
<option disabled>_________</option>
<option>Second</option>
<option>Third</option>

Tejashree
  • 750
  • 12
  • 14
1

You could use the em dash "—". It has no visible spaces between each character.
(In some fonts!)

In HTML:

<option value="—————————————" disabled>—————————————</option>

Or in XHTML:

<option value="—————————————" disabled="disabled">—————————————</option>
Tobi
  • 133
  • 1
  • 7
  • 1
    Spacing between characters is determined by the font and rendering engine. E.g. I see spaces between your em dashes on Chrome (Windows) but not on Safari (Mac). – Hank May 01 '13 at 17:51
1

I elected to conditionally alternate color and background. Setting a sort order and with vue.js, I did something like this:

<style>
    .altgroup_1 {background:gray; color:white;}
    .altgroup_2{background:white; color:black;}
</style>

<option :class = {
    'altgroup_1': (country.sort_order > 25),
    'altgroup_2': (country.sort_order > 50 }"
    value="{{ country.iso_short }}">
    {{ country.short_name }}
</option
STWilson
  • 1,538
  • 2
  • 16
  • 26
1
 <option  data-divider="true" disabled>______________</option>

you can do this one also. it is easy and make divider select drop down list.

DSK
  • 492
  • 5
  • 13
0

this will solve your problem:

<select>
    <optgroup>
        <option>New Window</option>
        <option>New Tab</option>
    </optgroup>
    <optgroup label="_________">
        <option>Save Page</option>
    </optgroup>
    <optgroup label="_________">
        <option>Exit</option>
    </optgroup>
</select>