How to align texts inside input?

For all inputs by default, the input text starts on the left. How do you start on the right side?

+123
html css html-input forms
Aug 24 '12 at 18:09
source share
6 answers

Use the text-align property in your CSS:

input { text-align: right; } 

This will apply to all page entrances.
Otherwise, if you want to align the text of just one input, set the inline style:

 <input type="text" style="text-align:right;"/> 
+203
Aug 24 '12 at 18:12
source share

Try this in your CSS:

 input { text-align: right; } 

To center the text:

 input { text-align: center; } 

But it should be left-aligned, as this is the default value - and, apparently, is the most user-friendly.

+17
Aug 28 '12 at 9:18
source share

Here you go :

 input[type=text] { text-align:right } 
 <form> <input type="text" name="name" value=""> </form> 
+8
Aug 24 '12 at 18:13
source share

The accepted answer here is correct, but I would like to add some information. If you use a library / framework such as bootstrap, classes can be created for this. For example, bootstrap uses the text-right class. Use it like this:

 <input type="text" class="text-right"/> <input type="number" class="text-right"/> 

As a note, this also works with other types of input, such as numeric, as shown above.

If you are not using a good framework like bootstrap, you can create your own version of this helper class. Like other answers, but we are not going to add it directly to the input class, so it will not apply to all inputs of your site or page, this may be an undesirable behavior. This way, it will create a good css class to align objects without the need for inline styling or impact on every input window.

 .text-right{ text-align: right; } 

Now you can use this class in the same way as the inputs above with class="text-right" . I know that this does not save a lot of key touches, but it makes your code cleaner.

+5
Aug 22 '17 at 15:43 on
source share

If you want it to align to the right after the text loses focus, you can try using the direction modifier. This will display the correct part of the text after losing focus. for example, useful if you want to show the file name in a long way.

 input.rightAligned { direction:ltr; overflow:hidden; } input.rightAligned:not(:focus) { direction:rtl; text-align: left; unicode-bidi: plaintext; text-overflow: ellipsis; } 
 <form> <input type="text" class="rightAligned" name="name" value=""> </form> 

Selector not currently supported: browser support

+1
Sep 04 '18 at 10:45
source share

No CSS: Use the STYLE property of text input

STYLE = "text-align: right;"

-5
Dec 19 '13 at 9:01
source share



All Articles