Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping wrapped elements together

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.

like image 281
Frecklefoot Avatar asked Jul 19 '12 16:07

Frecklefoot


People also ask

How do you stop elements from wrapping?

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).

How do I force HTML elements to stay on the same line?

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”.

How do you get elements to show next to each other?

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.

How do you stick elements to the left?

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.


1 Answers

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

like image 178
Ana Avatar answered Oct 27 '22 07:10

Ana