2017-02-13 8 views
4

私は画面の中央に合わせてタイトルを中央に置いていますが、コンテナは画面の幅の100%を占めていません。オフセット付きの切り詰めテキストの中央揃え - CSSのみ

また、テキストを切り捨てる必要があります。右側にパディングを残したくない場合もあります。

This is what I've got so far - JSFiddle。黄色のdivのテキストがテキストの下線に沿って並んでいないことがわかります。黄色のdivにpadding-rightを追加すると、サイズ変更時に黄色のdivの100%がテキストに表示されません。助言がありますか?

HTML

<div class="cont"> 
    <div class="left-h"> 
     place holder 
    </div> 
    <div class="middle-h"> 
     my very long long title goes here 
    </div> 
</div> 
<div class="real-center"> 
    my very long long title goes here 
</div> 

CSS

.cont{ 
    width: 100%; 
    border: 1px solid black; 
    display: flex; 
    text-align: center; 
} 

.left-h{ 
    flex-basis: 150px; 
    background-color: #e5e5e5; 
} 

.middle-h{ 
    background-color: yellow; 
    flex-grow: 1; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
} 

.real-center{ 
    width: 100%; 
    text-align:center; 
} 
+0

スニペット? – Banzay

+0

実際の場所は、次のようなものです。私は視覚的な参照としてのみ追加しますが、タイトルは黄色のdivにのみ表示する必要があります –

+0

あなたはそれが必要ですか?:https://jsfiddle.net/banzay52/pzbk869h/ – Banzay

答えて

3

これは私が見つけた最接近..ですwidthmin-widthを追加すると、必要に応じてテキストを中央に維持します

.middle-h::after{ 
    content: ''; 
    display: inline-block; 
    width: calc(100% - 150px); 
    max-width: 148px;   /* 150px - 2px border */ 
} 

Fiddle sample

スタックはに答えるさらに別の方法を見つけた

.cont{ 
 
    width: 100%; 
 
    border: 1px solid black; 
 
    display: flex; 
 
    text-align: center; 
 
} 
 
.left-h{ 
 
    flex-basis: 150px; /* width/height - initial value: auto */ 
 
    background-color: #e5e5e5; 
 
} 
 
.middle-h{ 
 
    background-color: yellow; 
 
    flex: 1 0; 
 
    white-space: nowrap; 
 
    overflow: hidden; 
 
    text-overflow: ellipsis; 
 
} 
 
.middle-h::after{ 
 
    content: ''; 
 
    display: inline-block; 
 
    width: calc(100% - 150px); 
 
    max-width: 148px;   /* 150px - 2px border */ 
 
} 
 

 
.real-center{ 
 
    width: 100%; 
 
    text-align:center; 
 
}
<div class="cont"> 
 
    <div class="left-h"> 
 
    place holder 
 
    </div> 
 
    <div class="middle-h"> 
 
    my very long long title goes here 
 
    </div> 
 
</div> 
 
<div class="real-center"> 
 
     my very long long title goes here 
 
</div>


更新

スニペット左右の両方のアイテムを持っていた

これはあくまでも1つで、あらかじめ定義された幅は必要ありません。

Fiddle sample

スタックは、真のタイトルでそれらのどの

.cont { 
 
    display: flex; 
 
    text-align: center; 
 
    border: 1px solid black; 
 
} 
 
.cont > * { 
 
    white-space: nowrap; 
 
    padding: 2px 4px; 
 
    background: lightgray; 
 
} 
 
.cont > .center { 
 
    background: yellow; 
 
    overflow: hidden; 
 
    text-overflow: ellipsis; 
 
} 
 

 
.cont .left, 
 
.cont::after { 
 
    content: ''; 
 
    flex: 1; 
 
} 
 

 
.real-center{ 
 
    width: 100%; 
 
    padding: 2px 4px; 
 
    text-align:center; 
 
}
<div class="cont"> 
 
    <div class="left"> 
 
    place holder 
 
    </div> 
 
    <div class="center"> 
 
    my very long long title goes here 
 
    </div> 
 
</div> 
 

 
<div class="real-center"> 
 
     my very long long title goes here 
 
</div>

0

あなたは、ビューポート内middle-hの内容を中心にしたいとフレキシボックス容器内as explained here最良の方法は、にあるため、これはトリッキーですabsoluteの位置を使用して、ビューポートに対して中心になるようにします。しかし、text-overflow: ellipsis;を絶対位置要素で処理するのは難しいです。私は最終的にこれを考え出した、と再び擬似によってachive 不可能物事に私に

を助けた

<div class="cont"> 
    <div class="left-h"> 
     place holder 
    </div> 
    <div class="middle-h"> 
     <span class="abs-center">my very long long title goes here</span> 
    </div> 
</div> 
<div class="real-center"> 
    my very long long title goes here 
</div> 


.abs-center { 
    position: absolute; 
    left: 150px; 
    right: 150px; 
    z-index: 1; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    white-space: nowrap; 
    display: inline-block; 
    margin-left: 8px; 
    margin-right: 8px; 
} 

http://www.codeply.com/go/S2sw2jrn7p

関連する問題