垂直对齐跨度内的文本

时间:2013-07-31 00:56:32

标签: html css css3

JSFiddle here - http://jsfiddle.net/2R36y/

相关范围代码 -

CSS

nav ul li a span {
    font: 30px"Dosis", sans-serif;
    text-transform: uppercase;
    position: relative;
    left: 120px;
    width: inherit;
    height: inherit;
    display: none;
    text-align: left;
}

HTML

<nav>
    <ul>
        <li> <a href="#">
                <span>Home</span>
            </a>
        </li>
        <li> <a href="#">
                <span>About</span>
            </a>
        </li>
        <li> <a href="#">
                <span>Portfolio</span>
            </a>
        </li>
        <li> <a href="#">
                <span>Contact</span>
            </a>
        </li>
    </ul>
</nav>

2 个答案:

答案 0 :(得分:6)

一种方法是使用display: table-cell;vertical-align: middle; - 您也可以使用line-height,因为<span>具有固定的高度。

nav ul li a:hover span {
    font: 30px"Dosis", sans-serif;
    text-transform: uppercase;
    position: relative;
    left: 120px;
    width: inherit;
    height: inherit;
    display: table-cell;
    vertical-align: middle;
    text-align: left;
}

http://jsfiddle.net/2R36y/1/

答案 1 :(得分:0)

使用display:table

nav ul li {
    list-style: none;
    margin: 0 0 0 0;
    display: table; 
}

nav ul li a:hover span {
    display: table-cell;
    vertical-align: middle;
    background-color: #000000;
}
相关问题