简体   繁体   English

水平li-垂直对齐元素

[英]Horizontal li - align element vertically

I'm using a <ul> with css display: inline; 我在CSS display: inline;使用<ul> display: inline; set to use as a toolbar/menu at the top of my page like this: 设置为用作页面顶部的工具栏/菜单,如下所示:

在此处输入图片说明

As you can see, the text vertically aligns perfectly, however the img on the left is too high, and the input box on the right is slightly low. 如您所见,文本在垂直方向上完美对齐,但是左侧的img太高,右侧的输入框略低。 I'm struggling to move them so they are perfectly vertically aligned. 我正在努力移动它们,以使其完全垂直对齐。

Below is my CSS: 以下是我的CSS:

.mynav {
    font-weight: 300;
    border: 1px solid #ccc;
    border-width: 1px 0;
    list-style: none;
    margin: 0;
    padding: 0;
    text-align: center;
    background-color: #0067b1;
}

.mynav li {
    display: inline;
      align-items: center;
        vertical-align:middle;
  line-height:35px;
}

.mynav a {
    color: white;
    display: inline-block;
    padding: 15px;
    font-size: 14pt;
    text-decoration: none;
}

.mynav input {
    margin-left: 25px;
    font-family: 'Open Sans', sans-serif;
    font-weight: 300;
    font-style: italic;
    -moz-border-radius: 10px;
    border-radius: 10px;
    border: none;
    padding: 5px;
    width: 200px;
    height: 25px;
    margin-top: 0px;
}

.mynav i
{
    font-weight: 300;
    color: white;
    font-size: 24px;
    margin-left: 15px;
}

And my HTML (if needed): 还有我的HTML(如果需要):

<ul class="mynav">
    <li><img src="http://www.advancegroupholdings.com.au/my_logo_nav.png" style="width: 100px; height:25px; padding-right:25px;" /></li>
    <li><a href="/">My Jobs</a></li>
    <li><a href="/about/">Obtain a Quote</a></li>
    <li><a href="/work/">Submit New Instruction</a></li>
    <li><a href="/clients/">Settings</a></li>
    <li><a href="/contact/">Help</a></li>
    <li><input type="text" placeholder="Enter Your Ref or Job Number"/><i class="fa fa-search"></i></li>
</ul>

As for the input box, it has a padding of 5 which I have tried to remove with no luck. 至于输入框,它的填充为5,我尝试删除该填充没有运气。 I have also set margin-top: 0px on the input, still no luck. 我还在输入上设置了margin-top: 0px ,仍然没有运气。

As for the logo, I'm also quite stumped. 至于徽标,我也很困惑。 I've tried adding margin-top: 5px , but it's not working. 我尝试添加margin-top: 5px ,但是它不起作用。

I also have found both these questions on StackOverflow: 我也在StackOverflow上找到了这两个问题:

<ul> horizontal nav bar... vertically-align element <ul>水平导航栏...垂直对齐的元素

css - verticaly align horizontal li CSS-Vertically对齐水平线

The first had no effect and the second wanted to change the CSS to display: flex , which for me gets rid of the horizontal bar, doesn't it? 第一个无效,第二个想要更改CSS以display: flex ,对我来说它摆脱了水平条,不是吗?

Can anyone help me get the logo and the input box (and the magnifying glass) vertically centered? 谁能帮助我使徽标和输入框(以及放大镜)垂直居中? Thank you in advance! 先感谢您!

Plunker here 在这里暴跌

Adding next two rules to your image will center it correctly. 将下两个规则添加到图像将使其正确居中。

display: inline-block;
vertical-align: sub; /* or text-bottom */

As for the input , it looks correctly centered to me. 至于输入,它看起来正确地居中。

I created a small bootstrap example for what you require. 我为您创建了一个小的引导示例。 Different to what you have but give the required output more or less. 与您所拥有的有所不同,但是或多或少地提供所需的输出。 Hopefully you can continue from here if this solution works for you. 如果此解决方案适合您,希望您可以从这里继续。

HTML HTML

  <div class="col-md-2"><img src="http://www.advancegroupholdings.com.au/my_logo_nav.png"  /></div>
    <div class="col-md-1">My Jobs</div>
    <div class="col-md-2">Obtain a Quote</div>
    <div class="col-md-2">Submit a New Instruction</div>
    <div class="col-md-1">Settings</div>
    <div class="col-md-1">Help</div>
    <div class="col-md-3"><input class="inputHeight" type="text" placeholder="Enter Your Ref or Job Number"/><i class="fa fa-search"></i></div>
  </div>
</div>

CSS CSS

 .navbar {
  width:80%;
   font-weight: 300;
    border: 1px solid #ccc;
    border-width: 1px 0;
    margin: 0;
    padding: 0;
    text-align: center;
    background-color: #0067b1;
    vertical-align:middle;
    line-height:50px;
}

.inputHeight {
    margin-left: 25px;
    font-family: 'Open Sans', sans-serif;
    font-weight: 300;
    font-style: italic;
    -moz-border-radius: 10px;
    border-radius: 10px;
    border: none;
    padding: 5px;
    width: 200px;
    height: 25px;
    margin-top: 0px;

}

Plunker Example 柱塞示例

In your specific example it's enough to add vertical-align:middle ; 在您的特定示例中,只需添加vertical-align:middle to <img> and to <a> , <i> elements! <img><a><i>元素! Also I'd recommend to have <li> elements as inline-block, not inline for more flexibility. 另外,我建议将<li>元素设置为内联块,而不是内联,以提高灵活性。

In general case, to align vertically the image height must be equal as other element's line-height in the line and vertical-align:middle has to be set. 通常,要垂直对齐,图像高度必须等于该行中其他元素的line-height ,并且必须设置vertical-align:middle

If image height is 25px then set: 如果图像高度为25px,则设置:

.mynav li, .mynav i {
    vertical-align:middle;
    line-height:25px;
}
img {
    vertical-align:middle;
    height: 25px;
    line-height:25px;
}

Here you go: https://plnkr.co/edit/Xf3uvvjuqykQMjnhVxes?p=preview 在这里,您可以前往: https : //plnkr.co/edit/Xf3uvvjuqykQMjnhVxes?p=preview

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

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