CSS(层叠样式表)是一种用于描述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>Horizontal Centering</title>
<style>
.container {
text-align: center;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to Our Website</h1>
</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;
align-items: center;
justify-content: center;
height: 100vh;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to Our Website</h1>
</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 {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
}
</style>
</head>
<body>
<div class="container">
<h1>Welcome to Our Website</h1>
</div>
</body>
</html>原因:
解决方法:
!important关键字来覆盖其他样式。100vh或其他合适的高度。.container {
display: flex;
align-items: center;
justify-content: center;
height: 100vh !important;
}通过以上方法,你可以实现网页的整体居中,并解决常见的居中问题。