Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS text ellipsis and 100 percent width

Tags:

html

css

I have the following container of text:

<div class="cardTitles">

<div class="title">
    <div class="titleContent">
        <i class="fa fa-star-o"></i>
        <b>Some long long long title here</b>
        <a href="some href"></a>
    </div>
</div>

... title divs ...

</div>

css:

.cardTitles {
    width: 100%;
    height: 100%;
}

.title
{
    position: relative;

    padding-top: 8px;
    padding-bottom: 8px;
}

I want to make text full width + text-overflow: ellipsis. So when I resize browser all title divs should be full 100% width of the parent cardTitles with cut text to three dots at the end.

I found that this is possible using flex. Here is the possible answer:

.parent-div {
    display: flex;
}

.text-div {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;    

    min-width: 0;
}

But this doesn't work for me. Parent container stops resizing when it reach the max length of some child title.


You can find an example here: http://88.198.106.150/tips/cpp/

Try to resize browser and look at title with text: What does it mean that a reference must refer to an object, not a dereferenced NULL pointer. I need to make it overflow ellipsis.

like image 507
Max Frai Avatar asked Dec 05 '14 10:12

Max Frai


People also ask

How do I change the width to 100% in CSS?

It seems like this should be one of the easiest things to understand in CSS. If you want a block-level element to fill any remaining space inside of its parent, then it's simple — just add width: 100% in your CSS declaration for that element, and your problem is solved.

How do you make an ellipsis text in CSS?

To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.

How do you handle 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 make HTML width 100?

With no height value provided for the HTML element, setting the height and/or min-height of the body element to 100% results in no height (before you add content). However, with no width value provided for the HTML element, setting the width of the body element to 100% results in full page width.


4 Answers

Try this out:

.titleContent {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

Here's and updated jsFiddle

like image 52
Aakash Avatar answered Sep 21 '22 14:09

Aakash


No Flex but could work also ... try it.

.element {
  display: table;
  table-layout: fixed;
  width: 100%;
}

.truncate {
  display: table-cell;
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
}

text-overflow ellipsis with 100% width

like image 27
Amitka Avatar answered Sep 24 '22 14:09

Amitka


The width needs to be added in pixels, percentage wont work along with other three properties as given below:-

.text-ellipsis{
    max-width: 160px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
like image 38
Nitesh Avatar answered Sep 21 '22 14:09

Nitesh


#div1 {
   white-space: nowrap; 
   width: 12em; 
   overflow: hidden;
   text-overflow: clip; 
   border: 1px solid #000000;
}

 #div2 {
   white-space: nowrap; 
   width: 12em; 
   overflow: hidden;
   text-overflow: ellipsis; 
   border: 1px solid #000000;
}

The following two divs contains a long text that will not fit in the box. As you can see, the text is clipped.

This div uses "text-overflow:clip":

This is some long text that will not fit in the box

This div uses "text-overflow:ellipsis":

This is some long text that will not fit in the box

like image 41
Giuseppe Pesce Avatar answered Sep 23 '22 14:09

Giuseppe Pesce