I can't get this example to work by wrapping the label to the right of the checkbox if it is too long relative to the 100px div.
I attach the example.
<div style="width:100px">
<tr valign="bottom">
<td>
<div style="float:left; width:100%;">
<input type="checkbox" id="aceptoImagen" name="aceptoImagen" value="SI">
<label style="word-wrap:normal; font-family: Verdana, Geneva, sans-serif; font-size: 12px; color: rgb(102, 102, 102); font-weight: bold; font-style: normal; padding-top: 3px; cursor: pointer;"> ¿Quiero que se rompa en varias lineas si el texto es my largo y no se meta debajo del check sino que se quede alineado a su derecha siempre ?</label>
</div>
</td>
</tr>
</div>
I modified your HTML because as it was (tr inside a div) it didn't make sense. I hope the answer helps you.
First , you have to tell the label that its alignment is
inline-block
. That is, that it appears next to the checkbox but in the form of a box and that way it does not overflow to the left in the second or third line.Second , you have to give the label an explicit width , roughly calculating that between paddings and other default styles the checkbox will be about 30px wide. That is, if your container is 150px wide, the label can only be up to 120px wide before generating an implicit break. If you don't give it a fixed width (and since you did set
inline-block
), the label will try to use all available space and generate an implicit break.Third , you tell it that its vertical alignment is
top
so that the first line of the label fits the checkbox, and not the checkbox in the middle or bottom of the text.In summary:
display: inline-block
width: 120px
(you will see how to adjust it)vertical-align: top
.If you don't know the width of the container, or the container is flexible, you can always tell the label that its width is
calc(100% - 30px)
, although this sometimes requires some adjustment when using a preprocessor.(You could also say that both the checkbox and the label have
float:left
. This would be the same as givingdisplay:inline-block
both elements, but it doesn't display exactly the same and you still need to give the label an explicit width.)There are other ways to do this by replacing the input+label set with other combinations (it can be an element
a
, you can separate them using two table cells, you can use a flex-grid) but the beauty of leaving it as it is is that a label it can have the attributefor
that associates it with the checkbox, and allows you to click on the label as well as on the checkbox, which is positive for the user experience.A quick fix that wouldn't require much change: add the style
display:flex
to thediv
one containing theinput
andlabel
. By default they will be aligned in columns adjusting their width automatically to the content (so as the text is longer it will push theinput
to the left, occupying all the remaining space on the right).Here you can see it working with that minor change:
The best thing is that you change the label
<label>
to<p>
sincep
it fits the whole line and you can also use right alignment for that...