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">
这段文字是垂直居中的
</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">
这段文字是水平和垂直居中的
</div>
</body>
</html>
text-align
、display
、justify-content
、align-items
)设置正确。通过以上方法,可以有效地实现CSS文字的居中显示,并解决常见的居中显示问题。