简体   繁体   English

如何在固定高度的中心垂直对齐文本?

[英]how to align the text vertically in center in a fixed height?

I have an image of height=79px and width=138px and I want to align the text vertically center through <li> . 我的图像的height=79pxwidth=138px ,我想通过<li>将文本垂直居中对齐。 For this, I write my css as : 为此,我将css编写为:

#left-container .col1 li
{
    background:url(../images/button.png) no-repeat top center;
    height:79px;
    line-height:79px;
    font-weight:bold;
    font-size:1.5625em; /* 25px */
    text-align:center;
    text-transform:uppercase;
    margin-bottom:1px;
    position:relative;  
}

This is okay with single word, eg Qty. 单个单词(例如Qty. , but not with 2 words, eg Item Options , Hold Selected , etc. ,但不能包含2个字,例如Item OptionsHold Selected等。

So, can you please suggest whether I should create separate classes for each button to adjust line heights separately, or is there any other unified way to adjust all the line heights in just one class? 因此,您能否建议我是否应该为每个按钮创建单独的类来分别调整行高,或者是否有其他统一的方法来仅调整一个类中的所有行高?

If you set the line-height property to 79px, each line of text will be 79px height. 如果将line-height属性设置为79px,则每行文本的高度将为79px。

To vertical center a text inside an element, you have to use the vertical-align property, together with the display:table property for the container element. 要使文本在元素内垂直居中,必须使用vertical-align属性以及容器元素的display:table属性。 The idea is to make the element work like a table. 想法是使元素像表一样工作。

So you need a container element acting like a table, an inner element acting like a table-row, and inside that, elements acting like table cells. 因此,您需要一个像表一样工作的容器元素,一个像表行一样工作的内部元素以及其中的像表单元格一样工作的元素。

I managed to let your .col1 acting like a table: 我设法让您的.col1像表一样工作:

#left-container .col1 {
    display:table;
}

Then your ul acting like table rows: 然后您的ul就像表行一样:

#left-container .col1 ul {
    display:table-row;

}

And then each li item acting like a table cell, adding this CSS to yours: 然后,每个li项都像一个表格单元一样,将以下CSS添加到您的CSS中:

#left-container .col1 li  {  
    display:table-cell;
    vertical-align:middle; 
}

I removed the line-height property. 我删除了line-height属性。

Note that you need now to have multiple ul for each row you want to have in the "table", so if you want to have the same structure as before, you need to have something like this: 请注意,现在您需要在“表”中的每一行具有多个ul,因此,如果要具有与以前相同的结构,则需要具有以下内容:

<div id="left-container">
    <div class="col1">
        <ul>
            <li>List item 1 </li>
        </ul>
        <ul>
            <li>List item 2 </li>
        </ul>
        <ul>
            <li>List item 3 </li>
        </ul>
        <ul>
            <li>List item 4 </li>
        </ul>
        <ul>
            <li>List item 5 </li>
        </ul>
    </div>
</div>

UPDATE UPDATE

Look at it here: http://jsfiddle.net/W2H6a/22/ 在这里查看: http : //jsfiddle.net/W2H6a/22/

try this: 尝试这个:

HTML HTML

<div id="left-container">
    <div class="col1">
        <ul>
        <li>List item 1 </li>
        <li>List item 2 </li>
        <li>List item 3 </li>
        <li>List item 4 </li>
        <li>List item 5 </li>
        </ul>
    </div>
</div>

CSS: CSS:

#left-container .col1 li  { 
    height:79px; 
    width:138px;
    line-height: 70px;
    font-weight:bold;   
    text-align:center;
    text-transform:uppercase;
    margin-bottom:1px;   
    border: 1px solid green;  /* Just to check */
 }

See the demo here: http://jsfiddle.net/W2H6a/3/ 在此处查看演示: http : //jsfiddle.net/W2H6a/3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM