如何垂直对齐 div 中的文本?
我正在尝试找到将文本与 div 对齐的最有效方法。我尝试了一些方法,但似乎都不起作用。
.testimonialText {
position: absolute;
left: 15px;
top: 15px;
width: 150px;
height: 309px;
vertical-align: middle;
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
font-style: italic;
padding: 1em 0 1em 0;
}
<div class="testimonialText">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
I am trying to find the most effective way to align text with a div. I have tried a few things and none seem to work.
.testimonialText {
position: absolute;
left: 15px;
top: 15px;
width: 150px;
height: 309px;
vertical-align: middle;
text-align: center;
font-family: Georgia, "Times New Roman", Times, serif;
font-style: italic;
padding: 1em 0 1em 0;
}
<div class="testimonialText">
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
在现代浏览器中执行此操作的正确方法是使用 Flexbox。
有关详细信息,请参阅此答案。
请参阅下文,了解在旧版浏览器中运行的一些旧方法。
CSS 中的垂直居中
http://www.jakpsatweb.cz/css/css- Vertical-center-solution.html
文章摘要:
对于 CSS 2 浏览器,可以使用
display:table
/display:table-cell
将内容居中。JSFiddle 上还提供了示例:
可以将旧浏览器 (Internet Explorer 6/7) 的 hack 合并到样式中,并使用
#
隐藏较新浏览器的样式:The correct way to do this in modern browsers is to use Flexbox.
See this answer for details.
See below for some older ways that work in older browsers.
Vertical Centering in CSS
http://www.jakpsatweb.cz/css/css-vertical-center-solution.html
Article summary:
For a CSS 2 browser, one can use
display:table
/display:table-cell
to center content.A sample is also available at JSFiddle:
It is possible to merge hacks for old browsers (Internet Explorer 6/7) into styles with using
#
to hide styles from newer browsers:您需要添加
line-height
属性,并且该属性必须与div
的高度匹配。在你的情况下:事实上,您可以完全删除
height
属性。不过,这只适用于一行文本,所以要小心。
You need to add the
line-height
attribute and that attribute must match the height of thediv
. In your case:In fact, you could probably remove the
height
attribute altogether.This only works for one line of text though, so be careful.
2024 更新
自从我在下面最初的回答以来,事情已经发生了很大的变化,我想向您指出这篇关于如何在 CSS 中居中 div 的杰出 Josh Comeau 文章的方向:
https://www.joshwcomeau.com/css/center-a-div/
PS 他还有一些很棒的学习资源 CSS Flexbox和 CSS 网格
原始答案
这里是很棒的资源
来自http://howtocenterincss.com/:
使用 Flexbox
Inline 并保持这篇文章与最新技术保持同步,这是一种使用 Flexbox 居中某些内容的更简单的方法。 Internet Explorer 9 及更低版本不支持 Flexbox。
以下是一些很棒的资源:
带有浏览器前缀的 JSFiddle
另一个解决方案
这是来自zerosixthird 并允许您使用六行 CSS 将任何内容居中
Internet Explorer 8 及更低版本不支持此方法。
jsfiddle
先前的答案
一种简单且跨浏览器的方法,非常有用,因为标记答案中的链接稍微过时了。
Andy Howard - 如何在无序列表或 div 中垂直和水平居中文本
作为对于我从事的最后一个项目,我不太关心 Internet Explorer 7/6,我使用了一个稍微精简的版本(即删除了使其在 Internet Explorer 7 和 6 中工作的内容)。它可能对其他人有用...
JSFiddle
2024 Update
Things have moved on a lot since my original answer below, and I want to point you in the direction of this exceptional Josh Comeau article on how to center a div in CSS:
https://www.joshwcomeau.com/css/center-a-div/
P.S. he also has some great resources for learning CSS Flexbox and CSS Grid
Original answer
Here's a great resource
From http://howtocenterincss.com/:
Using Flexbox
Inline with keeping this post up to date with the latest tech, here's a much easier way to center something using Flexbox. Flexbox isn't supported in Internet Explorer 9 and lower.
Here are some great resources:
JSFiddle with browser prefixes
Another solution
This is from zerosixthree and lets you center anything with six lines of CSS
This method isn't supported in Internet Explorer 8 and lower.
jsfiddle
Previous answer
A simple and cross-browser approach, useful as links in the marked answer are slightly outdated.
Andy Howard - How to vertically and horizontally center text in an unordered list or div
As I didn't care much for Internet Explorer 7/6 for the last project I worked on, I used a slightly stripped down version (i.e. removed the stuff that made it work in Internet Explorer 7 and 6). It might be useful for somebody else...
JSFiddle
使用
display: flex
很容易。使用以下方法,div
中的文本将垂直居中:如果你愿意的话,水平:
您必须看到您需要的浏览器版本;在旧版本中,该代码不起作用。
It is easy with
display: flex
. With the following method, the text in thediv
will be centered vertically:And if you want, horizontal:
You must see the browser version you need; in old versions the code doesn’t work.
我使用以下内容轻松垂直居中随机元素:
HTML:
CSS:
这将我的
div
中的文本居中到 200px 高的外部div
的垂直中间位置。请注意,您可能需要使用浏览器前缀(例如我的例子中的-webkit-
)才能使其适用于您的浏览器。这不仅适用于文本,也适用于其他元素。
I use the following to vertically center random elements easily:
HTML:
CSS:
This centers the text in my
div
to the exact vertical middle of a 200px-high outerdiv
. Note that you may need to use a browser prefix (like-webkit-
in my case) to make this work for your browser.This works not only for text, but also for other elements.
试试这个,添加到父 div 上:
Try this, add on the parent div:
您可以通过将显示设置为“table-cell”并应用
vertical-align: middle;
来实现此目的:但是,根据我从 < 复制的摘录,并非所有版本的 Internet Explorer 都支持此功能。 a href="http://www.w3schools.com/cssref/pr_class_display.asp/%22w3%20schools%22" rel="noreferrer">http://www.w3schools.com/cssref/pr_class_display.asp 未经许可。
注意:值“inline-table”、“table”、“table-caption”、“table-cell”、“table-column”、“table-column-group”、“table- Internet Explorer 7 及更早版本不支持“行”、“表行组”和“继承”。 Internet Explorer 8 需要 !DOCTYPE。 Internet Explorer 9 支持这些值。
下表显示了 http://www.w3schools 中允许的显示值。 com/cssref/pr_class_display.asp。
You can do this by setting the display to 'table-cell' and applying a
vertical-align: middle;
:This is however not supported by all versions of Internet Explorer according to this excerpt I copied from http://www.w3schools.com/cssref/pr_class_display.asp without permission.
Note: The values "inline-table", "table", "table-caption", "table-cell", "table-column", "table-column-group", "table-row", "table-row-group", and "inherit" are not supported by Internet Explorer 7 and earlier. Internet Explorer 8 requires a !DOCTYPE. Internet Explorer 9 supports the values.
The following table shows the allowed display values also from http://www.w3schools.com/cssref/pr_class_display.asp.
如今(我们不再需要 Internet Explorer 6-8)我只会使用 CSS
display: table
来解决此问题(或display: flex
)。桌子:
弹性:
对于较旧的浏览器:
这是(实际上是)我最喜欢的解决方案(简单且浏览器支持非常好):
These days (we don't need Internet Explorer 6-8 any more) I would just use CSS
display: table
for this issue (ordisplay: flex
).Table:
Flex:
For older browsers:
This is (actually, was) my favorite solution for this issue (simple and very well browser supported):
Flexbox 非常适合我,将多个元素集中在父 div 内水平和垂直。
下面的代码将parent-div内的所有元素堆叠在一列中,使元素水平居中并居中。垂直。
我使用 child-div 将两个 Anchor 元素保持在同一行(行)上。如果没有 child-div,所有三个元素(Anchor、Anchor 和 Paragraph)都会堆叠在parent-div 的列内,彼此堆叠。这里只有 child-div 堆叠在parent-div 列内。
Flexbox worked perfectly for me, centering multiple elements inside parent div horizontally & vertically.
Code below stacks all elements inside of parent-div in a column, centering the elements horizontally & vertically.
I used the child-div to keep the two Anchor elements on same line (row). Without child-div all three elements (Anchor, Anchor & Paragraph) are stacked inside parent-div's column on top of each other. Here only child-div is stacked inside parent-div column.
您可以使用显示网格和放置项目中心:
You can use display grid and place-items center:
非常非常简单的解决方案,我在项目中多次使用它:
源代码
输出:
感谢您提出这个问题。乐于分享。
Very very simple solution, I used it in my projects many times which is:
Source Code
Output:
Thanks to ask this question. Happy to share.
这是最适合单行文本的解决方案。
如果行数已知,它也可以用于多行文本,并进行一些调整。
这是一个 JSFiddle。
Here is a solution that works best for a single line of text.
It can also work for multi-lined text with some tweaking if the number of lines is known.
Here is a JSFiddle.
您可以使用 Flexbox 在 div 内垂直居中对齐文本。
您可以在Flexbox 完整指南了解更多信息。
You can align center text vertically inside a div using Flexbox.
You can learn more about it at A Complete Guide to Flexbox.
检查这个简单的解决方案:
HTML
CSS
JSFiddle
Check this simple solution:
HTML
CSS
JSFiddle
这是我找到的最干净的解决方案(Internet Explorer 9+),并通过使用其他答案省略的
transform-style
添加了“关闭 0.5 像素”问题的修复。来源:垂直对齐仅 3 的任何内容CSS 行
This is the cleanest solution I have found (Internet Explorer 9+) and adds a fix for the "off by .5 pixel" issue by using
transform-style
that other answers had omitted.Source: Vertical align anything with just 3 lines of CSS
HTML:
CSS:
HTML :
CSS :
使用 CSS 网格为我做到了:
Using CSS grid did it for me:
有一种更简单的方法可以垂直对齐内容,而无需借助表格/表格单元格:
http://jsfiddle.net /bBW5w/1/
在其中我添加了一个不可见的(宽度=0)div,它假定容器的整个高度。
它似乎适用于 Internet Explorer 和 Firefox(最新版本)。我没有检查其他浏览器
当然还有CSS:
There's a simpler way to vertically align the content without resorting to table/table-cell:
http://jsfiddle.net/bBW5w/1/
In it I have added an invisible (width=0) div that assumes the entire height of the container.
It seems to work in Internet Explorer and Firefox (latest versions). I didn't check with other browsers
And of course the CSS:
解决不知道值的元素的简单方法:
HTML
CSS
A simple solution to an element of not knowing values:
HTML
CSS
根据 Adam Tomat 的回答,准备了一个 JSFiddle 示例 对齐 div 中的文本:
通过在 CSS 中使用 display:flex:
与另一个 示例以及博客文章。
According to Adam Tomat's answer there was prepared a JSFiddle example to align the text in div:
by using display:flex in CSS:
with another example and a few explanations in a blog post.
网格项上的自动边距。
与 Flexbox 类似,在网格项上应用 margin auto 使其在两个轴上居中:
Margin auto on a grid-item.
Similarly to Flexbox, applying margin auto on a grid-item centers it on both axes:
使用:
通过这个技巧,如果您不想使其居中,则可以对齐任何内容,添加“left:0”以左对齐。
Use:
With this trick, you can align anything if you don't want to make it center add "left:0" to align left.
HTML
CSS
HTML
CSS
使用 flex 时,请注意浏览器渲染的差异。
这对于 Chrome 和 Internet Explorer 都很有效:
与仅适用于 Chrome 的这个进行比较:
Using flex, be careful with differences in browsers' rendering.
This works well both for Chrome and Internet Explorer:
Compare with this one that works only with Chrome:
2024
浏览器现已添加对
align-content 上的代码>
(即使用
display:block
设置)。经过这么多年,您终于可以使用两个简单的 CSS 属性来做到这一点:此解决方案的一个很酷的优点是您可以使用
text-overflow: ellipsis
而无需向浏览器支持:Chrome 123、Edge 123、Firefox 125、Safari 17.4
2024
Browsers now added support for
align-content
on a default <div> (i.e. set withdisplay:block
). So after all these years you can finally do this with two simple CSS properties:A cool advantage of this solution is that you can use
text-overflow: ellipsis
without adding a child to your <div>.Browser Support: Chrome 123, Edge 123, Firefox 125, Safari 17.4
这是在 CSS 中使用
calc()
的div
模式中div
的另一种变体。这是可行的,因为:
position:absolute
为了在div
中精确放置div
,div
因为我们将其设置为20px
。calc(50% - 10px)
表示 50% - 高度的一半,用于使内部div
居中This is another variation of the
div
in adiv
pattern usingcalc()
in CSS.This works, because:
position:absolute
for precise placement of thediv
within adiv
div
because we set it to20px
.calc(50% - 10px)
for 50% - half the height for centering the innerdiv
这工作正常:
HTML
Sass
不带和带图像标签
(这是一种字体)。This works fine:
HTML
Sass
Without and with the image tag
<mat-icon>
(which is a font).您可以使用 css
flexbox
。You can use css
flexbox
.这将使文本垂直居中:
This will center text vertically: