Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS text-overflow on label

Tags:

css

label

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?

like image 549
JeroenD Avatar asked Oct 06 '13 10:10

JeroenD


People also ask

How do I fix text-overflow in CSS?

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.

How do I stop text wrapping in CSS?

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 you break a long text in CSS?

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.

How do I make text go to next line in CSS?

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.


3 Answers

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>
like image 102
Arun Aravind Avatar answered Sep 21 '22 10:09

Arun Aravind


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

like image 26
ShellZero Avatar answered Sep 17 '22 10:09

ShellZero


A utility class ellipsis could be used here:

.ellipsis {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
like image 36
Paul Melero Avatar answered Sep 20 '22 10:09

Paul Melero