Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place two divs side by side where LEFT one is sized to fit and other takes up remaining space?

Tags:

html

css

ellipsis

I'm trying to place two div's beside each other with the following criteria:

  1. Both divs must stay on the same line.
  2. Priority must be given to the left div. As much text as possible should be displayed in the left div up to the point where ellipsis is used in case of overflow.
  3. The right div's text should be right aligned. In the case of overflow, ellipsis should be used.
  4. Text is dynamic, so no percentages or fixed widths can be used.
  5. Only needs to work on webkit based browser, so CSS3 solution is preferred.

Here are some sample images of how it would look:

Input

<div class='left'>I should always fit. If not, ellipsis should be used.</div><div class='right'>Right align and fit me if space available here.</div>

Output

enter image description here

Input

<div class='left'>I should always fit. If not, ellipsis should be used. And some more text and more, and more text.</div><div class='right'>Right align and fit me if space available here.</div>

Output

enter image description here

Input

<div class='left'>This text is left aligned.</div><div class='right'>This text is right aligned.</div>

Output

enter image description here

like image 475
Philip Murphy Avatar asked Sep 16 '13 08:09

Philip Murphy


People also ask

How do I make two divs sit side by side?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How do I place two divs side by side the same height in CSS?

Answer: Use the CSS3 flexbox With CSS3 flex layout model you can very easily create the equal height columns or <div> elements that are aligned side by side. Just apply the display property with the value flex on the container element and the flex property with the value 1 on child elements.


1 Answers

I have it with the exception that when there is empty space my right div is eating it (with text right aligned). You don't list that as an ask, so I was unsure if it was just how you drew it? Fiddle here: http://jsfiddle.net/mdares/fSCr6/

HTML:

<div class="container">
    <div class="left">Some Text, Repeat, Repeat, Repeat, ,Some Text, and then: </div>
    <div class="right">other Text ttt other Text tttother Text tttother Text ttt</div>
</div>

<p />

<div class="container">
    <div class="left">Some Text, Repeat, Repeat, Repeat, ,Some Text, Some Text, Repeat, Repeat, Repeat, ,Some Text,</div>
    <div class="right">other Text ttt other Text tttother Text tttother Text ttt</div>
</div>

<p />

<div class="container">
    <div class="left">Some Text, Repeat, Repeat, Repeat, ,Some Text, </div>
    <div class="right">other Text ttt</div>
</div>

CSS:

.container {
    width: 600px;
}
.left {
    max-width: 100%;
    background:red;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
    -ms-text-overflow:ellipsis;
    float: left;
}
.right {
    background:yellow;
    white-space:nowrap;
    overflow:hidden;
    text-overflow:ellipsis;
    -ms-text-overflow:ellipsis;
    text-align: right;
}

And finally:

enter image description here

like image 134
Matthew Avatar answered Sep 20 '22 07:09

Matthew