Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Dropdown (select) with Text Wrap and Border after every value (option)

I am trying to achieve two things with DropDown.

  • First I want to Wrap the text in the list of options within a dropdown.
  • Second, I want to put a border after every option

and I want to support IE (and other browsers too).

This is because I would have long text in the dropdown and I don't wish to cut them. For that reason, I want to do the aforementioned things.

Something like this:-

http://jsfiddle.net/fnagel/GXtpC/embedded/result/

select the one with "Same with option text formatting, Select an Address". Notice how the options are formatted and have a border-bottom with each of them.

Here is what I tried (Text):-

.myselect {
  width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.myselect option {
  white-space: nowrap;
  width: 100% border-bottom: 1px #ccc solid;
  /* This doesn't work. */
}
<select name="d" class="myselect">
  <option value="sdf" class="test1"> line text How to wrap the big line text </option>
  <option value="sdf2" class="test1"> line text How to wrap the big line text </option>
  <option value="sdf3" class="test1"> line text How to wrap the big line text </option>
  <option value="sdf4" class="test1"> line text How to wrap the big line text </option>
</select>
like image 934
Steve Avatar asked Sep 02 '13 17:09

Steve


2 Answers

select {
  width: 100px;
  overflow: hidden;
  white-space: pre;
  text-overflow: ellipsis;
  -webkit-appearance: none;
}

option {
  border: solid 1px #DDDDDD;
}
<select name="d" class="myselect">
  <option value="sdf" class="test1"> line text How to wrap the big line text </option>
  <option value="sdf2" class="test1"> line text How to wrap the big line text </option>
  <option value="sdf3" class="test1"> line text How to wrap the big line text </option>
  <option value="sdf4" class="test1"> line text How to wrap the big line text </option>
</select>
like image 82
Ashok Dhakhada Avatar answered Nov 18 '22 15:11

Ashok Dhakhada


The currently accepted answer only truncates text with ellipsis and adds a border which doesn't completely solve the problem at hand.

I feel like this is a more complete, more cross-browser compatible answer: Make text in select element wrap when too long?

In a nutshell, you can either do it the right way by using javascript, or do it the easy... not so compatible way by using the css property white-space: pre-wrap on your option elements.

Note: if you go with using white-space: pre-wrap, be sure to add -moz- and -o- browser prefixes.


Here's what I've come up with as a quick, simple CSS solution (though the Jquery one I've mentioned is more robust and better):

select {
  width: 200px;
  max-width: 100%;
  /* So it doesn't overflow from it's parent */
}

option {
  /* wrap text in compatible browsers */
  -moz-white-space: pre-wrap;
  -o-white-space: pre-wrap;
  white-space: pre-wrap;
  /* hide text that can't wrap with an ellipsis */
  overflow: hidden;
  text-overflow: ellipsis;
  /* add border after every option */
  border-bottom: 1px solid #DDD;
}
<select name="d" class="myselect">
  <option value="sdf" class="test1"> line text How to wrap the big line text </option>
  <option value="sdf2" class="test1"> line text How to wrap the big line text </option>
  <option value="sdf3" class="test1"> line text How to wrap the big line text </option>
  <option value="sdf4" class="test1"> line text How to wrap the big line text </option>
</select>
like image 7
Nathan Blair Avatar answered Nov 18 '22 13:11

Nathan Blair