For my website I have a checkbox with a label next to it, but if the text in the label is longer than for example 60px, I want to cut off the text.
I know about the text-overflow: clip
, this does exactly what I want, but for some reason it doesn't work on a label. And using a div instead isn't a really good solution, since you can't click on the text to select the checkbox then.
How can I do this?
A text-overflow property in CSS is used to specify that some text has overflown and hidden from view. The white-space property must be set to nowrap and the overflow property must be set to hidden. The overflowing content can be clipped, display an ellipsis ('…'), or display a custom string.
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).
Breaking long words The word-wrap property is now treated by browsers as an alias of the standard property. An alternative property to try is word-break . This property will break the word at the point it overflows.
break-word: This is the actual CSS syntax that tells the browser to wrap a long text over to a new line. normal: It breaks each word at the normal points of separation within a DOM.
label or span are inline by default. You cannot set width on inline elements. So make label inline-block
/// markup
<input type="checkbox"/> <label style="display: inline-block; width: 60px; overflow: hidden">Sample text sample text</label>
vertical-align: middle
serves as a great solution here to align the label with the check-box and text-overflow: ellipsis
for snipping the text with the three dots ...
at the end.
The following snippet shows an example:
HTML:
<input type="checkbox"/>
<label>Pretty awesome label to describe the uber cool checkbox. </label>
CSS:
label {
text-overflow: ellipsis;
display: inline-block;
overflow: hidden;
width: 300px;
white-space: nowrap;
vertical-align: middle;
}
Here is a fiddle for you - link
A utility class ellipsis
could be used here:
.ellipsis {
width: 100%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
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