(100-40)/2=30
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>demo</title>
</head>
<body>
<styletype="text/css">
.div1{ width: 100px; height: 100px; border: 1px solid #000000;}
.div2{ width:40px ; height: 40px; background-color: green;}
.div22{
margin-left: 30px;margin-top: 30px;
}
</style>
<divclass="div1">
<divclass="div2 div22">
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>demo</title>
</head>
<body>
<styletype="text/css">
.div1{ width: 100px; height: 100px; border: 1px solid #000000;}
.div2{ width:40px ; height: 40px; background-color: green;}
.div11{
position: relative;
}
.div22{
position: absolute;top:50%;left: 50%;margin-top: -20px;margin-left: -20px;
}
</style>
<divclass="div1 div11">
<divclass="div2 div22">
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>demo</title>
</head>
<body>
<styletype="text/css">
.div1{ width: 100px; height: 100px; border: 1px solid #000000;}
.div2{ width:40px ; height: 40px; background-color: green;}
.div11{
position: relative;
}
.div22{
position: absolute;margin:auto; top: 0;left: 0;right: 0;bottom: 0;
}
</style>
<divclass="div1 div11">
<divclass="div2 div22">
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>demo</title>
</head>
<body>
<styletype="text/css">
.div1{ width: 100px; height: 100px; border: 1px solid #000000;}
.div2{ width:40px ; height: 40px; background-color: green;}
.div11{
display: table-cell;vertical-align: middle;
}
.div22{
margin: auto;
}
</style>
<divclass="div1 div11">
<divclass="div2 div22">
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>demo</title>
</head>
<body>
<styletype="text/css">
.div1{ width: 100px; height: 100px; border: 1px solid #000000;}
.div2{ width:40px ; background-color: green;}
.div11{
display: table-cell;vertical-align: middle;
}
.div22{
margin: auto;
}
</style>
<divclass="div1 div11">
<divclass="div2 div22">
div居中方法
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>demo</title>
<styletype="text/css">
.div1 {
width: 100px;
height: 100px;
border: 1px solid #000000;
}
.div2 {
height: 40px;
width: 40px;
background-color: green;
}
.div11 {
display: flex;
/*!*flex-direction: column;*!可写可不写*/
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<divclass="div1 div11">
<divclass="div2 div22">
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8">
<title>demo</title>
<styletype="text/css">
body {
margin: 100px auto;
position: relative;
}
.div1 {
width: 100px;
height: 100px;
border: 1px solid #000000;
background-color: red;
}
.div2 {
height: 40px;
width: 40px;
background-color: green;
}
.center {
position: absolute;
top: 50%;
left: 50%
-ms-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<divclass="div1 center">
我是外部盒子
<divclass="div2 center">
我要居中
</div>
</div>
</body>
</html>
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
<style type="text/css">
.div1 {
width: 100px;
height: 100px;
border: 1px solid #000000;
background-color: red;
position: relative;
}
.div2 {
height: 40px;
width: 40px;
background-color: green;
margin:30px 30px;
}
.center{
position: fixed;
left: 50%;
}
</style>
</head>
<body>
<div class="div1 center">
<div class="div2 center">
我要居中
</div>
</div>
</body>
</html>
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!