Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML select and option mixed directions (ltr & rtl)

I have a select box that I want to be aligned to the right, but I want to keep the options dropdown aligned to the left. Like so:

<select>
  <option>item</option>
</select>

And the CSS:

select { direction: rtl; }
option { direction: ltr; }

But setting the direction of the select tag also sets the direction of the select dropdown menu. See here

Is this even possible? I know that I could, as an alternative, build a custom select functionality with js, but I'd like to keep it simple if I can.

like image 732
Kyle Shike Avatar asked Jul 10 '14 16:07

Kyle Shike


People also ask

How do you style the option of an HTML select element?

You can style the option elements to some extent. Using the * CSS selector you can style the options inside the box that is drawn by the system. That is true only because the all selector targets the shadow DOM innards, you can target those manually per browser.

What HTML element is used to allow the user to select from multiple options?

The <select> element has some unique attributes you can use to control it, such as multiple to specify whether multiple options can be selected, and size to specify how many options should be shown at once.

What is LTR and rtl?

For example, the en-US locale (for US English) specifies left-to-right. Most Western languages, as well as many others around the world, are written LTR. The opposite of LTR, RTL (Right To Left) is used in other common languages, including Arabic ( ar ) and Hebrew ( he ).


1 Answers

direction: rtl; is not exactly text alignment. Or to be precise text alignment is a side effect of direction. Note that direction also affects position of the dropdown arrow in your setup.

Conventional browsers do not allow you to style select element (it looks significantly different on different platforms, especially mobiles).

In Sciter you can style <select> parts individually as select components are normal DOM elements there:

select > caption { text-align:right; } 
select > button { ... } 
select > popup > option { text-align:left; } 

I've made it possible as Sciter is designed specifically for desktop UI and <select> styling is a must there.

As a solution for plain web pages I would suggest to use <select> substitutes like http://getbootstrap.com/components/#btn-dropdowns

like image 156
c-smile Avatar answered Sep 18 '22 02:09

c-smile