Let's say I have 10 input fields and before the input fields on the left side I have span tag that holds the text to indicate what the user should enter into the field. I did some stuff but I am unsure how to add space in between the span tag and the input field regardless of how big the text is?
Like this:
Give space using the space character If you don't want to write any external CSS to put the space between the label and input, an alternative solution would be to use space character. The or the non-breaking space is an empty space which does not break into a new line on word wrap.
HTML Non-Breaking Space ( ) The simplest way to add a space in HTML (besides hitting the spacebar) is with the non-breaking space entity, written as or  .
You can add “ “ in between those HTML tags. This is an HTML entity, if you write multiple You'll get multiple white spaces. You could apply the space character in the label “ ” or apply a left or right margin to either element.
To create extra spaces before, after, or in-between your text, use the (non-breaking space) extended HTML character.
Some time has passed and I changed my approach now when building forms. I've done thousands of them till today and got really tired of typing id for every label/input
pair, so this was flushed down the toilet. When you dive input
right into the label
, things work the same way, no ids necessary. I also took advantage of flexbox
being, well, very flexible.
HTML:
<label> Short label <input type="text" name="dummy1" /> </label> <label> Somehow longer label <input type="text" name="dummy2" /> </label> <label> Very long label for testing purposes <input type="text" name="dummy3" /> </label>
CSS:
label { display: flex; flex-direction: row; justify-content: flex-end; text-align: right; width: 400px; line-height: 26px; margin-bottom: 10px; } input { height: 20px; flex: 0 0 200px; margin-left: 10px; }
Fiddle DEMO
Use label
instead of span
. It's meant to be paired with inputs and preserves some additional functionality (clicking label focuses the input).
This might be exactly what you want:
HTML:
<label for="dummy1">title for dummy1:</label> <input id="dummy1" name="dummy1" value="dummy1"> <label for="dummy2">longer title for dummy2:</label> <input id="dummy2" name="dummy2" value="dummy2"> <label for="dummy3">even longer title for dummy3:</label> <input id="dummy3" name="dummy3" value="dummy3">
CSS:
label { width:180px; clear:left; text-align:right; padding-right:10px; } input, label { float:left; }
jsfiddle DEMO here.
This can be accomplished using the brand new CSS display: grid
(browser support)
HTML:
<div class='container'> <label for="dummy1">title for dummy1:</label> <input id="dummy1" name="dummy1" value="dummy1"> <label for="dummy2">longer title for dummy2:</label> <input id="dummy2" name="dummy2" value="dummy2"> <label for="dummy3">even longer title for dummy3:</label> <input id="dummy3" name="dummy3" value="dummy3"> </div>
CSS:
.container { display: grid; grid-template-columns: 1fr 3fr; }
When using css grid, by default elements are laid out column by column then row by row. The grid-template-columns
rule creates two grid columns, one which takes up 1/4 of the total horizontal space and the other which takes up 3/4 of the horizontal space. This creates the desired effect.
JS-FIDDLE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With