Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: ellipsis in left-aligned text of single line of dynamic width text where width of left-aligned text = 100% - right-aligned text

Tags:

css

I need to create a design in which the end user will see a single line of text with two components. The text-align:left div could have any arbitrary length of text. The text-align:right div will generally be about 9 or 10 characters long, but it could conceivably go as high as 20-some characters.

Here's how it needs to appear to end users:

  • The text-align:left text should use ellipsis to hide any text that doesn't fit on the screen in a single line of text.
  • The text-align-right text should always be on screen in that same single line.
  • The width of the text-align:left div should be equal to the width of the browser window less the width of the text-align:right text's div.
  • The width of the text-align:right div should be equal to the width of the text within it plus its horizontal padding.
  • The text needs to be vertically centered in the middle of the line.

I almost got this working in a fiddle, but in that I needed to use a fixed width for the text-align:right div. Here's the HTML:

<body>
    <div id="wrap">
        <div id="left">Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left</div>
        <div id="right">Right</div>
    </div>
</body>

and here's the CSS:

* {border:0; margin:0; padding:0;}

html, body {background-color:gray; height:100%; width:100%}

#left, #right {padding:5px;}

#left {
    background-color:red;
    float:left;
    text-align:left;
    width:calc(100% - 120px); /* deducted amount = width of #right + total horizontal padding*/

    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

#right {
    background-color:orange;
    float:right;
    text-align:right;
    white-space: nowrap;
    width:100px;
}

I could conceivably jump to JQuery here and have it set the widths of both divs according to the width of the text-align:right div, but I'd really prefer to solve this with CSS if possible.

Previously I tried to use display:table and display:table-cell to make this happen, but I ran into the various problems of using ellipsis with table-cell. Using table-layout:fixed didn't properly display ellipsis, nor did display:block, nor did setting a max-width. I'm happy to use table-cell if that will do it.

So is there a way to do this using purely CSS?

like image 605
Vincent Avatar asked Sep 02 '13 06:09

Vincent


1 Answers

1) Remove float:left from your left div and

2) Add display:block on your left div

and bingo! - the right div takes up only as much space as it needs - without giving it a fixed width anymore.

UPDATED FIDDLE + FIDDLE 2 (just to prove it works)

Markup

<div id="wrap">
        <div id="right">Right</div>
        <div id="left">Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left Left</div> 
</div>

CSS

#wrap
{
    width: 100%;
}
#left, #right {padding:5px;}

#left {
    background-color:red;
    text-align:left;
    display: block;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

#right {
    background-color:orange;
    float:right;
    text-align:right;
    white-space: nowrap;
}
like image 81
Danield Avatar answered Nov 15 '22 07:11

Danield