2

I was wondering if it is possible to sort options in a html select tag with using HTML and CSS only.

So given following snippet:

<select>
    <option>c</option>
    <option>b</option>
    <option>a</option>
</select>

I want the browser to show those items in following order:

a
b
c

Using javascript, or worse hoping that those elements will come pre-sorted just feels so wrong :)

thx

1
  • 1
    I don't think this is possible. If you have access to the server side, sorting them before submitting is definitely the way to go. Commented Apr 27, 2016 at 11:13

1 Answer 1

6

Unfortunately NO! In html you can just code the things and in css you can style elements but cannot remove and save or shuffle the positions. There is no supported property in css or html to do so.

Using javascript is the only solution for it.

Further if you decide to do it with javascript then you may refer this Sort an html list with javascript

Sign up to request clarification or add additional context in comments.

4 Comments

You have to use javascript for some things which are not possible in css or html there is not problem in using for these cases. Go ahead with javascript dude. :)
Technically not a problem, but it would be just awesome if I could specify this really simple feature directly in the tag or using a css class ;(
i understand..do u really want to do it in select box or it's just a example.?
It is an actual case I am trying to implement as cleanly as possible and was just wondering why everyone on the net is apparently using 30-40 lines of code to achieve this :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.