简体   繁体   English

HTML5 和 CSS 3 - Div 文本垂直居中对齐

[英]HTML5 and CSS 3 - Div text Vertical Align Middle

Please help,请帮忙,

i want to align the header menu/nav links to vertically align.我想将 header 菜单/导航链接垂直对齐。 See:看:

http://hyindia.com/demo/myoffshore/index.html http://hyindia.com/demo/myoffshore/index.html

See the CODE here:请在此处查看代码:

nav ul { list-style-type:none; padding:0px; margin:0px; float:left; width:100%;}
nav ul li { float:left; width:119px; height:66px;}
nav ul li a { 
 float:left;
 width:119px;
 height:66px;
 font:bold 15px 'Myriad Pro';
 color:#fff;
 text-shadow:1px 1px #1f1f1f;
 text-align:center;
}

<nav>
<ul>
    <li><a href="#" class="nav1">HOME</a></li>
    <li><a href="#" class="nav2">HEALTH INSURANCE</a></li>
    <li><a href="#" class="nav3">LIFE INSURANCE</a></li>
    <li><a href="#" class="nav4">OVERSEAS MORTGAGES</a></li>
    <li><a href="#" class="nav5">ESTATE PLANNING</a></li>
    <li><a href="#" class="nav6">BANKING</a></li>
    <li><a href="#" class="nav7">WEALTH MANAGEMENT</a></li>
    <li><a href="#" class="nav8">QROPS</a></li>
</ul>

Since some of your nav items have text spanning several rows you won't be able to use the classic line-height-trick (which would be to set the line-height equal to the height).由于您的某些导航项的文本跨越多行,您将无法使用经典的行高技巧(将行高设置为等于高度)。

Instead I'd suggest changing your menu styling to use display: table/table-row/table-cell since tables are excellent at vertically aligning things in the middle.相反,我建议更改菜单样式以使用display: table/table-row/table-cell ,因为表格非常适合在中间垂直对齐内容。

What you need to do is to change your entire nav styling to this:您需要做的是将整个nav样式更改为:

nav {
    display: table;
    width: 100%;
}

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

        nav ul li {
            display: table-cell;
            vertical-align: middle;
        }

            nav ul li a {
                display: block;
                padding: 5px 10px;
            }

Remove all the floats and widths + heights (using padding on the a instead) etc (what I have above is all you should have).删除所有浮动和宽度 + 高度(改为在a上使用填充)等(我上面有的就是你应该拥有的)。

You'll also need to move the actual background styling from the a s to the li s since the a s won't be equal in height any more (but the li s will).您还需要将实际背景样式从a s 移动到li s,因为a s 的高度不再相等(但li s 会)。

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

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