CSS自适应宽度居中是指在不同屏幕尺寸下,网页内容能够自动调整宽度并保持在页面中心位置。这种布局方式可以提高用户体验,使网页在不同设备上都能良好显示。
margin: 0 auto;实现居中。margin: 0 auto;实现居中。justify-content: center;和align-items: center;实现居中。justify-items: center;和align-items: center;实现居中。<!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 {
width: 800px;
margin: 0 auto;
background-color: #f0f0f0;
padding: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>固定宽度居中示例</h1>
<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>Flexbox居中</title>
<style>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
.content {
background-color: #fff;
padding: 20px;
border-radius: 10px;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
<h1>Flexbox居中示例</h1>
<p>这是一个使用Flexbox布局实现居中的示例。</p>
</div>
</div>
</body>
</html>margin: 0 auto;时内容没有居中?原因:
解决方法:
width: 800px;。.container {
width: 800px;
margin: 0 auto;
background-color: #f0f0f0;
padding: 20px;
}原因:
display: flex;。justify-content: center;和align-items: center;。解决方法:
display: flex;。justify-content: center;和align-items: center;。.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}没有搜到相关的文章