web前端

css 居中–分类整理及程序示例

本节将css居中分类如下:

  • 水平居中
  • 垂直居中
  • 水平垂直居中

表格

居中样式父元素依赖需要定宽待居中元素
水平nullwidth: 200px(高自适应)margin: 0 auto
水平nullwidth: 200px(高自适应)position: relative; margin: auto
水平display: relativewidth: 200px(高自适应)left: 50%; top: 0; margin: 0 -100px;
垂直display: relativeheight: 100px(宽自适应)left: 0; top: 50%; margin: -50px 0;
水平垂直display: relativewidth: 200px; height: 100pxleft: 50%; top: 50%; margin: -50px -50px;
水平display: relativenull(高宽自适应)position: absolute; left: 50%; top: 0; transform: translate(-50%,0);
垂直display: relativenull(高宽自适应)position: absolute; left: 0; top: 50%; transform: translate(0,-50%);
水平垂直display: relativenull(高宽自适应)position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%);
水平display:flex;justify-content: centernull(width自适应,height未设置时拉伸为与父元素等高)null
垂直display:flex;align-items: centernull(width、height自适应)null
水平垂直display:flex;align-items: center;justfy-content:centernull(width、height自适应)null
水平居中text-align: centernull(高宽自适应)display: inline-block OR display: inline
垂直dispaly: table-cell; verical-align: middlenull(高宽自适应)display: inline-block OR display: inline
水平垂直dispaly: table-cell; verical-align: middle; text-align: center;null(高宽自适应)display: inline-block OR display: inline

代码块

通用html:

/*HTML*/
  <div class="parent">
    <div class="child">
        <p>ok!p</p>
    </div>
  </div>

css代码:

  • margin: 0 auto
 /*CSS*/
  .child{
    width: 200px;
    margin: 0 auto;
    background-color:  pink;
  }

  • margin: auto;
.child{
    position: relative;
    width: 200px;
    margin: auto;
    background-color:  pink;
  }

  • absolute+margin
 .parent{
    position: relative;
    width: 400px;
    height: 300px;
    background-color: gray;
  }
  .child{
    position: absolute;
    width: 200px;
    height: 100px;
    left: 50%;
    top: 50%;
    margin: -50px -100px;
    background-color:  pink;
  }

这里写图片描述

  • absolute+transform
  .parent{
    position: relative;
    width: 400px;
    height: 300px;
    background-color: gray;
  }
  .child{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    background-color:  pink;
  }

这里写图片描述

  • text-align
 .parent{
    text-align: center;
    width: 400px;
    height: 300px;
    background-color: gray;
  }
  .child{
    display: inline-block;
    background-color:  pink;
  }

这里写图片描述

  • verical-align
  .parent{
    display: table-cell;
    vertical-align: middle;
    width: 400px;
    height: 300px;
    background-color: gray;
  }
  .child{
    display: inline-block;
    background-color:  pink;
  }

这里写图片描述

  • verical-align + text-align
  .parent{
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    width: 400px;
    height: 300px;
    background-color: gray;
  }
  .child{
    display: inline-block;
    background-color:  pink;
  }

这里写图片描述

  • flex+justify-content
 .parent{
    display: flex;
    justify-content: center;
    width: 400px;
    height: 300px;
    background-color: gray;
  }
  .child{
    background-color:  pink;
  }

这里写图片描述
- flex+align-items

  .parent{
    display: flex;
    align-items: center;
    width: 400px;
    height: 300px;
    background-color: gray;
  }
  .child{
    background-color:  pink;
  }

这里写图片描述
- flex+justify-content+align-items

  .parent{
    display: flex;
    justify-content: center;
    align-items: center;
    width: 400px;
    height: 300px;
    background-color: gray;
  }
  .child{
    background-color:  pink;
  }

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值