文本垂直居中
链接到我的 jsbin
我遇到了一些意想不到的麻烦。我想将“Thing2”文本垂直居中。但是 css 属性 vertical-algin: middle
似乎不起作用。
所以我想知道两件事:
- 为什么它不起作用?
- 我这样做的方式(通过向每个类添加一个主类,然后添加一个样式类)是最简单的方法吗?
注意 这只是我总体想法的概念证明。显然类名和 ID 会改变。
Link to my jsbin
I am having a bit of unexpected trouble. I would like to vertically center the "Thing2" text. However the css property vertical-algin: middle
doesn't seem to be working.
So I'd like to know 2 things:
- Why isn't it working?
- Is the way I'm doing it (by adding a main class to each then a styling class) the easiest way to do this?
Note
This is just a proof of concept of an overall idea that I have. So obviously class names and ids will change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法以这种方式垂直对齐文本,请参阅 http://www.w3 .org/wiki/CSS/Properties/vertical-align
如果您只想垂直对齐单行文本,请尝试将容器 div 上的行高设置为等于 div 的高度。
例如,
这是一篇很好的文章,详细介绍了垂直对齐 html 元素的几种不同方法: http ://blog.themeforest.net/tutorials/vertical-centering-with-css/
如果您只支持最新的浏览器,您还可以使用新的表格和表格单元格显示样式,它们支持垂直对齐在同一个就像桌子一样。
例如,
请记住,这不适用于早期的浏览器,例如 Internet Explorer 6。
You can't vertically align text in that way, see http://www.w3.org/wiki/CSS/Properties/vertical-align
Try instead setting a line height on your container div equal to the height of the div if you just want to vertically align a single line of text.
e.g.
This is a nice post detailing a few different ways of vertically aligning html elements: http://blog.themeforest.net/tutorials/vertical-centering-with-css/
If you are only supporting the latest browsers you can also use the new table, and table-cell display styles, which support vertical alignment in the same way that tables do.
e.g.
Bear in mind this won't work on earlier browsers such as internet explorer 6 though.
我垂直居中的方式是将行高设置为容器的高度,
尝试将
line-height: 87.5px
添加到 .aboutThe way i vertically center things is to set the line-height to the height of the container
try adding
line-height: 87.5px
to .about尝试使用此 http://jsfiddle.net/YUruZ/
使用 CSS 样式
display
属性table
和table-cell
Try Using This http://jsfiddle.net/YUruZ/
Use CSS style
display
propertytable
andtable-cell