How to use ONLY the first letter of an HTML element using CSS?

text-transform: capitalize does not work in this case, because the text already has an uppercase.

<select>
    <option>
      OPTION
    </option>
</select>

That didn't work either.

select{
  -webkit-appearance: none;
  text-transform: lowercase;
  display: inline;
}

select option::first-letter{
  text-transform: uppercase;
}

It should work on Chrome (at least) using CSS (not JS).

+2
source share
1 answer

In Chrome and Firefox, you can create an element optionif it selectis larger than 1.

You can use it as follows:

select {
  height: 1.4em;             /* show only one option when not focused */
}

select:focus {
  height: 100%;              /* show all options when focused */
}

option  {
  text-transform: lowercase; /* change to lowercase */
  padding-right: 2em;        /* the select width is based on width of its longest non-transformed ... */
                             /* option.  padding ensures that option is completely visible */
  display: none;             /* hide all options by default (see below) */
}

option::first-letter {
  text-transform: uppercase; /* change first letter to uppercase */
}

option:checked, select:focus option { 
  display: block;            /* show selected option, or show all options when the select is focused */
}
<select size="4">
  <option selected>NOW IS THE TIME</option>
  <option>for all good men</option>
  <option>tO Come To tHe aid</option>
  <option>of the party</option>
</select>
Run codeHide result

It will not act like a regular selection box, and Chrome has a weird behavior, since the selected option will have a gray background. I can’t figure out how to prevent this.

+3
source

Source: https://habr.com/ru/post/1620042/


All Articles