2015-07-11 343 views
2

我有一个菜单和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; 
} 

回答

1

我们可以使用: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>

+0

OMG这是非常完美的解决我的问题。谢谢,我不希望这种特殊的罕见情况会发现一个完全自动化的解决方案。非常感谢。我不能upvote我没有15声望呢。我会再回来:) – Sofian

0

使用line-height

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

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

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

#menu ul li a { 
    text-decoration: none; 
    display:block; 
    width: 200px; 
    background: RoyalBlue; 
    line-height: 50px; 
} 

演示:JSFiddle

+1

感谢你们了,你的答案是非常基本的Golez TROL的谁,甚至提高了它周围的代码。 – Sofian

1

主要的变化是设置线高度为t元素的高度,在这种情况下为50px。

此外,您可以将一些属性移动到链接。通过这样做,ulli大小与链接,你不需要重复这些属性:

#menu { 
 
    background: CornflowerBlue; 
 
} 
 
#menu ul { 
 
    list-style-type: none; 
 
    text-align: center; 
 
} 
 
#menu ul li { 
 
    display: inline-block; 
 
} 
 
#menu ul li a { 
 
    text-decoration: none; 
 
    display: inline-block; 
 
    width: 200px; 
 
    background: RoyalBlue; 
 
    line-height: 50px; 
 
}
<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>

+0

感谢您的回答,这是我在这里描述的完美。其实我需要的是更复杂的,我想我的问题保持简单,但我现在看到,我需要把整个故事。我即将创建一个流畅的布局,其中菜单栏和其他元素必须在每个屏幕尺寸上保持一定宽度和高度的比例。我使用填充底部技巧来实现这一点,调整浏览器的大小以查看行为。现在我只需要将文本放在块的中心。有关这种情况的任何想法? http://codepen.io/Sofian777/pen/NqMOom – Sofian

+1

我想给你一个投票,但我没有15声望呢。所以,谢谢你的回答,完美地回答了我最初的简单问题。 – Sofian