CSS(Cascading Style Sheets,层叠样式表)是一种用于描述HTML或XML(包括SVG、MathML等各种XML方言)文档样式的样式表语言。在CSS中,有多种方法可以使文字居中。
文字居中是指将文本内容在水平方向和/或垂直方向上居中对齐。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Horizontal Centering</title>
<style>
.centered-text {
text-align: center;
}
</style>
</head>
<body>
<div class="centered-text">
This text is horizontally centered.
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vertical Centering</title>
<style>
.container {
height: 200px;
display: flex;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<p>This text is vertically centered.</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Horizontal and Vertical Centering</title>
<style>
.container {
height: 200px;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<p>This text is centered both horizontally and vertically.</p>
</div>
</body>
</html>
通过这些方法,你可以轻松地在网页上实现文字的居中对齐。
领取专属 10元无门槛券
手把手带您无忧上云