添加填充会导致垂直对齐问题

时间:2016-05-26 21:37:11

标签: html css

我有一个标题,左边有两个href,右边有一个导航栏。我有问题让所有东西都垂直对齐中间。我已经完成了关于垂直对齐问题的数百万个问题,但没有解决我的问题。

如果我在菜单标签中添加填充,则href不再由中间垂直对齐。如果菜单的填充为0,则所有内容都对齐。导航栏需要高度和宽度吗?

.menu {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    text-decoration: none;
    color: #fff;
    background: #19c589;
    display: block;
    float: right;
    margin-right: 100px;
    border: red 3px solid;
    padding: 10px 0;
    vertical-align: middle;
}

#header nav{
    display:inline;
}
#header {
    background: gray;
    padding: 28px 0 26px;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    width: 100%;

}
a[href="#top"] {
    margin-left:100px;
    margin-right:50px;
    vertical-align: middle;
    text-decoration: none;
    font-size: 20px;
    font-weight: bold;
}
a img{
   vertical-align:middle;
}
a, a:visited{
     color: white;
}
<body>
    <header id="header"> 
        <a href="#top">Test</a>
        <a href="">
            <img src="" alt="img" height="24" width="24">
        </a>
        <nav id="navbar">
        	<label class="menu">Menu</label>
        </nav>
    </header>

</body>
</html>

1 个答案:

答案 0 :(得分:1)

编辑小提琴这应该对您有用https://jsfiddle.net/5f3pbg8b/13/

注意:您不能将vertical-align: middle;与浮动元素一起使用,这是因为浮动元素与顶部对齐,您可以在此处阅读更多相关内容https://www.w3.org/TR/CSS21/visuren.html#floats

.menu {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    text-decoration: none;
    color: #fff;
    background: #19c589;
    position: absolute;/* new */
    right: 0;/* new */
    margin-right: 100px;
    border: red 3px solid;
    padding: 10px 0;
    margin-top: -10px;/* new */
}