<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>响应式布局</title>
</head>
<style>
* {
margin: 0;
padding: 0;
}
.father {
width: 500px;
height: 300px;
border: 1px solid #0a3b98;
position: relative;
/* 方式三 */
/* display: table-cell;
vertical-align: middle;
text-align: center; */
/* 方式四 */
/* display: flex;
justify-content: center;
align-items: center; */
/* 方式五 */
/* display: grid;
justify-content: center;
align-items: center; */
}
.son {
width: 100px;
height: 100px;
background: #f0a238;
/* 方式一 : 子元素若不设置宽高,会和父元素一样的宽高*/
/*
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
*/
/* 方式二: */
/*
position: absolute;
top: 50%;
left: 50%; */
/* 知道元素的宽高 */
/* margin-left: -50px;
margin-top: -50px; */
/* 不知道元素的宽高 */
/* transform: translate(-50%,-50%) */
/* 方式三 */
/* display: inline-block; */
}
</style>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
css元素居中方式
最新推荐文章于 2024-06-05 17:42:57 发布