[英]Vertically align span within a (link) tag within list element
I'm trying to vertically align a menu button I've made using span
tags next to a text field
I've made. 我试图垂直对齐我使用的
text field
旁边的span
标签制作的菜单按钮。 I would like to do it without applying a fixed margin to the top if possible. 如果可能的话,我希望不对顶部应用固定的边距。
Here is an example of the code I've written: https://jsfiddle.net/dLbdxa4j/3/ 这是我编写的代码的示例: https : //jsfiddle.net/dLbdxa4j/3/
What am I doing wrong? 我究竟做错了什么?
ul { list-style: none; height: 40px; } li { float: left; margin: 0 5px; } .line { width: 22px; height: 3px; background: black; display: block; margin: 4px 0 0; } input[type="text"] { height: 30px; }
<div> <ul> <li> <a> <span class="line"></span> <span class="line"></span> <span class="line"></span> </a> </li> <li> <input type="text" value="Search me!" /> </li> </ul> </div>
You can use inline-block
on the li
's with vertical-align
or make the parent flex
and use align-items
to align them vertically. 您可以在
li
上使用inline-block
进行vertical-align
也可以使父级flex
并使用align-items
将它们垂直对齐。 Also make the a
inline-block
so it's centered properly taking into consideration the vertical margin on .line
还要制作
a
inline-block
以便考虑到.line
上的垂直边距使其正确居中
ul { list-style: none; height: 40px; } li { margin: 0 5px; } .inlineBlock li { display: inline-block; vertical-align: middle; } .flex { display: flex; align-items: center; } .line { width: 22px; height: 3px; background: black; display: block; margin: 4px 0 0; } li a { display: inline-block; } input[type="text"] { height: 30px; }
<ul class="flex"> <li> <a> <span class="line"></span> <span class="line"></span> <span class="line"></span> </a> </li> <li> <input type="text" value="Search me!" /> </li> </ul> <ul class="inlineBlock"> <li> <a> <span class="line"></span> <span class="line"></span> <span class="line"></span> </a> </li> <li> <input type="text" value="Search me!" /> </li> </ul>
All you need to do is edit the css for list-items and the '.line' class: 您所需要做的就是编辑列表项的CSS和“ .line”类:
li {
display: inline-block;
vertical-align: middle;
}
.line {
width: 22px;
height: 3px;
background: black;
display: block;
margin: 0 0 4px 0;
display: block;
}
If you want to do it strictly through floating, then add the following rule to your css: 如果要严格通过浮动来执行此操作,则将以下规则添加到css中:
ul li:nth-child(2) {
margin-top: 5px;
}
You can also add a new class to the list-item with the search-field in it so that you don't have to depend on html hierarchy. 您也可以在列表项中添加一个带有搜索字段的新类,这样您就不必依赖html层次结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.