是html中最常使用的元素之一,它主要是用来定义一个块级区域。在网页设计中,我们经常会遇到需要将一个div水平居中显示的需求,今天我们就来学习一下如何用css来实现这个效果。首先,我们需要明确一下,要...
<div class="container">
<p>这是一个div。</p>
</div>
<style>
.container {
margin-left: auto;
margin-right: auto;
width: 50%;
height: 200px;
background-color: #ccc;
}
</style> <div class="container">
<p>这是一个div。</p>
</div>
<style>
.container {
display: flex;
justify-content: center;
align-items: center;
width: 50%;
height: 200px;
background-color: #ccc;
}
</style>