CSS(层叠样式表)是一种用于描述HTML或XML(包括SVG、MathML等各种XML方言)文档样式的样式表语言。字体居中是指将文本在某个容器内水平或垂直居中对齐。
text-align: center; 属性。margin: 0 auto; 属性(适用于块级元素)。line-height 属性(适用于单行文本)。transform 属性。<!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>
.container {
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<p>This text is horizontally 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>Vertical Centering</title>
<style>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
</style>
</head>
<body>
<div class="container">
<p>This text is vertically centered.</p>
</div>
</body>
</html>text-align: center; 不起作用?原因:
解决方法:
display: block;)。.container {
text-align: center;
}
.container p {
display: inline-block;
}原因:
display: flex; 没有正确应用到容器上。align-items: center; 没有正确应用到容器上。解决方法:
display: flex; 和 align-items: center; 正确应用到容器上。.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}希望这些信息对你有所帮助!
没有搜到相关的沙龙