How to align texts inside of an input?

HtmlCssFormsHtml Input

Html Problem Overview


For all default inputs, the text you fill starts on the left. How do you make it start on the right?

Html Solutions


Solution 1 - Html

Use the text-align property in your CSS:

input { 
    text-align: right; 
}

This will take effect in all the inputs of the page.
Otherwise, if you want to align the text of just one input, set the style inline:

<input type="text" style="text-align:right;"/> 

Solution 2 - Html

Try this in your CSS:

input {
text-align: right;
}

To align the text in the center:

input {
text-align: center;
}

But, it should be left-aligned, as that is the default - and appears to be the most user friendly.

Solution 3 - Html

The accepted answer here is correct but I'd like to add a little info. If you are using a library / framework like bootstrap there may be built in classes 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 works on other input types as well, like numeric as shown above.

If you aren't using a nice framework like bootstrap then you can make your own version of this helper class. Similar to other answers but we are not going to add it directly to the input class so it won't apply to every single input on your site or page, this might not be desired behavior. So this would create a nice easy css class to align things right without needing inline styling or affecting every single input box.

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

Now you can use this class exactly the same as the inputs above with class="text-right". I know it isn't saving that many key strokes but it makes your code cleaner.

Solution 4 - Html

Here you go:

input[type=text] { text-align:right }

<form>
    <input type="text" name="name" value="">
</form>

Solution 5 - Html

If you want to get it aligned to the right after the text looses focus you can try to use the direction modifier. This will show the right part of the text after loosing focus. e.g. useful if you want to show the file name in a large path.

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>

The not selector is currently well supported : Browser support

Solution 6 - Html

> @Html.TextBoxFor(model => model.IssueDate, new { @class = "form-control", name = "inv_issue_date", id = "inv_issue_date", title = "Select Invoice Issue Date", placeholder = "dd/mm/yyyy", style = "text-align:center;" })

Solution 7 - Html

The following methods may be of use to you:

<style>
   input {
	     text-align: right !important; 
   }
   textarea{
        text-align:right !important;
   }
   label {
       float: right !important;
   }
</style>

Solution 8 - Html

With Bootstrap 5 is now class="text-end":

<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<input step="any" id="myInput" type="number" class="text-end">

. Refer to: https://getbootstrap.com/docs/5.0/utilities/text/#text-alignment

Solution 9 - Html

Without CSS: Use the STYLE property of text input

STYLE="text-align: right;"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionphzView Question on Stackoverflow
Solution 1 - HtmleliasView Answer on Stackoverflow
Solution 2 - HtmlGautam3164View Answer on Stackoverflow
Solution 3 - HtmlDoug E FreshView Answer on Stackoverflow
Solution 4 - HtmlCynthiaView Answer on Stackoverflow
Solution 5 - HtmlrulonderView Answer on Stackoverflow
Solution 6 - HtmlNadeem TajView Answer on Stackoverflow
Solution 7 - HtmlMohamed EmadView Answer on Stackoverflow
Solution 8 - Htmlpft1View Answer on Stackoverflow
Solution 9 - HtmlHardeep SinghView Answer on Stackoverflow