Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontally text center for select option

Tags:

html

css

I have this simple fiddle with standard <select> html element, all what am trying to do is give center alignment for <option> tags.

I've tried already text-indent: 20px but its center only the visible tag in the select!

How can I center it?

like image 958
Mahmoud Avatar asked Nov 05 '13 13:11

Mahmoud


People also ask

How do I center align text in Dropdownlist?

Dropdown button can be positioned in the center of the page by setting the “text-align” property of dropdown div to center. The following example contains a simple Bootstrap dropdown menu with an added class “my-menu”. The property “text-align: center” is added to the class.

How do I center the content horizontally?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do you center text horizontally in HTML?

If we wanted to horizontally center that text on the page, then we can use the text-align property. If you wanted to horizontally center all of the text on the page, then you can use the text-align property in the body selector. In this next example, we have some more text in our HTML.


4 Answers

Solution 1: (A simple solution)

select {
   ...    
   padding: 0 0 0 20px;
}

DEMO 1

Solution 2: (added 27-10-2017)
Only the selected number will be centered.
* Probably not yet supported by Safari

select {
   ...
   text-align: center;
   text-align-last: center;
}

DEMO 2

like image 193
C Travel Avatar answered Nov 02 '22 08:11

C Travel


You can just use

select {
    text-align: center;
}

See modified JSFiddle

Unfortunately, this works for Firefox only. For Chrome, you must use padding-left similar to @CTravel's solution

select {
    padding-left: 20px;
}

Modified JSFiddle

But this doesn't work on the option elements in Firefox, so this isn't a cross-browser solution either.

like image 39
Olaf Dietsche Avatar answered Nov 02 '22 08:11

Olaf Dietsche


don't use

select {
   ...    
   padding: 0 0 0 20px;
}

or

select {
    text-align: center;
}

they are no working in select. maybe you can use JQuery's select style:JQuery UI Select

like image 2
Qilin Lou Avatar answered Nov 02 '22 09:11

Qilin Lou


Check this link out:

http://filamentgroup.com/lab/jquery_ui_selectmenu_an_aria_accessible_plugin_for_styling_a_html_select/

as stated elsewhere, it is not possible with plain CSS. (Not for all browsers anyhow)

like image 1
William Riley Avatar answered Nov 02 '22 07:11

William Riley