How do I add a horizontal line (<hr>
tag) in the dropdown control or in select control in HTML?
-
2why do you want to do this and how is this jquery related? – crodjer Nov 30 '10 at 18:18
-
3Possible duplicate of [html select option separator](http://stackoverflow.com/questions/899148/html-select-option-separator) – Ilya Serbis Jan 26 '16 at 16:39
-
You can also use "optgroups" to kind of achieve a similar concept, see http://stackoverflow.com/questions/899148/html-select-option-separator – rogerdpack Oct 12 '16 at 15:49
10 Answers
Generally speaking this is not a feature of <select>
, most browsers have very poor styling control for that control. In Firefox you can do the following (though doesn't work in other browsers):
<select name="test">
<option val="a">A</option>
<option val="b" class="select-hr">B</option>
<option val="c">C</option>
<option val="d">D</option>
</select>
with CSS:
option.select-hr { border-bottom: 1px dotted #000; }
But generally speaking, the only method is to put an option in with dashes, and try to make it unselectable.
<select name="test">
<option val="a">A</option>
<option val="b">B</option>
<option disabled="disabled">----</option>
<option val="c">C</option>
<option val="d">D</option>
</select>

- 62,887
- 36
- 269
- 388

- 20,413
- 3
- 53
- 64
-
1The disabled seperator - Nice touch! But this might create issues when doing any operations using `selectedIndex`. – Varun Natraaj Nov 27 '13 at 17:28
-
@VarunNatraaj: Yes, I've never found the need to use `selectedIndex` myself - but you would need to take account of elements with `.disabled` during any counts, calculations, movements of that index. – Orbling Nov 28 '13 at 18:38
-
Seems even the firefox example doesn't work in firefox these days :) – rogerdpack Oct 12 '16 at 15:45
-
The use of `optgroup` would allow you to avoid the issue with selectedIndex, as well as automatically being disabled. – Chris - Jr Dec 01 '17 at 18:36
-
@Chris-Jr Note the year, `optgroup` support wasn't as universal as it is now. – Orbling Dec 11 '17 at 11:56
I've run into this issue before and the only cross-browser way to achieve this is to use unicode box drawing horizontal characters. Browsers don't render spaces between these characters. Of course that also means that your page must accept unicode (utf-8) which is almost a given these days.
Here is a demo, this one using a "light horizontal" box mark:
<option value="" disabled="disabled">─────────────────────────</option>
There are various unicode box character options you can use as separator which should be rendered without spaces between them:
"─", "━", "┄", "┅", "┈", "┉"
More about unicode box drawing characters here: http://www.duxburysystems.com/documentation/dbt11.2/miscellaneous/Special_Characters/Unicode_25xx.htm
You can also include them using HTML escape chars (copy and paste usually works by inserting the box characters' UTF-8 sequence, which browsers seem to respect, but if that's not an option), this for four light horizontal box marks in a row:
────
which renders as
────

- 62,887
- 36
- 269
- 388

- 404
- 4
- 4
-
Great! Your variant of separator character is looking awesome in IE! Also i suggest to use optgroup label instead of option because you will be able change color of optgroup label, but not in disabled option (IE). – Aleko Nov 17 '14 at 16:41
The select
element may only contain optgroup
or option
elements, and option
elements may only contain text. Markup like <hr>
is forbidden.
I would use an element like this to create a separator:
<option disabled role=separator>
You may consider markup like this:
<optgroup label="-------"></optgroup>
However, the rendering between different browsers varies a little too much to be acceptable.

- 62,887
- 36
- 269
- 388

- 171,072
- 38
- 269
- 275
-
-
seperator role is a good one, glad to know there are alternatives! – Mustafa Erdogan Dec 31 '18 at 12:19
You could use the em dash "—". It has no visible spaces between each character.
In HTML:
<option value disabled>—————————————</option>
Or in XHTML:
<option value="" disabled="disabled">—————————————</option>

- 10,623
- 4
- 56
- 80

- 133
- 1
- 7
-
I'm not sure if it was due to other css on the page, but I found that there are visible spaces between em dash in drop downs on Firefox and Chrome on OS X. http://jsfiddle.net/PhZPh/ – radicaledward101 Apr 05 '13 at 15:25
-
7
-
You can also use its html escape char — (though copy and paste of that char seems to work as well) – rogerdpack Oct 12 '16 at 15:37
<option>----------</option>
or
<option>__________</option>
but you can't write <option><hr /></option>
you could also add a css class to an empty <option>
with a background image
<option class="divider"> </option>

- 62,308
- 19
- 113
- 113
-
+1 for using a background image. Good idea. But does it work? :) – Bennett McElwee Oct 27 '11 at 22:48
-
2I just tried adding a style attribute with both background colour and image, neither seem to work in Safari 7 sadly. – Peter Bagnall Nov 13 '13 at 12:24
In the HTML Standard an <hr>
is now allowed in the <select>
element.
It seems Safari 17 will be the first to implement it.
<select>
<option>option 1</option>
<option>option 2</option>
<hr>
<option>option 3</option>
<option>option 4</option>
</select>
Note
Your browser will currently not display it correctly. Some browsers however support an <hr>
that is added via script:
const selectEl = document.querySelector('select');
selectEl.insertBefore(document.createElement('HR'), selectEl.childNodes[4]);
<select>
<option>option 1</option>
<option>option 2</option>
<option>option 3</option>
<option>option 4</option>
</select>
Both Safari and Chrome on MacOs render it correctly with the last snippet. Firefox however does not. (July 2023)

- 6,247
- 1
- 30
- 45
This is an old post, but I ran into a lot of posts in which no one even bother to find a elegant solution, although it is simple.
Everyone is trying do ADD something into between <option>
tags, but no one thought about STYLING the <option>
tag, which is the only way to do it and to look amazing.
To easy to be true ? Look
<select>
<option>First option</option>
<option>Second option</option>
<option>Third option</option>
<option style="border-bottom:1px solid rgba(153,153,153,.3); margin:-10px 0 4px 0" disabled="disabled"></option>
<option>Another option</option>
<option style="border-bottom:1px solid rgba(153,153,153,.3); margin:-10px 0 4px 0" disabled="disabled"></option>
<option>Something else</option>
</select>
I've placed the style with the html for ease.
This doesn't work anymore, it's clear, don't know why people keep posting answers. The rules changed, this solution worked, I've been using it myself. Nowadays I'm using jQuery plugins for this.
Lates edit: Everyone can use the amazing selectpicker plugin

- 1,300
- 10
- 12
-
1It does, I'm using it. Maybe you should bring some arguments along the way. – Lucian Minea Jun 22 '17 at 07:17
-
1Please read https://developer.apple.com/documentation/appkit/nsmenu?language=objc and try it for yourself in Safari 10.1 – Nicholas Shanks Jun 22 '17 at 14:03
-
Doesn't work in Chrome 66, Edge, Opera, anything mobile. I've only ever seen this work in Firefox. But I applaud your arrogance. Bravo. – Jamie Carl May 11 '18 at 06:34
I used JSP but you will see that is same. I am iterating through a brandMap LinkedHashMap where if key >= 50000, then it is a line separator, else is a normal select option. (I need that in my project)
<c:forEach items="${brandMap}" var="entry">
<c:if test="${entry.key ge 50000}">
<option value=${entry.key} disabled>––––</option>
</c:if>
<c:if test="${entry.key lt 50000}">
<option value=${entry.key}>${entry.value}</option>
</c:if>

- 3,675
- 3
- 34
- 37
If you want to insert a seperator of sorts into a tag, you can do something like this: http://jsfiddle.net/k4emic/4CfEp/
However, this isn't recommended practice.

- 411
- 2
- 11
Simple Way :
<h2 style="
font-family:Agency FB;
color: #006666;
table-layout:fixed;
border-right:1px solid #006666;
border-right-width:400px;
border-left:1px solid #006666;
border-left-width:400px;
line-height:2px;"
align="center"> Products </h2>