css - 具有可变高度的菜单中的链接垂直居中

标签 css vertical-alignment

我有一个菜单和一个 CSS,如下所示,也可以在 http://codepen.io/Sofian777/pen/PqeyQa 上使用。

我需要垂直对齐菜单项,并尝试了垂直对齐的各种方法:中间和不同的显示:li和a上的设置(因为它在线适用于内联元素),但我无法让它工作.

有人知道如何实现这一目标吗?

编辑:这只是一个简化的场景,我认为就足够了,但事实并非如此,所以这是我的完整场景:http://codepen.io/Sofian777/pen/NqMOom (请参阅我对 GolezTrol 答案的评论中的简短描述)

<div id="menu"> 
      <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Menu Item</a></li>
            <li><a href="#">Categories</a></li>
            <li><a href="#">Contact</a></li>
      </ul>
</div> 

#menu {background: CornflowerBlue; 
  width: 100%;}

#menu ul {
    list-style-type: none; 
    text-align: center;
    height: 50px;
} 

#menu ul li {
    display: inline; 
    text-align: center;
}

#menu ul li a {
    text-decoration: none;
    display: inline-block; 
    width: 200px; 
    height: 100%;
    background: RoyalBlue;
    vertical-align: middle;
}

最佳答案

我们可以使用:before来帮助对齐。

   html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin:0;
    padding:0;
    border:0;
    outline:0;
    font-size:100%;
    font: inherit;
    vertical-align:baseline;
    background:transparent;
}


html {font-size:62.5%}
body {font-size: 16px; max-width: 1000px; margin: auto; line-height: 1.618;}
#menu {background: CornflowerBlue; 
  position: relative; 
  width: 100%; 
  padding-bottom: 5.6%; /* defining our 1000px : 56px ratio of the menubar */;}

#menu ul {
    position: absolute; right: 0; left: 0; top: 0; bottom: 0; /* This additional container is needed to not add the content to the padding-trick for our aspect ratio. */
    list-style-type: none; 
    text-align: center;
} 

#menu ul li {
    position: relative;
    display: inline; 
    text-align: center;
}

#menu ul li a {
    text-decoration: none;
    display: inline-block; 
    width: 20%; 
    height: 100%;
    background: RoyalBlue;
}
#menu ul li a:before{
    height: 100%;
width: 1px;
display: inline-block;
content: '';
vertical-align: middle;
}
    <div id="menu"> 
  <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">Menu Item</a></li>
        <li><a href="#">Categories</a></li>
        <li><a href="#">Contact</a></li>
   </ul>
  </div>

关于css - 具有可变高度的菜单中的链接垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31357691/

相关文章:

html - 使用 animate.css 自定义水平滚动

html - 将父级上方的子级向右对齐,不要重叠

html - 垂直对齐导航栏中的链接和 Logo

javascript - jQuery - 使用 anchor 链接将元素滚动到屏幕中间而不是顶部

html - 如何创建带有下拉菜单的垂直导航栏?

html - Ionic 在 ionic View 中将标题和顶部导航栏放在哪里?

html - 将按钮放在内联 block div 旁边

html - CSS HTML 自动内容格式化

链接中水平和垂直的 HTML-CSS 居中文本

html - 相对于其垂直中心定位 div