How do you right-justify text in an HTML textbox?

HtmlCss

Html Problem Overview


I have a need to display many numerical values in columns. These values need to be easily editable so I cannot just display them in a table. I am using textboxes to display them. Is there a way for me to right-justify the text displayed in a textbox? It would also be nice if when the user is entering data for it to start displaying what they type from the right.

Html Solutions


Solution 1 - Html

Did you try setting the style:

input {
    text-align:right;
}

Just tested, this works fine (in FF3 at least):

<html>
	<head>
		<title>Blah</title>
		<style type="text/css">
		input { text-align:right; }
		</style>
	</head>
	<body>
		<input type="text" value="2">
	</body>
</html>

You'll probably want to throw a class on these inputs, and use that class as the selector. I would shy away from "rightAligned" or something like that. In a class name, you want to describe what the element's function is, not how it should be rendered. "numeric" might be good, or perhaps the business function of the text boxes.

Solution 2 - Html

Using inline styles:

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

or, put it in a style sheet, like so:

<style>
   .rightJustified {
        text-align: right;
    }
</style>

and reference the class:

<input type="text" class="rightJustified"/>

Solution 3 - Html

Apply style="text-align: right" to the input tag. This will allow entry to be right-justified, and (at least in Firefox 3, IE 7 and Safari) will even appear to flow from the 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
QuestionHaabdaView Question on Stackoverflow
Solution 1 - HtmlChris Marasti-GeorgView Answer on Stackoverflow
Solution 2 - HtmlPeter MeyerView Answer on Stackoverflow
Solution 3 - HtmlJohn RudyView Answer on Stackoverflow