Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make text-overflow:ellipsis for select with css only?

Tags:

html

css

frontend

Is it possible to make text-overflow: ellipsis; for select? In the divit is simple. When the string is too long there are dots, I need the same in select. I know, that it is possible with js, but I would like to get "light" css decision:

.select {
  box-sizing: border-box;
  display: block;
  width: 200px;
  height: 34.5px;
  padding: 5px 22px 3px 11px;
  font: 400 16px/24px sans-serif;
  color: #464a4c;
  vertical-align: middle;
  background: #fff url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiID8+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgd2lkdGg9IjEyIiBoZWlnaHQ9IjciPgoJPGRlZnM+CgkJPGNsaXBQYXRoIGlkPSJjbGlwXzAiPgoJCQk8cmVjdCB4PSItNDE1IiB5PSItNjYyIiB3aWR0aD0iMTQzNyIgaGVpZ2h0PSIyMjE2IiBjbGlwLXJ1bGU9ImV2ZW5vZGQiLz4KCQk8L2NsaXBQYXRoPgoJPC9kZWZzPgoJPGcgY2xpcC1wYXRoPSJ1cmwoI2NsaXBfMCkiPgoJCTxwYXRoIGZpbGw9InJnYig2NSw2NSw2NSkiIHN0cm9rZT0ibm9uZSIgZD0iTTAuNjUzNDQzIDQuNzY4MzdlLTA3TDExLjM0NjEgLTQuNzY4MzdlLTA3QzExLjk0MDIgLTQuNzY4MzdlLTA3IDEyLjE2ODIgMC41ODQ0ODggMTEuODY4MiAwLjkwNzY0OUw2LjU1MDMzIDYuNzE0MDRDNi4yNDczMSA3LjAzNjAzIDUuNzUzNzcgNy4xMTc3IDUuNDUwNzYgNi43OTQ1NEwwLjEzMjkxIDAuODY2ODE3Qy0wLjE3MDEwOSAwLjU0NDgyMyAwLjA2MjQwNTYgNC43NjgzN2UtMDcgMC42NTM0NDMgNC43NjgzN2UtMDciLz4KCTwvZz4KPC9zdmc+Cg==) no-repeat right 6px top 13px;
  border: 1px solid #D6D6D6;
  border-radius: 0;
  -moz-appearance: none;
  -webkit-appearance: none;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis
}
<select class="select">
  <option selected>select Mississippi Mississippi Mississippi Mississippi Mississippi</option>
  <option value="1">1 Mississippi Mississippi Mississippi Mississippi Mississippi</option>
  <option value="2">2 Mississippi Mississippi Mississippi Mississippi Mississippi</option>
  <option value="3">3 Mississippi Mississippi Mississippi Mississippi Mississippi</option>
</select>

<br>

<div class="select">div Mississippi Mississippi Mississippi Mississippi Mississippi</div>
like image 855
kizoso Avatar asked Aug 03 '17 20:08

kizoso


People also ask

How do you make an ellipsis text in CSS?

To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.

How do I force text-overflow ellipsis?

To force overflow to occur and ellipses to be applied, the author must apply the nowrap value to the white-space property on the element, or wrap the content in a <NOBR> tag.


1 Answers

Not the cleanest solution but you could always "trick" it, wrapping the select inside a div, and use :after pseudoelement to position a fake ... over the select hidding the content below:

.select {
  box-sizing: border-box;
  display: block;
  width: 200px;
  height: 34.5px;
  padding: 5px 22px 3px 11px;
  font: 400 16px/24px sans-serif;
  color: #464a4c;
  vertical-align: middle;
  background: #fff url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiID8+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgd2lkdGg9IjEyIiBoZWlnaHQ9IjciPgoJPGRlZnM+CgkJPGNsaXBQYXRoIGlkPSJjbGlwXzAiPgoJCQk8cmVjdCB4PSItNDE1IiB5PSItNjYyIiB3aWR0aD0iMTQzNyIgaGVpZ2h0PSIyMjE2IiBjbGlwLXJ1bGU9ImV2ZW5vZGQiLz4KCQk8L2NsaXBQYXRoPgoJPC9kZWZzPgoJPGcgY2xpcC1wYXRoPSJ1cmwoI2NsaXBfMCkiPgoJCTxwYXRoIGZpbGw9InJnYig2NSw2NSw2NSkiIHN0cm9rZT0ibm9uZSIgZD0iTTAuNjUzNDQzIDQuNzY4MzdlLTA3TDExLjM0NjEgLTQuNzY4MzdlLTA3QzExLjk0MDIgLTQuNzY4MzdlLTA3IDEyLjE2ODIgMC41ODQ0ODggMTEuODY4MiAwLjkwNzY0OUw2LjU1MDMzIDYuNzE0MDRDNi4yNDczMSA3LjAzNjAzIDUuNzUzNzcgNy4xMTc3IDUuNDUwNzYgNi43OTQ1NEwwLjEzMjkxIDAuODY2ODE3Qy0wLjE3MDEwOSAwLjU0NDgyMyAwLjA2MjQwNTYgNC43NjgzN2UtMDcgMC42NTM0NDMgNC43NjgzN2UtMDciLz4KCTwvZz4KPC9zdmc+Cg==) no-repeat right 6px top 13px;
  border: 1px solid #D6D6D6;
  border-radius: 0;
  -moz-appearance: none;
  -webkit-appearance: none;
  position:relative;

}
div {
  position:relative;
  display:inline-block;
}
div:after {
      content: '...';
    display: block;
    background-color: #fff;
    height: 15px;
    width: 25px;
    color: #464a4c;
    position: absolute;
    right: 23px;
    bottom: 11px;
    text-align: center;
    pointer-events: none;
}
<div>
<select class="select">
  <option selected>select Mississippi Mississippi Mississippi Mississippi Mississippi</option>
  <option value="1">1 Mississippi Mississippi Mississippi Mississippi Mississippi</option>
  <option value="2">2 Mississippi Mississippi Mississippi Mississippi Mississippi</option>
  <option value="3">3 Mississippi Mississippi Mississippi Mississippi Mississippi</option>
</select>
</div>

I have also added pointer-events: none; to the element so even if you click on it it will still open the select below.

like image 115
Alvaro Menéndez Avatar answered Oct 21 '22 22:10

Alvaro Menéndez