JSFiddle - React, Tailwind, and code Playground
by pmamode
HTML
<select id="width_tmp_select">
<option id="width_tmp_option"></option>
</select>
<span>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </span>
<select class="resizer">
<option>All</option>
<option>Longer</option>
<option>A very very long string..................................</option>
</select>
<span>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </span>
<select class="resizer">
<option>A very very long string...........................</option>
<option>two plus four</option>
<option>four plus four</option>
</select>
<span>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span>
CSS
#resizing_select {
/*width: 150px;*/
}
#width_tmp_select {
display: none;
}
select {
background-color: grey;
color: white;
border: 0 !important;
/*Removes border*/
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
text-overflow: '';
text-indent: 0.01px;
/* Removes default arrow from firefox*/
text-overflow: "";
/*Removes default arrow from firefox*/
}
select::-ms-expand {
display: none;
}
.select-wrapper {
padding: 0px;
overflow: hidden;
}
JavaScript
$(document).ready(function() {
$('.resizer').change(function() {
$("#width_tmp_option").html($('option:selected', this).text());
$(this).width($("#width_tmp_select").width());
});
function resizeSelect() {
$('.resizer').each(function() {
$("#width_tmp_option").html($('option:selected', this).text());
$(this).width($("#width_tmp_select").width());
});
}
resizeSelect();
});