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] |
| |
---------------------------------------------------------
The text-overflow property in CSS deals with situations where text is clipped when it overflows the element's box. It can be clipped (i.e. cut off, hidden), display an ellipsis ('…', Unicode Range Value U+2026) or display an author-defined string (no current browser support for author-defined strings).
Definition and Usage. 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.
To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.
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
.container {
width: 100%;
}
.oneline {
display: inline-block;
vertical-align: middle;
max-width: 80%;
margin: 0;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
<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>
JSFiddle
To align the buttons at the right side, add width: 80%;
to .oneline
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With