简体   繁体   English

如何使顶部导航与徽标垂直居中?

[英]How to make top navigation vertically center with the logo?

I am trying to make the top menu vertically center without assigning value like margin-top: 50px; 我试图使顶部菜单垂直居中,而不分配像margin-top: 50px;这样的值margin-top: 50px; because some of my friends say this is not the ideal approach. 因为我的一些朋友说这不是理想的方法。

 /* Nav Section */ .nav { width: 100%; padding: 0; margin: 0; } .nav-contain { width: 1100px; margin: 0 auto; padding: 0; overflow: hidden; } .logo { z-index: 10; display: inline-block; background: #2980B9; padding: 65px 50px 35px 45px; font-size: 36px; line-height: 42px; color: #fff; text-align: center; } .logo a { color: #FFFFFF; text-decoration: none; } #medical { display: block; text-transform: uppercase; } .menu { padding: 0; margin: 0; float: right; display: table-cell; position: relative; } .menu a { text-decoration: none; color: #505050; font-weight: bold; } .menu ul { padding: 0; margin: 0; float: left; top: 50%; } .menu ul ul { padding: 0; margin: 0; } .menu ul li { float: left; display: block; margin-left: 45px; } .menu ul ul { position: absolute; left: -999px; } .menu ul li:hover ul { left: auto; } .menu ul li ul li { margin-left: 0; float: none; margin-top: 15px; } 
 <div class="nav"> <div class="nav-contain"> <div class="logo"> <a href="#"><span id="medical">Medical</span><span id="company"> Company</span></a> </div> <!-- Logo --> <div class="menu"> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a> <ul class="dropdown"> <li><a href="#">Sample</a></li> <li><a href="#">Sample</a></li> </ul> </li> <li><a href="#">Gallery</a></li> <li><a href="#">Prices</a></li> <li><a href="#">Contact</a></li> </ul> </div> <!-- Menu --> </div> <!-- Nav Contain --> </div> <!-- Nav --> 

Remove float:right on .menu , and set both .logo and .menu to this: 删除.menu上的float:right ,并将.logo.menu设置为此:

.logo, .menu {
    display: inline-block;
    vertical-align: middle;
}

If you need .menu to stay on far right side, also add this: 如果您需要.menu停留在最右边,请添加以下内容:

.nav-contain {
    text-align: justify; 
}
.nav-contain:after{
    content: '';
    display: inline-block;
    width: 100%;
}

How it works: 这个怎么运作:

  1. Set text-align: justify; 设置text-align: justify; will line up the two inner inline blocks to the left and right edges of the container. 将两个内部内联块对齐到容器的左右边缘。

  2. Create an invisible 100% width element by using :after or :before pseudo-element stretching the box to occupy the entire space of the container. 通过使用:after:before伪元素创建一个不可见的100%宽度的元素,将元素拉伸框以占据容器的整个空间。 Otherwise inline element occupies only the space bounded by the tags that define the inline element. 否则,内联元素仅占据定义内联元素的标签所界定的空间。

One easy way to center here is to use Flexbox: 在此处居中的一种简单方法是使用Flexbox:

.nav-contain {
    /* what is already there */
    display: flex;
    align-items: center;
}

Beware of browser support (check caniuse.com to see if the compatibility level is acceptable to you). 提防浏览器支持(请检查caniuse.com以查看兼容性级别是否可接受)。

This is superior to the margin-top solution as it ensures that you won't have to manually change that 50px each time the size of the image or anything else in the navbar changes. 这优于边距最高的解决方案,因为它可以确保您不必每次图像大小或导航栏中的任何其他内容更改时都手动将其更改为50px。

Try: 尝试:

.menu > ul > li {
    min-height:50px;
    display: table;
}
.menu > ul > li > a {
    display: table-cell;
    vertical-align: middle;
}

http://jsfiddle.net/rawat/4h05rq2s/ http://jsfiddle.net/rawat/4h05rq2s/

Since your navbar remains the same height the whole time, I suggest you give the .nav-contain the following code: 由于导航栏始终保持相同的高度,因此建议您为.nav-contain以下代码:

.nav-contain {
    width: 1100px;
    margin: 0 auto;
    line-height: 184px;
    padding: 0;
    overflow: hidden;
}

Note the line-height. 注意行高。 This will, once you smaller the available width of your device, result in a probably not so nice looking huge navigation bar. 一旦减小设备的可用宽度,这将导致看起来不太好看的巨大导航栏。 For this, I suggest media queries. 为此,我建议媒体查询。

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

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