Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Width at Option of Select Box

enter image description here

In the picture, the width of option is larger than the select box. I want to set width of those options as same as select box & for those larger options set text-overflow as ellipsis. Any help would be appreciated.

Here is what I tried:

Html

<select>     <option>Select your University</option>     <option>Bangladesh University of Engineering and Technology</option>     <option>Mawlana Bhashani Science and Technology University</option> </select> 

Css

select, option {     width: 250px; }  option {     overflow: hidden;     white-space: nowrap;     text-overflow: ellipsis; } 

Fiddle: https://jsfiddle.net/3bsbcqfz/

like image 534
Mahedi Sabuj Avatar asked Apr 17 '16 12:04

Mahedi Sabuj


People also ask

How do you increase the width of a selection box in HTML?

Answer: Use the CSS :focus pseudo-class By default the size of the <select> element is depend on the size of the largest <option> text. However, sometimes it is useful to set a fixed width to the select box and increase its size back to the original size when the user tries to select some option (i.e. on focus).

How do I change the selection width in CSS?

Create a css and set the value style="width:50px;" in css code. Call the class of CSS in the drop down list. Then it will work.

How do I style a select dropdown?

There are many ways to design a <select> dropdown menu using CSS. The Dropdown Menu is mainly used to select an element from the list of elements. Each menu option can be defined by an <option> element that can nested inside the <select> element.


2 Answers

I tried to find a solution from CSS. But I failed to do it. Doesn't matter, I have written a simple javascript code for it. This is can do it something for it.

function shortString(selector) {    const elements = document.querySelectorAll(selector);    const tail = '...';    if (elements && elements.length) {      for (const element of elements) {        let text = element.innerText;        if (element.hasAttribute('data-limit')) {          if (text.length > element.dataset.limit) {            element.innerText = `${text.substring(0, element.dataset.limit - tail.length).trim()}${tail}`;          }        } else {          throw Error('Cannot find attribute \'data-limit\'');        }      }    }  }    window.onload = function() {    shortString('.short');  };
select {    width: 250px;  }    option {    width: 250px;  }
<select name="select" id="select">    <option class='short' data-limit='37' value="Select your University">Select your University</option>    <option class='short' data-limit='37' value="Bangladesh University of Engineering and Technology">Bangladesh University of Engineering and Technology</option>    <option class='short' data-limit='37' value="Mawlana Bhashani Science and Technology University">Mawlana Bhashani Science and Technology University</option>  </select>
like image 129
dinindu Avatar answered Sep 21 '22 04:09

dinindu


You can use javascript to make the length of the option text to be less so as that it matches the length of the option. I found no solution for only using css

var e=document.querySelectorAll('option')  e.forEach(x=>{  if(x.textContent.length>20)  x.textContent=x.textContent.substring(0,20)+'...';  })
select  {  width:160px;  }
<select>  <option>fwefwefwefwe</option>  <option>fwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwefwe</option>  </select>
like image 20
ellipsis Avatar answered Sep 18 '22 04:09

ellipsis