Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I layout text and inputs on a form to fit a specific width (justified)?

I have a form and I am trying to make a row "justified" so the entire row (which is a 4 textboxes and labels) to fit an exact pixel width (lets say 800px). Normally, if i just lay it out without any special css, It is less than 800px. I want to "stretch" it to be 800px. I don't care if I have to stretch the textboxes or the spaces in between them.

This is similar to justified layout in MS word if that helps describe what i am looking for. Is this possible within html / css in a form layout?

like image 226
leora Avatar asked Dec 26 '22 05:12

leora


1 Answers

You basically need text-align-last: justify which specifies the justification of the "last text line" in a block element, this defaults namely to the standard direction, which is left in LTR.

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>SO question 15994654</title>
        <style>
            #fields {
                width: 1000px;
                border: 1px solid gray;
            }

            .justified {
                text-align-last: justify;
            }
        </style>
    </head>
    <body>
        <p id="fields" class="justified">
            <label for="input1">label1</label>
            <input id="input1" />
            <label for="input2">label2</label>
            <input id="input2" />
            <label for="input3">label3</label>
            <input id="input3" />
            <label for="input4">label4</label>
            <input id="input4" />
        <p>
    </body>
</html>

This works in IE and Firefox (for older Firefox versions, add -moz-text-align-last: justify if necessary), however this fails in Webkit based browsers (Chrome/Safari). To cover those browser as well, you'd need to replace .justified as follows, so that the last line doesn't appear as a "last line" anymore, so that text-align: justify can do its job the usual way:

.justified {
    text-align: justify;
}

.justified:after {
    content: '';
    display: inline-block;
    width: 100%;
}

Note that the text-align-last: justify becomes redundant this way.

Here's the jsfiddle demo.

like image 127
BalusC Avatar answered Dec 28 '22 22:12

BalusC