小盒子在大盒子里水平居中

小盒子在大盒子里水平居中

  • 第一种
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.parent{background-color: #ccc;width: 400px;height: 400px;text-align:center;}.child{display: inline-block;background-color: pink;width: 250px;height: 250px;}</style>
</head>
<body><div class="parent"><div class="child">水平居中</div></div>
</body>
</html>
  • 第二种
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.parent{background-color: #ccc;width: 400px;height: 400px;}.child{display: table;margin: 0 auto;background-color: pink;width: 250px;height: 250px;}</style>
</head>
<body><div class="parent"><div class="child">水平居中</div></div>
</body>
</html>
  • 第三种
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.parent{background-color: #ccc;width: 400px;height: 400px;position: relative;}.child{background-color: pink;width: 250px;height: 250px;position: absolute;left: 50%;transform: translateX(-50%);}</style>
</head>
<body><div class="parent"><div class="child">水平居中</div></div>
</body>
</html>
  • 第四种
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.parent{background-color: #ccc;width: 400px;height: 400px;display: flex;justify-content:center;}.child{background-color: pink;width: 250px;height: 250px;}</style>
</head>
<body><div class="parent"><div class="child">水平居中</div></div>
</body>
</html>

小盒子在大盒子里水平居中

小盒子在大盒子里水平居中

  • 第一种
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.parent{background-color: #ccc;width: 400px;height: 400px;text-align:center;}.child{display: inline-block;background-color: pink;width: 250px;height: 250px;}</style>
</head>
<body><div class="parent"><div class="child">水平居中</div></div>
</body>
</html>
  • 第二种
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.parent{background-color: #ccc;width: 400px;height: 400px;}.child{display: table;margin: 0 auto;background-color: pink;width: 250px;height: 250px;}</style>
</head>
<body><div class="parent"><div class="child">水平居中</div></div>
</body>
</html>
  • 第三种
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.parent{background-color: #ccc;width: 400px;height: 400px;position: relative;}.child{background-color: pink;width: 250px;height: 250px;position: absolute;left: 50%;transform: translateX(-50%);}</style>
</head>
<body><div class="parent"><div class="child">水平居中</div></div>
</body>
</html>
  • 第四种
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><style>*{margin: 0;padding: 0;}.parent{background-color: #ccc;width: 400px;height: 400px;display: flex;justify-content:center;}.child{background-color: pink;width: 250px;height: 250px;}</style>
</head>
<body><div class="parent"><div class="child">水平居中</div></div>
</body>
</html>