在 100% 高度的 div 中垂直居中文本?
我正在使用一个高度为父级 div 100% 的 div。
div 仅包含一行文本。
div 不能有固定的高度。
所以我的问题是。
如何使文本行垂直居中?
我尝试过使用:
display: table-cell;
line-height:200%;
如果很重要,则 div 是绝对定位的。
Current CSS
.requests {
position: absolute;
right: 0;
height: 100%;
width: 50px;
padding: 0 10px;
background-color: #69A4B5;
display: table-cell;
vertical-align: middle;
border-bottom-right-radius: 5px;
}
I am working with a div that is 100% of the parent divs height.
The div only contains a single line of text.
The div cannot have a fixed height.
So my question is.
How do I vertically center the line of text?
I have tried using:
display: table-cell;
line-height:200%;
If it is important the div is absolutely positioned.
Current CSS
.requests {
position: absolute;
right: 0;
height: 100%;
width: 50px;
padding: 0 10px;
background-color: #69A4B5;
display: table-cell;
vertical-align: middle;
border-bottom-right-radius: 5px;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
最好、最简单的方法(目前是
20152020)是使用 flexbox:就是这样 :D
查看这个工作示例:
旧答案: 如果还指定了 display: table-cell;,则可以使用 Vertical-align: middle;
工作示例:
如果它不起作用,您可以尝试将其父级设置为
display: table;
:Edit: 您有此方法以及此问题中涵盖的所有方法: 如何使用 CSS 垂直居中文本?
The best and easiest way to do it (currently in
20152020) is using flexbox:And that's it :D
Check-out this working example:
Old answer: You can use vertical-align: middle if you specify also display: table-cell;
Working example:
If it does not work you can try setting its parent as
display: table;
:Edit: You have this method plus all the methods covered on this question in this other question: How do I vertically center text with CSS?
这个答案不再是最好的答案......请参阅下面的弹性盒答案!
为了使其完美居中(如大卫的回答中所述),您需要添加负上边距。如果您知道(或强制)只有一行文本,则可以使用:
margin-top: -0.5em;
例如:
http://jsfiddle.net/45MHk/623/
最初接受的答案不会在文本中间垂直居中(它基于文本顶部)。因此,如果您的父母不是很高,它看起来根本不会居中,例如:
http://jsfiddle。净/45MHk/
This answer is no longer the best answer ... see the flexbox answer below instead!
To get it perfectly centered (as mentioned in david's answer) you need to add a negative top margin. If you know (or force) there to only be a single line of text, you can use:
margin-top: -0.5em;
for example:
http://jsfiddle.net/45MHk/623/
The originally accepted answer will not vertically center on the middle of the text (it centers based on the top of the text). So, if you parent is not very tall, it will not look centered at all, for example:
http://jsfiddle.net/45MHk/
试试这个http://jsfiddle.net/Husamuddin/ByNa3/
对我来说效果很好,
CSS
和 html
Try this one http://jsfiddle.net/Husamuddin/ByNa3/
it works fine with me,
css
and the html
由于它是绝对定位的,因此您可以使用 top: 50% 将其垂直居中对齐。
但随后您会遇到页面比您想要的更大的问题。为此,您可以对父 div 使用溢出:隐藏。这是我用来创建相同效果的:
CSS:
HTML:
Since it is absolutely positioned you can use top: 50% to vertically align it in the center.
But then you run into the issue of the page being bigger than you want it to be. For that you can use the overflow: hidden for the parent div. This is what I used to create the same effect:
The CSS:
The HTML:
我不同意,这是一个免费的 JS 解决方案,它可以工作:
I disagree, here's a JS free solution, which works:
尽管这个问题已经很老了,但这里有一个解决方案,适用于需要垂直居中的单行和多行(可以轻松地垂直和水平居中,如 演示。
HTML
CSS
Even though this question is pretty old, here's a solution that works with both single and multiple lines that need to be centered vertically (could easily be centered both vertically & horizontally as seen in the css in the Demo.
HTML
CSS
如果您知道文本的高度,则可以使用
top:50%
和margin-top:-x px
的组合,其中 x 是高度的一半你的文字。工作示例:
http://jsfiddle.net/Qy4yy/
If you know how tall your text is going to be you can use a combination of
top:50%
andmargin-top:-x px
where x is half the height of your text.Working example:
http://jsfiddle.net/Qy4yy/
只需用这样的表格包装您的内容:
just wrap your content with a table like this:
你试过 line-height:1em; 吗?我记得这是让它垂直居中的方法。
have you tried line-height:1em;? I recall that that's the way to get it to center vertically.
你尝试过垂直对齐:中间吗???
您可以在此处找到有关垂直对齐的更多信息: http://www.w3schools.com/ css/pr_pos_vertical-align.asp
Did you try vertical-align: middle ???
You can find more info on vertical-align here: http://www.w3schools.com/css/pr_pos_vertical-align.asp
垂直对齐、动态高度与绝对位置相结合,除了一些特殊条件之外,如果没有几行 JS(例如 jQuery)就不可能实现。 (在某些情况下,可以使用后端代码来解决,或者使用最小高度与合理的顶部或边距样式相结合,但并不完美)
我大多数情况下只在某些内容应该相对于其他内容“弹出”时才使用绝对位置浮动,我认为这是使用它的最佳方式,这样您就不必摆弄这样的事情。
无意冒犯,但这里的大多数答案都离题很远。
Vertical align, dynamic height combined with absolute position is except some special conditions not possible without a few lines of JS (eg. jQuery). (can possibly be solved with backend code in some cases, or min-height combined with sensible top or margin styles, but not perfect)
I mostly only use absolute position when something is supposed to "popup" in relation to something else which is in the float, I think that's the best way to use it so you don't have to fiddle with things like this.
No offense, but most answers in here are way off.
将行高设置为与 div 的高度相同将使文本居中。仅当有一根线时才有效。 (例如按钮)。
Setting the line height to the same as the height of the div will cause the text to center. Only works if there is one line. (such as a button).
现代解决方案 - 适用于所有浏览器和IE9+
caniuse - 浏览器支持。
示例: http://jsbin.com/rehovixufe/1/
Modern solution - works in all browsers and IE9+
caniuse - browser support.
Example: http://jsbin.com/rehovixufe/1/