Line Break In HTML Select Option? - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Line Break in HTML Select Option? Ask Question Asked 14 years, 6 months ago Modified 2 years, 1 month ago Viewed 168k times 75

Can I have a two line text in an html select option? How?

Share Improve this question Follow edited Dec 12, 2014 at 14:36 rtruszk's user avatar rtruszk 3,92213 gold badges39 silver badges53 bronze badges asked May 19, 2010 at 9:24 Frank Michael Kraft's user avatar Frank Michael KraftFrank Michael Kraft 2,3625 gold badges25 silver badges30 bronze badges Add a comment |

14 Answers 14

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 63

I know this is an older post, but I'm leaving this for whomever else comes looking in the future.

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.

<select> <option title="This is my lengthy explanation of what this selection really means, so since you only see 1 on the drop down list you really know that you're opting to elect me as King of Willywarts! Always be sure to read the fine print!" value="1">1</option> </select>

Share Improve this answer Follow edited Oct 9, 2022 at 23:31 Dai's user avatar Dai 154k30 gold badges294 silver badges418 bronze badges answered Jul 8, 2012 at 16:08 jcanker's user avatar jcankerjcanker 8978 silver badges8 bronze badges 2
  • It doesn't work on Chrome and Safari. I could only get it to work on Firefox.user5832543 Commented Jan 9, 2020 at 15:32
  • It doesn't work on Chrome 104. – jMike Commented Aug 24, 2022 at 10:01
Add a comment | 42

How about putting the long text in another <option> right below and disabling it? Might be a workaround for someone so posting here.

<select> <option>My real option text</option> <option disabled style="font-style:italic">&nbsp;&nbsp;&nbsp;(...and my really really long option text that is basically a continuation of previous option)</option> <option disabled style="font-style:italic">&nbsp;&nbsp;&nbsp;(...and still continuing my previous option)</option> <option>Another real option text</option> </select>

Share Improve this answer Follow edited Jun 28, 2016 at 20:44 answered Jun 24, 2016 at 21:36 joym8's user avatar joym8joym8 4,1923 gold badges54 silver badges99 bronze badges 1
  • It's mostly a workaround, but for me needs it did the trick. I just added font-size:0.8em to the style to make clear that it is a description for the option above. – kinda-wired Commented Dec 27, 2022 at 6:52
Add a comment | 37

No, browsers don't provide this formatting option.

You could probably fake it with some checkboxes with <label>s, and JS to turn it into a fly out menu.

Share Improve this answer Follow answered May 19, 2010 at 9:25 Quentin's user avatar QuentinQuentin 941k132 gold badges1.3k silver badges1.4k bronze badges 0 Add a comment | 18

A bit of a hack, but this gives the effect of a multi-line select, puts in a gray bgcolor for your multi line, and if you select any of the gray text, it selects the first of the grouping. Kinda clever I'd say :) The first option also shows how you can put a title tag in for an option as well.

function SelectFirst(SelVal) { var arrSelVal = SelVal.split(",") if (arrSelVal.length > 1) { Valuetoselect = arrSelVal[0]; document.getElementById("select1").value = Valuetoselect; } } <select name="select1" id="select1" onchange="SelectFirst(this.value)"> <option value="1" title="this is my long title for the yes option">Yes</option> <option value="2">No</option> <option value="2,1" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is my description for the no option</option> <option value="2,2" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is line 2 for the no option</option> <option value="3">Maybe</option> <option value="3,1" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is my description for Maybe option</option> <option value="3,2" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is line 2 for the Maybe option</option> <option value="3,3" style="background:#eeeeee">&nbsp;&nbsp;&nbsp;This is line 3 for the Maybe option</option> </select>

Share Improve this answer Follow edited Jan 20, 2016 at 7:09 brasofilo's user avatar brasofilo 26k15 gold badges93 silver badges184 bronze badges answered Nov 18, 2014 at 21:01 brian's user avatar brianbrian 1811 silver badge2 bronze badges 1
  • Doesn't look as good on iOS/Android, but clever indeed! (might be able to add styling to achieve a better effect on mobile). – Lindsay-Needs-Sleep Commented May 9, 2020 at 4:37
Add a comment | 10

HTML Code

<section style="background-color:rgb(237.247.249);"> <h2>Test of select menu (SelectboxIt plugin)</h2> <select name="select_this" id="testselectset"> <option value="01">Option 1</option> <option value="02">Option 2</option> <option value="03">Option 3</option> <option value="04">Option 4</option> <option value="05">Option 5</option> <option value="06">Option 6</option> <option value="07">Option 7 with a really, really long text line that we shall use in order to test the wrapping of text within an option or optgroup</option> <option value="08">Option 8</option> <option value="09">Option 9</option> <option value="10">Option 10</option> </select> </section>

Javascript Code

$(function(){ $("#testselectset").selectBoxIt({ theme: "default", defaultText: "Make a selection...", autoWidth: false }); });

CSS Code

.selectboxit-container .selectboxit, .selectboxit-container .selectboxit-options { width: 400px; /* Width of the dropdown button */ border-radius:0; max-height:100px; } .selectboxit-options .selectboxit-option .selectboxit-option-anchor { white-space: normal; min-height: 30px; height: auto; }

and you have to add some Jquery Library select Box Jquery CSS

Jquery Ui Min JS

SelectBox Js

Please check this link JsFiddle Link

Share Improve this answer Follow edited Jan 14, 2019 at 13:28 Joakim Johansson's user avatar Joakim Johansson 3,1941 gold badge28 silver badges43 bronze badges answered Apr 6, 2015 at 12:07 Arjun's user avatar ArjunArjun 8231 gold badge8 silver badges18 bronze badges 1
  • 1 That's awesome, I searched it a long time – niz_sh Commented May 24, 2019 at 7:52
Add a comment | 6

If you're using select2 you can easily build something to fit the dropdown options and selected option likw below:

When your text option is splitted by |

<option value="1">Pacheco|Fmcia Pacheco|11443322551</option>

Then your script could be just like this one:

<script type="text/javascript"> function formatSearch(item) { var selectionText = item.text.split("|"); var $returnString = $('<span>' + selectionText[0] + '</br><b>' + selectionText[1] + '</b></br>' + selectionText[2] +'</span>'); return $returnString; }; function formatSelected(item) { var selectionText = item.text.split("|"); var $returnString = $('<span>' + selectionText[0].substring(0, 21) +'</span>'); return $returnString; }; $('.select2').select2({ templateResult: formatSearch, templateSelection: formatSelected }); </script>

The result you can see below:

enter image description here

Share Improve this answer Follow answered Feb 1, 2020 at 21:53 Fábio Araújo's user avatar Fábio AraújoFábio Araújo 4958 silver badges11 bronze badges 3
  • 1 Good answer! Mandou bem, fábio! 👍🏻 – David Toledo Commented Jul 26, 2021 at 16:55
  • @Fábio Great answer. If I am using select2 and have my select box with data setup like you've shown. How exactly do I use your script to make it work with my select box? – user1609391 Commented Jan 3, 2022 at 5:41
  • Hi @user1609391, you have to assign your element as a select2 element and define the templateResult and templateSelection. You have it on the "Then your script could be just like this one:" section in the answer above. – Fábio Araújo Commented Jan 4, 2022 at 12:06
Add a comment | 3

Just style the select control like this:

<select style="height: 50px; white-space: pre-wrap;">

Result:

enter image description here

Share Improve this answer Follow answered May 25, 2021 at 20:43 Renato Mestre's user avatar Renato MestreRenato Mestre 687 bronze badges 1
  • This works on the selected option, but not on the listed options (i.e., that same option, in the dropdown list, will be truncated at "(o perc" or somewhere near there. – Heretic Monkey Commented Nov 29, 2021 at 22:36
Add a comment | 2

you can use normal input element with list attribute, and then add options with value, the value will be displayed to as a first line and the option text will be displayed as second line automatically. I found this solution, have a look, this way may also help

<label class="m-t-20">Customer List</label> <input name="customerlist" class="customerlist form-control" list="customerlists" > <datalist id="customerlists"> <option value="Gerda Weijers">Aw Trucks Limited </option> <option value="Tracy Lolesi">Acorn Stoneworx Limited </option> <option value="Pacheco">Pacheco Fmcia Pacheco</option> </datalist>

but this only works in Chrome, other browsers don't show value text.

Share Improve this answer Follow edited Apr 20, 2021 at 7:25 answered Apr 13, 2021 at 5:56 SAW Kk's user avatar SAW KkSAW Kk 749 bronze badges Add a comment | 1

Does not work fully (the hr line part) on all browsers, but here is the solution:

<select name="selector"> <option value="1">Option 1</option> <option value="2">Option 2</option> <option value="3">Option 3</option> <option disabled><hr></option> <option value="4">Option 4</option> <option value="5">Option 5</option> <option value="6">Option 6</option> </select>

Share Improve this answer Follow edited Dec 26, 2016 at 22:55 answered Dec 26, 2016 at 22:49 Justin Levene's user avatar Justin LeveneJustin Levene 1,66719 silver badges19 bronze badges Add a comment | 1

Instead, maybe try replacing the select with markup, e.g.

// Wrap any selects that should be replaced $('select.replace').wrap('<div class="select-replacement-wrapper"></div>'); // Replace placeholder text with select's initial value $('.select-replacement-wrapper').each(function() { $(this).prepend('<a>' + $('select option:selected', this).text() + '</a>'); }); // Update placeholder text with updated select value $('select.replace').change(function(event) { $(this).siblings('a').text( $('option:selected', this).text() ); }); /* Position relative, so that we can overlay the hidden select */ .select-replacement-wrapper { position: relative; border: 3px solid red; /* to show area */ width: 33%; margin: 0 auto; } /* Only shows if JS is enabled */ .select-replacement-wrapper a { /* display: none; */ /* Notice that we've centered this text - you can do whatever you want, mulitple lines wrap, etc, since this is not a select element' */ text-align: center; display: block; border: 1px solid blue; } select.replace { position: absolute; width: 100%; height: 100%; top: 0; border: 1px solid green; opacity: 0; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <select id="dropdown" class="replace"> <option value="First">First</option> <option value="Second" selected>Second, and this is a long line, just to show wrapping</option> <option value="Third">Third</option> </select>

Share Improve this answer Follow answered Oct 17, 2017 at 18:06 RogerRoger's user avatar RogerRogerRogerRoger 3963 silver badges9 bronze badges 1
  • This is a very clever solution. – designosis Commented Oct 7, 2020 at 23:56
Add a comment | 0

Ok i found a solution:

HTML:

<div class="styled-select"> <select class="select-css"> <option disabled selected></option> <option>Apples</option> <option>Bananas</option> <option>Grapes</option> <option>Oranges</option> </select> <span>How many kg's per week do you expect to be ordering</span> </div>

CSS:

.styled-select select.select-css { appearance: none; height: 80px; pointer-events:all; position:absolute; top:0; left:0; } .styled-select { position:relative; appearance: none; overflow: hidden; pointer-events:none; }

jQuery:

$(".select-css").on("change", function(){ $(this).next('span').css('display', 'none'); }); Share Improve this answer Follow answered Mar 22, 2020 at 12:36 user3267302's user avatar user3267302user3267302 3114 silver badges15 bronze badges Add a comment | 0

You can use a library called select2

You also can look at this Stackoverflow Question & Answer

<select id="selectBox" style="width: 500px"> <option value="1" data-desc="this is my <br> multiple line 1">option 1</option> <option value="2" data-desc="this is my <br> multiple line 2">option 2</option> </select>

In javascript

$(function(){ $("#selectBox").select2({ templateResult: formatDesc }); function formatDesc (opt) { var optdesc = $(opt.element).attr('data-desc'); var $opt = $( '<div><strong>' + opt.text + '</strong></div><div>' + optdesc + '</div>' ); return $opt; }; }); Share Improve this answer Follow answered May 23, 2020 at 18:47 H.M Maruf's user avatar H.M MarufH.M Maruf 6512 bronze badges Add a comment | 0

An idea could be to use the optgroup. In my case found it better than the disabled approach. It's less confusing for the user than seeing the disabled option I think.

<select id="q1" v-model="selected" v-on:change="setCPost1(selected)"> <option value="0"></option> <template v-for="(child, idx) in getLevel1" v-bind:value="child.id" > <optgroup v-bind:value="child.id" :key="idx" :label="child.label" v-if="child.label_line_two" > </optgroup> <option v-bind:value="child.id" :key="idx" v-if="!child.label_line_two" > {{ child.label }} </option> <option v-bind:value="child.id" :key="idx" v-if="child.label_line_two" style="font-style:italic"> {{ child.label_line_two }} </option> </template> </select>

An external component sounds cool like Vue Select, but I wanted to stick with the native html select at the moment.

Share Improve this answer Follow edited Dec 10, 2020 at 15:46 answered Dec 10, 2020 at 15:38 magnarja's user avatar magnarjamagnarja 212 bronze badges Add a comment | -2

yes, by using css styles white-space: pre-wrap; in the .dropdown class of the bootstrap by overriding it. Earlier it is white-space: nowrap; so it makes the dropdown wrapped into one line. pre-wrap makes it as according to the width.

Share Improve this answer Follow edited Sep 13, 2017 at 6:03 answered Sep 12, 2017 at 13:51 Asterisk's user avatar AsteriskAsterisk 1291 silver badge11 bronze badges 0 Add a comment |

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid …

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

Draft saved Draft discarded

Sign up or log in

Sign up using Google Sign up using Email and Password Submit

Post as a guest

Name Email

Required, but never shown

Post Your Answer Discard

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.

  • The Overflow Blog
  • We'll Be In Touch - A New Podcast From Stack Overflow!
  • The app that fights for your data privacy rights
  • Featured on Meta
  • More network sites to see advertising test
  • We’re (finally!) going to the cloud!
  • Call for testers for an early access release of a Stack Overflow extension...

Linked

2 PHP wordwrap not working on <select> tag 0 Is there possibility to break line in select? 34 Are Multi-line Options in Html Select Tags Possible? 16 How to break a line in select2 dropdown? 17 Can you have multiple lines in an <option> element? 20 Word wrap options in a select list 9 Is there a way to add a description to each option in a dropdown using select2? 3 Select2 options with description 1 iOS 6 Select Menu vs iOS 7 Select Menu 1 Need to show the ng-option in two lines angular 6 See more linked questions 261 Is there an <option> separator for <select> elements? 34 Are Multi-line Options in Html Select Tags Possible? 17 Can you have multiple lines in an <option> element? 1 line break in option select dropdown 4 can options in selects be more than one line? 0 HTML: Consecutive spaces merged in option name in select 0 Line break as html select option value 4 HTML select option multiline value 0 HTML Select Option : How to Add Another Line of Option 16 How to break a line in select2 dropdown?

Hot Network Questions

  • What happens to your original form when you lose body parts while under the effect polymorph or alter self?
  • Prevent same name overwrite with robocopy
  • What is someone to do if they inherited a laptop containing illegal images
  • Continuum-distanced complete, ultrametric space
  • Polynomial.java - A tiny Java library for dealing with polynomials with double coefficients
  • New 90 TB/10 drive RAID 5 array state: clean, degraded, recovering. Why, and how long will it take to recover?
  • Which is larger? 999,999! or 2^(11!)
  • Are there three distinct Pythagorean triples on six integers?
  • Is it safe to solder 230V wire on relay?
  • logical error probability independent from distance in STIM default repetition code
  • Does a premise based on subjectivity compromise a logical argument seeking to conclude with an objective conclusion/claim?
  • USB drives in space?
  • Is it potentially dangerous to run a bash script with sh?
  • Best way to design a PCB for frequent component switching?
  • Does launch on warning assume incoming ICBMs carry nuclear warheads?
  • What is the best language to speak with locals in Singapore?
  • When SG-1 arrives in 1969, why is it initially an "empty" gate room?
  • how to stop using a command directly
  • Was it really possible to damage my VGA card by programming it in assembly through its latches registers?
  • Indoor 10 feet tall avocado tree I tall needs to be cut
  • Georeference the second and the following pages of PDF in QGIS
  • Adding wireless switch to existing 3-way wired system
  • Is it allowed to use web APIs exposed in open-source code?
  • How to generate N-dimensional multivariate-normal sample from N-2 marginals
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-html

Từ khóa » Html Br Options