How can I make a fluid width header with text-overflow ellipsis without wrapping?

I need a header and button on the same line, using ellipses if necessary.

Here's a fiddle: http://jsfiddle.net/epyFT/1/

I'd like output to look like this:

_________________________________________________________
|                                                       |
| Header goes here [button]                             |
|                                                       |
---------------------------------------------------------

Or

_________________________________________________________
|                                                       |
| Super, super, super, super long header... [button]    |
|                                                       |
---------------------------------------------------------

Or with a smaller window:

____________________________
|                          |
| Header goes... [button]  |
|                          |
----------------------------

The button should never float to the next line. How can I do this?

HTML

<div class="container">
    <h2>This is the header that should never wrap and elipse if it doesn't fit</h2>
    <button>Button</button>
</div>

<div class="container">
    <h2>Header</h2>
    <button>Button</button>
</div>

CSS

.container {
    width:100%;
}
h2 {
    display:inline;
    min-width:200px;
    overflow: hidden; white-space: nowrap; text-overflow: ellipsis; word-break: break-all; word-wrap: break-word;
}
button {
    width:100px;
}

Bonus

Extra credit for getting a second (fixed width) button in there to pull right.

_________________________________________________________
|                                                       |
| Header goes here [button1]                  [button2] |
|                                                       |
|                                                       |
| Super, super, super, super long... [button] [button2] |
|                                                       |
---------------------------------------------------------

Answers


Maybe something like this helps? http://jsfiddle.net/epyFT/3/

I'm not sure how to make the button align to the no-wrapped width of the container, and am only using percentage widths for the cells.

New code:

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

.container>div {
  display: table-cell;
}

.cell1 {}

.wrap {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  word-break: break-all;
  word-wrap: break-word;
}

.cell2 {
  width: 60%;
}

h2 {
  display: inline;
  min-width: 200px;
}

button {
  width: 100px;
}
<div class="container">
  <div class="cell1">
    <div class="wrap">
      <h2>This is the header that should never wrap and elipse if it doesn't fit</h2>
    </div>
  </div>
  <div class="cell2">
    <button>Button</button>
  </div>
</div>
<div class="container">
  <h2>Header</h2>
  <button>Button</button>
</div>

I think I found you a better solution: http://jsfiddle.net/epyFT/9/

HTML:

<div class="container">
    <h2>This is a very long heading that should wrap with ellipsis when too long, but have a button to it's right.</h2>
    <button>Hello.</button>
</div>

<div class="container">
    <h2>This is short.</h2>
    <button>Sup</button>
</div>

CSS:

.container {
    display: inline-block;
   max-width:100%;
   position: relative;
}
h2 {  
    padding-right: 200px;
    box-sizing: border-box;
    width: 100%;
    display: inline-block; 
    overflow: hidden; 
    white-space: nowrap; 
    text-overflow: ellipsis; 
    word-break: break-all; 
    word-wrap: break-word; 
}

button {
    position: absolute;
    width: 100px;
    right: 95px;
    top: 2em;
}

Here's another one

HTML:

<div class="container">
     <h2 class="oneline">This is the header that should never wrap and elipse if it doesn't fit</h2>
    <button>Button</button>
</div>
<div class="container">
     <h2 class="oneline">Header</h2>
    <button>Button</button>
</div>

CSS:

.container {
    width: 100%;
}
.oneline {
    display: inline-block;
    vertical-align: middle;
    max-width: 80%;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

JSFiddle

To align the buttons at the right side, add width: 80%; to .oneline.


I took Dan's great answer (which was only compatible with Webkit browsers), and made it compatible with Firefox and Internet Explorer 8+. Here is the fiddle: http://jsfiddle.net/hammerbrostime/9t5bX/1/

HTML:

<div class="container">
    <h2>This is a very long heading that should wrap with ellipsis when too long, but have a button to it's right.</h2>
    <button>Hello.</button>
</div>

<div class="container">
    <h2>This is short.</h2>
    <button>Sup</button>
</div>

CSS:

.container {
    display: inline-block;
    max-width:100%;
    position: relative;
}
h2 {  
    padding-right: 200px;
    box-sizing: border-box;
    width: 100%;
    display: inline-block; 
    overflow: hidden; 
    white-space: nowrap; 
    text-overflow: ellipsis; 
    word-break: break-all; 
    /* word-wrap: break-word;  taken out, caused issues in IE */
    max-width: -moz-calc(100% - 200px); /* FF hack */
}

button {
    position: absolute;
    width: 100px;
    right: 95px;
    top: 2em;
}

Need Your Help

Calling a java method from c++ in Android

java c++ android java-native-interface android-ndk

I'm trying to get a simple Java method call from C++ while Java calls native method. Here's the Java code:

Facebook Graph API - get ID for a URL?

facebook-graph-api reliability

This seems like a pretty obvious, basic thing to expect from the Graph API, but I'm having serious difficulty with it. All I want to do is get the ID for any particular URL. They have a method for ...