1平面转换
加粗样式位移 ,旋转,缩放 transform 实现位移transformX()&transformY() translate 有什么作用呢? 快速实现绝对定位的元素居中效果 在这里插入代码片` position:absolute; left:50%; top:50%; margin-top:50%; margin-left:50%; width:200px; height:100px;
居中
方法一
<style>
/* 方法1 position 定位 */
.bese {
/* 子绝父相 相对定位 */
position: relative;
width: 500px;
height: 500px;
/* 边框样式 */
border: 2px solid cadetblue;
margin: 0 auto;
}
.beser {
/* 子绝父相 绝对定位 */
position: absolute;
left: 50%;
top: 50%;
width: 100px;
height: 100px;
/* 边框样式 */
border: 2px solid rgb(20, 207, 58);
margin-left: -50px;
margin-top: -50px;
/* 缩写 margin:-25px 0 0 -25px;*/
}
</style>
</head>
<body>
<div class="bese">
<div class="beser">
</div>
</div>
</body>
方法二
<style>
/* 方法2 position定位 translate平移 */
.bese {
/* 子绝父相 相对定位 */
position: relative;
width: 500px;
height: 500px;
/* 背景颜色 */
background-color: aqua;
margin: 0 auto;
}
.beser {
/* 子绝父相 绝对定位 */
position: absolute;
left: 50%;
top: 50%;
width: 100px;
height: 100px;
/* 背景颜色 */
background-color: rgb(107, 221, 62);
/* 平移X轴Y轴 */
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="bese">
<div class="beser">
</div>
</div>
</body>
方法三
<style>
/* 方法3 flex */
.bese {
/* 子绝父相 相对定位 */
position: relative;
width: 500px;
height: 500px;
display: flex;
justify-content: center;
align-items: center;
/* 背景颜色 */
background-color: aqua;
}
.beser {
/* 子绝父相 绝对定位 */
position: absolute;
width: 100px;
height: 100px;
/* 背景颜色 */
background-color: rgb(107, 221, 62);
/* 平移X轴Y轴 */
}
</style>
</head>
<body>
<div class="bese">
<div class="beser">
</div>
</div>
</body>
方法四
<style>
/* 方法4 position定位 */
.bese {
/* 子绝父相 相对定位 */
position: relative;
width: 500px;
height: 500px;
/* 背景颜色 */
background-color: aqua;
margin: 0 auto;
}
.beser {
/* 子绝父相 绝对定位 */
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
width: 100px;
height: 100px;
/* 背景颜色 */
background-color: rgb(107, 221, 62);
/* 平移X轴Y轴 */
}
</style>
</head>
<body>
<div class="bese">
<div class="beser">
</div>
</div>
方法五
<style>
/* 方法5 flex margin */
.bese {
width: 500px;
height: 500px;
/* 背景颜色 */
background-color: aqua;
display: flex;
margin: 0 auto;
}
.beser {
width: 100px;
height: 100px;
/* 背景颜色 */
background-color: rgb(107, 221, 62);
/* 平移X轴Y轴 */
margin: auto;
}
</style>
详细代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
/* 如何让一个盒子水平居中方法一定位 */
/* .father {
position: relative;
width: 500px;
height: 500px;
border: 1px solid #000;
margin: 100px auto;
}
.box {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
left: 50%;
top: 50%;
margin-top: -50px;
margin-left: -50px;
}
/* margin-top: -50px;
自身的一半但是不能用%
margin-top: -50%;
*/
/* 方法二margin:auto; */
/* .father {
position: relative;
width: 500px;
height: 500px;
border: 1px solid #000;
margin: 100px auto;
}
.box {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
left: 0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
} */
/* 方法三display:table-cell */
/* .father {
width: 500px;
height: 500px;
display: table-cell;
vertical-align: middle;
text-align: center;
border: 1px solid #000;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
display: inline-block;
} */
/* 方法四:利用display:flex; */
/* .father {
display: flex;
justify-content: center;
align-items: center;
width: 500px;
height: 500px;
border: 1px solid #000;
margin: 100px auto;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
} */
/* 方法五利用外边距计算盒子距离 */
/* .father {
width: 500px;
height: 500px;
border: 1px solid #000;
margin: 100px auto;
}
.box {
width: 100px;
height: 100px;
background-color: aqua;
margin-top: 200px;
margin-left: 200px;
} */
/* 方法六利用transform */
.father {
width: 500px;
height: 500px;
border: 1px solid #000;
margin: 100px auto;
position: relative;
}
.box {
position: absolute;
width: 100px;
height: 100px;
background-color: aqua;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="father">
<div class="box"></div>
</div>
</body>
</html>