Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make "text-overflow: ellipsis" work on floating divs?

Tags:

css

I have two buttons floating to opposite sides of the screen, and I'd like them to collapse nicely if the screen shrinks. Visually, this is what I want:

________________________________________________________________
|                                                               |
|   |LongTextButtonName|                 |LongTextButtonName|   | When the screen
|_______________________________________________________________| is large

_______________________________________
|                                      |
|   |LongTextBu...|  |LongTextBu...|   | When the screen is small
|______________________________________|

Unfortunately, right now I get this:

________________________________________
|                                       |
|   |LongTextButtonName|                |
|                |LongTextButtonName|   | 
|_______________________________________|

I'd like a css-only solution.

Here's my current html and css (and here's a fiddle):

<div class="button-container">
  <div class="left-button"><a>LongTextButtonName</a></div>
  <div class="right-button"><a>LongTextButtonName</a></div>
</div>

.button-container {
  min-width:320px;
}
.button-container > div {
  border: 1px solid gray;
  background-color: orange;
  min-width: 160px;
  padding: 8px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.left-button {
  float: left;
}
.right-button {
  float: right;
}
like image 968
Nick G. Avatar asked Jan 06 '13 16:01

Nick G.


Video Answer


1 Answers

The fiddle below works based on the following:

  • the container requires a width in order to trigger the overflow and ellipsis.
  • to allow the boxes continually collapse I have used a percentage width.
  • to account for the padding I have used the CSS3 box-sizing:border-box;

Note that because of the box-sizing:border-box;, this solution will not work on older browsers.

.button-container {
  min-width:320px;
}
.button-container > div {
  border: 1px solid gray;
  background-color: orange;
  max-width: 50%;
  padding: 8px;
  white-space: nowrap;
  overflow: hidden;
  box-sizing:border-box;
  text-overflow: ellipsis;
}
.left-button {
  float: left;
}
.right-button {
  float: right;
}

http://jsfiddle.net/UfxgZ/1/

like image 99
PassKit Avatar answered Oct 04 '22 03:10

PassKit