Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Truncate text in an absolute positioned div

Tags:

html

css

Here is the JSFiddle I'm trying to do: JSFiddle Example

It is responsive, and in a large width, it is exactly what I want, like this:

enter image description here

But in small sizes, It overlaps the another text and/or breaks the lines, like this:

enter image description here

and this:

enter image description here

And this is my css to the texts:

.giro-nome {
    position: absolute;
    top: 25%;
}

.giro-percentual {
    position: absolute;
    right: 0;
    top: 25%;
    font-weight: 700;
}

I wanted just to stop the text in a single line, something like this(expected, not real):

enter image description here

Is it possible? Probably not with absolute, like I'm doing, but I have no idea another way to do it.

Thank you advanced.

like image 414
Cechinel Avatar asked Aug 27 '14 13:08

Cechinel


People also ask

How do I hide the overflow text in a div?

We can solve this problem by using CSS overflow property. overflow: hidden; hidden – The overflow is clipped, and the rest of the content will be invisible.

How do I hide 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.

Can I use text-overflow ellipsis?

The text-overflow property specifies how overflowed content that is not displayed should be signaled to the user. It can be clipped, display an ellipsis (...), or display a custom string.


1 Answers

text-overflow: ellipsis; is what you're looking for.

8.2. Overflow Ellipsis: the ‘text-overflow’ property

This property specifies rendering when inline content overflows its block container element ("the block") in its inline progression direction that has ‘overflow’ other than ‘visible’. Text can overflow for example when it is prevented from wrapping (e.g. due to ‘white-space:nowrap’ or a single word is too long to fit). Values have the following meanings:

ellipsis Render an ellipsis character (U+2026) to represent clipped inline content. Implementations may substitute a more language/script-appropriate ellipsis character, or three dots "..." if the ellipsis character is unavailable.

However you should specify the width of the absolutely positioned element at first. Either by left/right properties, or by other approaches such as width: 90% or width: calc(100% - 80px):

EXAMPLE HERE

.giro-nome {
    position: absolute;
    top: 25%;
    left: 0; right: 80px;  /* Equal to > width: calc(100% - 80px) */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
like image 189
Hashem Qolami Avatar answered Nov 15 '22 03:11

Hashem Qolami