简体   繁体   English

Html,CSS垂直对齐菜单

[英]Html, CSS vertical align a menu

I'm trying to make a menu, but I'm having trouble centering the text to the middle. 我正在尝试制作一个菜单,但是我无法将文本居中到中间。

<div id="menucontainer">
            <ul id="menu">
                <li>@Html.ActionLink("Home", "Index", "Home")</li> | 
                <li>@Html.ActionLink("About", "About", "Home")</li> | 
                <li>@Html.ActionLink("Projects", "Projects", "Home")</li> | 
                <li>@Html.ActionLink("Forum", "Forum", "Home")</li>
            </ul>
        </div>


#menu
{
    background-image: url("../Content/img/bg-menu.png");
    height:50px;
    padding-left:30px;
    padding-right:25px;
    text-align:center;
    border-radius:20px;
    background-repeat:repeat;
    display:block;
    list-style: none;
    margin-left:55%;
    position:absolute;
    color:#aa4dc6;
}
#menu li 
{   
    display:inline;
    padding:5px 10px 9px 10px;
}
#menu a
{
    text-decoration:none;
    color:#606060;
    text-decoration:none;
    text-transform:capitalize;
    font-size:19px;
    font-weight:bold;
    font-family: 'Open Sans', sans-serif;
}

在此输入图像描述

There are a few solutions to this problem. 这个问题有一些解决方案。

Here's a jsFiddle that demonstrates all of the solutions below. 这是一个jsFiddle ,演示了下面的所有解决方案。

First, you can try setting the display property to table-cell for the list items in the menu, and then you can use vertical-align:middle to center the text. 首先,您可以尝试将display属性设置为菜单中列表项的table-cell ,然后可以使用vertical-align:middle将文本居中。

The solution would probably work well, because instead of floating the list items, you can use CSS to make them behave like table cells. 解决方案可能会运行良好,因为您可以使用CSS来使其表现得像表格单元格,而不是浮动列表项目。

ul#menu {
    display: table-row;
}
ul#menu li {
    display: table-cell;
    vertical-align: middle;
}

Second, you can set the line-height property to the height of the list item. 其次,您可以将line-height属性设置为列表项的高度。 Be careful with this one though, because if the text wraps it will break the layout: 但要小心这个,因为如果文本包装它将打破布局:

ul#menu li {
    height: 30px;
    line-height: 30px;
}

Lastly, instead of setting a fixed height, you can use padding to set the height of the list items. 最后,您可以使用填充来设置列表项的高度,而不是设置固定高度。 Assuming you use the same padding for the top and bottom, the text should be aligned in the center: 假设您在顶部和底部使用相同的填充,文本应在中心对齐:

ul#menu li {
    padding: 15px 5px 15px 5px;
}

给它与元素height相同的line-height height ,文本应正确对齐

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

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