html/css 工具栏中垂直居中的元素
我有两个按钮栏 - 每个按钮栏都包含链接,但其中一个还包含一个特定高度的提交按钮。带有提交按钮的所有元素都垂直居中。我希望另一个没有提交按钮的按钮栏看起来相同,所以我给了它一个明确的高度。但是,其中的链接与顶部而不是中间对齐。
这是怎么回事,如何制作高度一致且元素垂直居中的链接栏?
HTML:
<div class="link-bar">
<input type="submit" value="Save"/>
<a href="#">link</a>
<a href="#">link</a>
</div>
<div class="link-bar">
<a href="#">link</a>
<a href="#">link</a>
</div>
CSS:
input[type='submit'] {
width:100px;
height:40px;
border:solid red 1px;
}
.link-bar {
height:40px;
background:#EEE;
border:blue 1px solid;
margin:10px;
vertical-align: middle;
}
请参阅 jsFiddle示例
I have two button bars- each contains links, but one also contains a submit button of a certain height. The one with the submit button has all the elements vertically centered. I want the other button bar, without the submit button, to look the same, so I gave it an explicit height. However, the links within it align to the top instead of in the middle.
What's going on here, and how can I make link bars that are of a consistent height, with vertically centered elements?
HTML:
<div class="link-bar">
<input type="submit" value="Save"/>
<a href="#">link</a>
<a href="#">link</a>
</div>
<div class="link-bar">
<a href="#">link</a>
<a href="#">link</a>
</div>
CSS:
input[type='submit'] {
width:100px;
height:40px;
border:solid red 1px;
}
.link-bar {
height:40px;
background:#EEE;
border:blue 1px solid;
margin:10px;
vertical-align: middle;
}
See jsFiddle for example
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需添加等于
高度
的line-height
即可。默认情况下,该行上的任何文本都将垂直居中。如果将文本换行,则会出现异常。http://www.w3.org/wiki/CSS/Properties/line-height
我还删除了您的
vertical-align
因为它对于块级元素中的内容来说是多余的。 它仅适用于内联元素。演示:
http://jsfiddle.net/SLqbk/9/
Simply add
line-height
equal to theheight
. By default, any text on that line will be vertically centered. The exception occurs if you wrap the text to a new line.http://www.w3.org/wiki/CSS/Properties/line-height
I also removed your
vertical-align
as it's superfluous to content in block level elements. It only applies to inline elements.DEMO:
http://jsfiddle.net/SLqbk/9/
使用
line-height
属性。http://jsfiddle.net/SLqbk/7/
Use the
line-height
property.http://jsfiddle.net/SLqbk/7/
将其添加到您的CSS
http://jsfiddle.net/xYVRj/
Add this to your css
http://jsfiddle.net/xYVRj/
我给了@Sparky672答案,因为他正确地解决了我的具体问题并引导我走上了正确的道路,但我想分享我最终所做的事情,我认为总体上更有效:
而不是明确设置 < 的行高code>.link-bar a 尝试匹配容器和按钮的高度,我只是将工具栏中的所有元素设置为相同的行高,并使它们
display:inline-bock
。虽然使用 inline-block 的一般注意事项适用(请参阅 这里和此处),最终结果是您在工具栏中放入的所有元素的大小一致且垂直居中,需要管理的 CSS 更少:请参阅 更新了小提琴。
I gave @Sparky672 the answer because he correctly addressed my specific question and led me on the right path, but I want to share what I ended up doing, which I think is more effective overall:
Instead of explicitly setting the line-height of
.link-bar a
to try to match up to the container and button heights, I just set ALL the elements in the toolbar to the same line-height, and make themdisplay:inline-bock
. While the normal caveats of using inline-block apply (See here and here), the end result is consistent sizing and vertical centering for all the elements you throw in your toolbar, with less css to manage:See the updated fiddle.