2013-04-20 16 views
16

Gerekirse, elips kullanarak aynı hatta bir başlık ve düğmeye ihtiyacım var. Ben çıkışını istiyorumSarma olmadan elek taşması elipsiyle sıvı genişliği üstbilgisini nasıl yapabilirim?

http://jsfiddle.net/epyFT/1/ şöyle görünecek şekilde: Burada

keman var

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

Ya

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

Veya daha küçük bir pencere ile

:

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

Düğme asla bir sonraki satıra gitmemelidir. Bunu nasıl yapabilirim?

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 doğru çekmeye orada ikinci bir (sabit genişlik) butonuna almak için

Ekstra kredi.

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

Eğer herhangi bir hedef tarayıcı gereksinimleri var mı? – Dan

+0

Ne yazık ki, 'text-overflow: ellipsis', sadece blok konteynerleri ile çalışır. http://dev.w3.org/csswg/css-ui/#text-overflow –

+0

@Dan, her zamanki modern olanlar - Chrome, Safari, IE 8, Firefox, vb. – Ryan

cevap

18

Belki de böyle bir şey yardımcı olur? http://jsfiddle.net/epyFT/3/

Düğmenin, kapsayıcının sarılı olmayan genişliğine hizalanmasını nasıl sağlayacağımı bilmiyorum ve yalnızca hücreler için yüzde genişliklerini kullanıyorum.

Yeni kod:

<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> 

CSS:

<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:

İşte
.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; 
} 
+1

Harika! Fazladan hücre sargısına ihtiyacım yoktu, ama kusursuz çalıştı! Sağ tarafta ekstra bir statik genişlikte boşlukta bile sıkışabilirdim. Güncel keman: http://jsfiddle.net/epyFT/7/ Tekrar teşekkürler! – Ryan

+0

Ayrıca, eğer H2 sarılıysa, sağ-kenarlı ya da göreceli konumlandırmalı bir dolguya sahip olabilir ve düğme o dolgu alanına mutlak olarak yerleştirilebilir. – Dan

+0

Tablo hücresi ile göreceli/mutlak konumlandırma konusunda dikkatli olun .. tarayıcılarda garip olur. – Dan

1

diğeri

HTML bulunuyor JSFiddle

.container { 
    width: 100%; 
} 
.oneline { 
    display: inline-block; 
    vertical-align: middle; 
    max-width: 80%; 
    margin: 0; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
} 
sağ tarafındaki düğmeleri hizalamak .oneline için width: 80%; ekleyin.

+0

Bunu işe alamıyorum. Kap gerçekten% 100 genişliğini doldurmuyor. İşte bir keman: http://jsfiddle.net/zRkmu/ – Ryan

+0

@Ryan Sınır eklediğimde: 1px katı kırmızı; '' '.container' için sağ kenara kadar uzanır. Neyi başarmak istiyorsun? Düğmeler temizlenmeli mi? FF 20 kullanıyorum, btw. –

+0

% 100 genişlik hakkında ne demek istediğini görüyorum. Ancak bu sadece sabit genişlikli bir başlık varsa çalışır. Başlığın sıvı olmasını istiyorum (yani olabildiğince uyumlu). Güncelleştirilmiş keman: http://jsfiddle.net/zRkmu/3/ – Ryan

7

Sana daha iyi bir çözüm buldum: 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: I (Webkit tarayıcıları ile uyumludur oldu) Dan büyük bir cevabı aldı

.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; 
} 
0

ve Firefox ve Internet Explorer 8+ ile uyumlu hale getirildi. İşte keman olduğunu: 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; 
}