<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 100px; height: 100px;
border: red solid 2px;
}
.block{
width: 50px; height: 50px;
background: #6c5ce7;
}
.wrapFlex{
display: flex;
justify-content: center;
align-items: center;
}
.blockFlex{
}
</style>
</head>
<body>
<div class="wrap wrapFlex">
<div class="block blockFlex"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 100px; height: 100px;
border: red solid 2px;
}
.block{
width: 50px; height: 50px;
background: #6c5ce7;
}
.wrapFlex{
display: flex;
}
.blockFlex{
margin: auto;
}
</style>
</head>
<body>
<div class="wrap wrapFlex">
<div class="block blockFlex"></div>
</div>
</body>
</html>
-
3、Flex属性 + 子元素 align-self
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 100px; height: 100px;
border: red solid 2px;
}
.block{
width: 50px; height: 50px;
background: #6c5ce7;
}
.wrapFlex{
display: flex;
}
.blockFlex{
align-self: center;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrap wrapFlex">
<div class="block blockFlex"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 100px; height: 100px;
border: red solid 2px;
}
.block{
width: 50px; height: 50px;
background: #6c5ce7;
}
.wrapFlex{
display: grid;
}
.blockFlex{
margin: auto;
}
</style>
</head>
<body>
<div class="wrap wrapFlex">
<div class="block blockFlex"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 100px; height: 100px;
border: red solid 2px;
}
.block{
width: 50px; height: 50px;
background: #6c5ce7;
}
.allwrap{
}
.allblock{
margin: calc(50% - 25px) auto;
}
</style>
</head>
<body>
<div class="wrap allwrap">
<div class="block allblock"></div>
</div>
</body>
</html>
-
6、绝对定位 position: absolute
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 100px; height: 100px;
border: red solid 2px;
position: relative;
}
.block{
width: 50px; height: 50px;
background: #6c5ce7;
}
.allwrap{
}
.allblock{
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="wrap allwrap">
<div class="block allblock"></div>
</div>
</body>
</html>
-
7、绝对定位 position: absolute
+ 动画 transform
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 100px; height: 100px;
border: red solid 2px;
position: relative;
}
.block{
width: 50px; height: 50px;
background: #6c5ce7;
}
.allwrap{
}
.allblock{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="wrap allwrap">
<div class="block allblock"></div>
</div>
</body>
</html>
-
8、表格属性 display: table-cell;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 100px; height: 100px;
border: red solid 2px;
}
.block{
width: 50px; height: 50px;
background: #6c5ce7;
}
.allwrap{
display: table-cell;
vertical-align: middle;
text-align: center;
}
.allblock{
display: inline-block;
}
</style>
</head>
<body>
<div class="wrap allwrap">
<div class="block allblock"></div>
</div>
</body>
</html>
-
9、当只有一行的时候,对里面的文字进行水平垂直居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{
margin: 0;
padding: 0;
}
.online{
width: 100px; height: 100px;
background: #6c5ce7;
line-height: 100px;
text-align: center;
}
</style>
</head>
<body>
<div class="online">123</div>
</body>
</html>