2017-04-13 95 views
0

我想要在同一行中有兩個文本塊,其中較大字體的文本垂直居中居中,較小字體的文本具有與第字體較大的那個。 看看我的小提琴在哪裏大文本居中根據需要。儘管較小的文本也居中,但我希望它稍微低一些,以便感覺兩個文本塊在同一「行」上。 https://jsfiddle.net/phzzhevq/2/如何居中大字體文本並讓小字體文本居中對齊底部

HTML:

<div class="my-table"> 
    <div class="my-cell"> 
     <span class="large-text">Large text</span> 
    </div> 
    <div class="my-cell"> 
     <span class="small-text">Small text</span> 
    </div> 
</div> 

CSS:

.my-table { 
    display: table; 
    height: 80px; 
    background-color: red; 
} 

.my-cell { 
    display: table-cell; 
    vertical-align: middle; 
} 

.large-text { 
    font-size: 40px; 
} 

.small-text { 
    font-size: 20px; 
} 

圖片,說明所期望的結果: Desired result

回答

0

試試這個小補丁。請讓我知道如果這是你想要的。

.my-table { 
    display: table; 
    height: 80px; 
    background-color: red; 
} 

.my-cell { 
    display: table-cell; 
} 

.large-text { 
    font-size: 40px; 
    line-height:80px; 
} 

.small-text { 
    font-size: 20px; 
    line-height:80px; 
} 
+0

它的工作原理!只剩下一個小問題。 「我的表」變得大於80px。是否有可能使它具有80px的高度 – Mike

+1

而不是在**。my-table **中使用'display:table'使用'display:inline-block'。並在**。my-cell **中使用'display:inline-block'。 – SreenathPG

+0

無法通過使用內聯塊將其垂直對齊。看到小提琴:https://jsfiddle.net/phzzhevq/6/ – Mike

0

您可以通過以下方式實現。

.my-table { 
 
    display: table; 
 
    height: 80px; 
 
    background-color: red; 
 
} 
 

 
.my-cell { 
 
    display: table-cell; 
 
} 
 

 
.large-text { 
 
    font-size: 40px; 
 
    line-height:80px; 
 
} 
 

 
.small-text { 
 
    font-size: 20px; 
 
    line-height:80px; 
 
}
<div class="my-table"> 
 
    <div class="my-cell"> 
 
     <span class="large-text">Large text</span> 
 
    </div> 
 
    <div class="my-cell"> 
 
     <span class="small-text">Small text</span> 
 
    </div> 
 
</div>

我希望它能幫助。