I have several elements in HTML that I want to keep grouped when they wrap. I think the answer lies with some CSS (or maybe even JavaScript), but I'm not sure. For example, I have several elements laid out like this:
<label A><input A><label B><input B><label C><input C>
What happens is the users sometimes resize the browser window, and we end up with this:
<label A><input A><label B>
<input B><label C><input C>
In the example above, the input B isn't next to its input element. Is there a way to group the label and the input so they wrap together? I've tried divs and putting the elements together in a table with "nowrap", with mixed results. I don't mind the lines wrapping, I just need to keep the label and input element together.
If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).
You can force the content of the HTML <div> element stay on the same line by using a little CSS. Use the overflow property, as well as the white-space property set to “nowrap”.
If you want to place them next to each other you would require to use a CSS property float. As the name goes the float property specifies how an element should float in the webpage on the left or the right!. Values that a float property can have areas below, left - The element will float left w.r.t to its container.
If position: absolute; or position: fixed; - the left property sets the left edge of an element to a unit to the left of the left edge of its nearest positioned ancestor. If position: relative; - the left property sets the left edge of an element to a unit to the left/right of its normal position.
Try something like this:
<label>First Name:
<input type="text">
</label>
<label>Last Name:
<input type="text">
</label>
and set
label {
display: inline-block;
}
Demo http://dabblet.com/gist/3145028
Alternative: wrap both <label>
and <input>
in a <div>
with display: inline-block
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