2017-02-13 51 views
4

我想根據屏幕中心居中顯示標題,而容器不佔用屏幕寬度的100%。使用偏移量居中截斷文本 - 僅限CSS-

我還需要文本被截斷,不想在右側留下填充。

This is what I've got so far - JSFiddle。您可以看到黃色div中的文本與下面的文本沒有對齊。如果我在黃色div上添加一個填充權限,在調整大小時,文本將不會佔用黃色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

我終於想通這一個,並再次僞幫我achive 不可能事情

通過添加一個width和一個min-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>


回答時更新

找到另一種方式其中有一個左側和右側項目

這是一個好處,它不需要預定義的寬度。

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內容的最好方式內Flexbox的容器是這是棘手使用absolute位置,以便它相對於視口居中。但是,使用絕對位置元素難以獲得text-overflow: ellipsis;

這是我發現的最接近..

<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