前端CSS中简单且快速实现元素居中的方法

文章介绍了使用CSS实现元素居中布局的三种常见方法:1)通过设置子元素margin:0auto和line-height等于height;2)结合absolute定位和transform属性;3)利用flex布局的justify-content和align-items属性。读者需要掌握这三种居中技术。
摘要由CSDN通过智能技术生成

一、水平居中(即横向纵向都位于盒子中间)

1、子元素设置:margin: 0 auto; line-height等于height;

<style>

        body{

           

        }

        .father{

            width: 300px;

            height: 300px;border: 1px solid #000;

            margin: 0 auto;

            display: flex;

           

        }

        .son{

            width: 100px;height: 100px;

            border: 1px solid #000;

            background-color: pink;

            align-self: center;

            margin: 0 auto;

            line-height: 100px;

        }

    </style>

</head>

<body>

    <div class="father">

        <div class="son"></div>

    </div>

</body>

 2.利用绝对和相对定位(利用绝对相对定位有多种方法,以下只举例较为简便方法)

      absolute + transform

<style>

        body{

           

        }

        .father{

            width: 300px;

            height: 300px;border: 1px solid #000;

            position: relative;

           

        }

        .son{

            width: 100px;height: 100px;

            border: 1px solid #000;

            background-color: pink;

            position: absolute;

            left: 50%;

            top: 50%;

            transform: translate(-50%, -50%);

           

        }

    </style>

</head>

<body>

    <div class="father">

        <div class="son"></div>

    </div>

 absolute + margin: auto

<style>

        body{

           

        }

        .father{

            width: 300px;

            height: 300px;border: 1px solid #000;

            margin: 0 auto;

            position: relative;

           

        }

        .son{

            width: 100px;height: 100px;

            border: 1px solid #000;

            background-color: pink;

            position: absolute;

            left: 0;

            right: 0;

            top: 0;

            bottom: 0;

            margin: auto;

           

        }

    </style>

</head>

<body>

    <div class="father">

        <div class="son"></div>

    </div>

</body>

 三、利用flex布局

<style>

        body{

           

        }

        .father{

            width: 300px;

            height: 300px;border: 1px solid #000;

            display: flex;

            justify-content: center;

            align-items: center;      

        }

//或者父元素设置display: flex;  子元素设置margin: auto;

        .son{

            width: 100px;height: 100px;

            border: 1px solid #000;

            background-color: pink;

           

           

        }

    </style>

</head>

<body>

    <div class="father">

        <div class="son"></div>

    </div>

</body>

需掌握至少三种居中方法:

即单操作父元素居中display: flex; justify-content: center;align-items: center;

单操作子元素居中margin: 0 auto;line-height: 100px;

以及操作俩者居中

position: relative;

position: absolute;

            left: 0;

            right: 0;

            top: 0;

            bottom: 0;

            margin: auto;

附:

        实现横向居中的简单方法:

元素设置 margin: 0 auto;

        实现元素纵向居中简单方法:

父元素设置 display: flex;  子元素设置align-self: center;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值