利用CSS实现【水平居中和垂直居中】的方法

实现水平居中和垂直居中的方法

一、水平居中

利用CSS布局

(1) 元素居中对齐

1.1 理论:要水平居中对齐一个元素(如 <div>), 可以使用 margin: auto;。

                  设置到元素的宽度将防止它溢出到容器的边缘。

       元素通过指定宽度,并将两边的空外边距平均分配:

  1. 代码示例:
  1.    <style>
  2.         * {
  3.             margin: 0;
  4.             padding: 0;
  5.         }
  6.         .c {
  7.             margin: auto;
  8.             /* 设置元素的宽度将防止它溢出到容器的边缘 */
  9.             width: 50%;
  10.             border: 3px solid #73AD21;
  11.             padding: 10px;
  12.         }
  13.     </style>
  14. </head>
  15. <body>
  16.     <div class="c">
  17.         <p ><b>注意:</b>元素居中对齐,要实现水平居中对齐一个元素,可以使用margin:auto; 。 </p>
  18.     </div>
  19. </body>
  20. </html>

1.3  运行结果:

(2)  文本居中对齐

text-align:center; 对文本元素设置水平居中

代码示例:

        .c {

            margin: auto;

            /* 设置元素的宽度将防止它溢出到容器的边缘 */

            width: 50%;

            border: 3px solid #73AD21;

            padding: 10px;

            text-align: center;

        }

  <div class="c">

        <p><b>注意:</b>元素居中对齐,要实现水平居中对齐一个元素,可以使用margin:auto; 。 </p>

    </div>

运行结果:

(3)  图片居中对齐

要让图片居中对齐, 可以使用 margin: auto; 并将它放到  元素中:

代码示例:

 <style>

        * {

            margin: 0;

            padding: 0;

        }

        img {

            /* 块级元素 */

            display: block;

            margin: 0 auto;

        }

<body>

    <img src="./imges/jingdong.png" alt="">

</body>

代码运行结果:

(4)使用position和transform

 实现原理: position: absolute;给元素设置绝对定位,left: 50%;让其距离左边偏移父元素的50%,然后通过 transform: translate(-50%);让其向左平移自身宽度的50%从而实现一个水平居中的效果。

②代码示例:

<style>

        * {

            margin: 0;

            padding: 0;

        }

        .box {

            position: relative;

            width: 500px;

            height: 500px;

            background-color: aquamarine;

            /* 水平居中 */

            margin: auto;

            text-align: center;

        }

        .box p {

            position: absolute;

            height: 200px;

            width: 200px;

            background-color: pink;

            /* 其左填充盒子宽度的50% */

            left: 50%;

            transform: translate(-50%);

        }

    </style>

body 部分

 <div class="box">

        <p>我是元素内容</p>

    </div>

③ 运行结果:

二 、 垂直居中

(1)垂直居中对齐 - 使用 padding

代码示例:

  * {

            margin: 0;

            padding: 0;

        }

        .box {

            width: 500px;

            padding: 50px 0;

            border: 3px solid green;

        }

body 部分

<div class="box">

        <p>我是元素内容</p>

    </div>

运行结果:

(2)垂直居中 - 使用 line-height

把行高line-height和高度height设置为一样,可以实现文本元素的垂直对齐,(该做法只适用于文本、行内块元素、行内元素的垂直居中,块元素的垂直居中不适用)案例代码:

 .box {

            width: 500px;

            height: 500px;

            background-color: aqua;

            line-height: 500px;

        }

body 部分

  <div class="box">

        <p>我是元素内容</p>

    </div>

运行结果:

(3)垂直居中 - 使用 position 和 transform

  1.  使用该方法实现垂直居中的原理和上面所讲的水平居中类似,简而言之就是,先让元素向下移动父元素的50%,在向上移动自身的50%,从而移动到了父元素垂直方向的中间位置。
  2. 代码示例:
  3.    .box {
  4.             position: relative;
  5.             width: 500px;
  6.             height: 500px;
  7.             background-color: aquamarine;
  8.             /* 水平居中 */
  9.             margin: auto;
  10.             text-align: center;
  11.         }
  12.         .box p {
  13.             position: absolute;
  14.             height: 200px;
  15.             width: 200px;
  16.             background-color: pink;
  17.             /* 其左填充盒子宽度的50% */
  18.             top: 50%;
  19.             transform: translate(0, -50%);
  20.         }

运行结果:

三、水平垂直居中 Flex布局

(1)flex布局中,通过设置侧轴上子元素排列方式为center,可以实现将元素垂直居中,适用于所有类型的元素。

  .box {

            display: flex;

            /*设置主轴方向是水平方向*/

            flex-direction: row;

            /*设置侧轴上,子元素的排列方式为居中对齐*/

            align-items: center;

            position: relative;

            width: 500px;

            height: 500px;

            background-color: aquamarine;

            margin: auto;

            text-align: center;

        }

body部分

<div class="box">

        <p>我是元素内容</p>

    </div>

运行结果:

(2) flex布局中,通过设置侧轴上子元素排列方式为center,可以实现将元素水平居中,适用于所有类型的元素。

代码示例:

  .box {

            display: flex;

            /*设置主轴方向是垂直方向*/

            flex-direction: column;

            /*设置侧轴上,子元素的排列方式为居中对齐*/

            align-items: center;

            position: relative;

            width: 500px;

            height: 500px;

            background-color: aquamarine;

            margin: auto;

            text-align: center;

        }

        .box p {

            position: absolute;

            height: 200px;

            width: 200px;

            background-color: pink;

}

body部分

<div class="box">

        <p>我是元素内容</p>

    </div>

运行结果:

(3) flex 布局 实现水平居中同时也实现垂直居中,通过设置侧轴上子元素排列方式为center,可以实现将元素水平居中,通过justify-content 设置容器内的项目在主轴方向上的排列和对齐方式。

代码示例:

        .box {

            display: flex;

            /*设置侧轴上,子元素的排列方式为居中对齐*/

            align-items: center;

           /*设置项目位于容器的中心,实现居中紧挨着填充的效果*/

            justify-content: center;

            position: relative;

            width: 500px;

            height: 500px;

            background-color: aquamarine;

            margin: auto;

            text-align: center;

        }

        .box p {

            position: absolute;

            height: 200px;

            width: 200px;

            background-color: pink;

        }

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值