Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make text of a label fit within its width

Tags:

css

word-wrap

I have a label in html document, it has a fixed width but when the text exceeds the label's width it goes to 2nd line, I want to the text to fit in given width, no matter if it doesn't show the starting text.. Here is the code

<div style="width:50%">
    <label id="mlbl" style="width:50%;">text</label>
</div>
<button id="mbtn">click</button>

Script:

$('#mbtn').click(function() {
  $('#mlbl').text('asdasda gfgfg ad ad asd ad asd asd ad ad ');
});

Here is the Fiddle http://jsfiddle.net/rd79bpwn/

like image 985
Addy Avatar asked Aug 29 '14 07:08

Addy


2 Answers

Then simple use white-space: nowrap:

fiddle

Take a look here white-space-prop

like image 64
Alex Char Avatar answered Sep 30 '22 21:09

Alex Char


Adding:

label{
    display: block;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    width: 100%;
}

You can get the desire effect and also adding three dots to avoid cutting the text abruptly by adding the text-overflow: ellipsis; attribute. Note: text-overflowonly works in one line texts (Not multiple line).

TEST

UPDATE:

So you want to always show the end part of the text no matter what and the initial part that overflows should be hidden. Like this?

TEST

like image 31
goerwin Avatar answered Sep 30 '22 20:09

goerwin