在网页设计中,有时我们需要把一个盒子居中展示。这时候,我们可以使用CSS来实现这一效果。首先,在CSS中定义该盒子的样式,包括宽度、高度、背景色等:.box { width: 200px; heigh...
在网页设计中,有时我们需要把一个盒子居中展示。这时候,我们可以使用CSS来实现这一效果。
首先,在CSS中定义该盒子的样式,包括宽度、高度、背景色等:
.box {
width: 200px;
height: 200px;
background-color: #ccc;
} .container {
display: flex;
justify-content: center;
align-items: center;
} <!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 200px;
height: 200px;
background-color: #ccc;
}
.container {
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
</html> <!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 200px;
height: 200px;
background-color: #ccc;
}
.container {
display: flex;
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
</html>