CSS(Cascading Style Sheets)是一种用于描述HTML或XML(包括SVG、MathML等各种XML方言)文档样式的样式表语言。文本居中是指将文本内容在水平或垂直方向上居中对齐。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>水平居中示例</title>
<style>
.centered-text {
text-align: center;
}
</style>
</head>
<body>
<div class="centered-text">
这段文本是水平居中的
</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>垂直居中示例</title>
<style>
.container {
height: 200px;
display: flex;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<p>这段文本是垂直居中的</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>水平和垂直居中示例</title>
<style>
.container {
height: 200px;
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<p>这段文本是水平和垂直居中的</p>
</div>
</body>
</html>
通过以上内容,你应该能够理解CSS设置文本居中的基础概念、优势、类型、应用场景以及常见问题的解决方法。
领取专属 10元无门槛券
手把手带您无忧上云