简体   繁体   English

CSS3:如何在div中垂直对齐文本?

[英]CSS3: How to align text vertically within a div?

I have a div with a custom font and I'm trying to make it so the text has the same height as the div it's inside of. 我有一个具有自定义字体的div,我正在尝试制作它,以便文本与内部的div具有相同的高度。

I've been trying to find a way to override the vertical alignment of text without a div with no success. 我一直在尝试找到一种方法来覆盖没有div且没有成功的文本的垂直对齐方式。 I was hoping there was something like the text-indent method for this purpose. 我希望有类似此目的的文本缩进方法。

Here's a simple JsFiddle with my example. 这是带有我的示例的简单JsFiddle。

<!DOCTYPE html>
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Lekton' rel='stylesheet' type='text/css'>
<style>
.texto {
    background-color: #000;
    height: 24px;
    font-family: 'Lekton', sans-serif;
    color:white;
    font-size: 24px;
}
</style>
</head>
<body>
    <div class="texto">Test</div>
</body>
</html>

使用line-height CSS属性:

line-height : 24px;

Add a line-height with a property value that measures the height of the containing DIV 添加一个带有测量该DIV高度的属性值line-height

 <style> .texto { background-color: #000; height: 24px; font-family: 'Lekton', sans-serif; color: white; font-size: 24px; line-height: 24px; /* This line solves your problem */ } </style> </head> <body> <div class="texto">Test</div> </body> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM