居中问题总结

1.元素内容水平居中:

text-align:center;

2.块级元素水平居中:

margin:0 auto

3.一行文字垂直居中:

行高等于高

4.多行内容垂直居中:

margin:20px 0; (加在自己上)
padding:20px 0; (加在父元素上)

5.子元素在父元素中水平垂直居中:

(1)margin:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       .box{
           width: 300px;
           height: 300px;
           background-color: palevioletred;
           overflow: hidden;
       }
       .box1{
           width: 100px;
           height: 100px;
           background-color: deepskyblue;
           margin: 100px auto;
       }
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
</div>
</body>
</html>

(2)position:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       .box{
           width: 300px;
           height: 300px;
           background-color: palevioletred;
           position: relative;
       }
       .box1{
           width: 100px;
           height: 100px;
           background-color: deepskyblue;
           position: absolute;
           left: 100px;
           top: 100px;
       }
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
</div>
</body>
</html>

(3)padding:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       .box{
           width: 300px;
           height: 300px;
           background-color: palevioletred;
           padding: 100px;
           box-sizing: border-box;
       }
       .box1 {
           width: 100px;
           height: 100px;
           background-color: deepskyblue;
       }
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
</div>
</body>
</html>

(4)表格法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       .box{
           width: 300px;
           height: 300px;
           background-color: palevioletred;
           display: table-cell;
           vertical-align: middle;
       }
       .box1 {
           width: 100px;
           height: 100px;
           background-color: deepskyblue;
           margin: 0 auto;
       }
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
</div>
</body>
</html>

(5)绝对定位法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       .box{
           width: 300px;
           height: 300px;
           background-color: palevioletred;
           position: relative;
       }
       .box1 {
           width: 100px;
           height: 100px;
           background-color: deepskyblue;
           position: absolute;
           top: 50%;
           left: 50%;
           margin-left: -50px;
           margin-top: -50px;
       }
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
</div>
</body>
</html>

(6)弹性盒法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
       .box{
           width: 300px;
           height: 300px;
           background-color: palevioletred;
           display: flex;
           justify-content: center;
           align-items: center;
       }
       .box1 {
           width: 100px;
           height: 100px;
           background-color: deepskyblue;
       }
    </style>
</head>
<body>
<div class="box">
    <div class="box1"></div>
</div>
</body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值