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>水平居中示例</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;
justify-content: 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;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<div class="container">
这段文本是水平和垂直居中的。
</div>
</body>
</html>
通过以上示例代码和参考链接,你可以了解更多关于CSS居中文本的方法和应用场景。
领取专属 10元无门槛券
手把手带您无忧上云