水平垂直居中(文本、盒子)

一、文本

1、单行文本

代码:

<!DOCTYPE html>
<html>

<head>
  <style>

    div {
      width: 200px;
      height: 100px;
      border: 1px solid black;
      text-align: center;
      background-color: greenyellow;
    }

    span {
      line-height: 100px;
    }
  </style>
</head>

<body>

  <div><span>单行文本</span></div>

</body>

</html>


显示结果:
在这里插入图片描述

2、多行文本

代码:

<!DOCTYPE html>
<html>

<head>
  <style>

    div {
      width: 200px;
      height: 100px;
      border: 1px solid black;
      text-align: center;
      display: table;
      background-color: greenyellow;
    }

    span {
      display: table-cell;
      vertical-align: middle;
    }

  </style>
</head>

<body>

  <div><span>多行文本多行文本多行文本多行文本多行文本多行文本多行文本多行文本</span></div>

</body>

</html>

显示结果:
在这里插入图片描述

二、盒子

1、flex

代码:

<!DOCTYPE html>
<html>

<head>
  <style>

    .container {
      width: 300px;
      height: 300px;
      border: 1px solid black;
      background-color: greenyellow;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .item {
      width: 100px;
      height: 100px;
      background-color: red;
      border: 1px solid black;
    }

  </style>
</head>

<body>

  <div class="container">
    <div class="item"></div>
  </div>

</body>

</html>

显示结果:
在这里插入图片描述

2、position+margin
(1)第一种

适用于:已知当前div的width和height。

代码:

<!DOCTYPE html>
<html>

<head>
  <style>

    .container {
      width: 300px;
      height: 300px;
      border: 1px solid black;
      background-color: greenyellow;
      position: relative;
    }

    .item {
      width: 100px;
      height: 100px;
      background-color: red;
      border: 1px solid black;
      position: absolute;
      top: 50%;
      left: 50%;
      margin-left: -50px;
      margin-top: -50px;
    }

  </style>
</head>

<body>

  <div class="container">
    <div class="item"></div>
  </div>

</body>

</html>

显示结果:
在这里插入图片描述

(2)第二种

代码:

<!DOCTYPE html>
<html>

<head>
  <style>

    .container {
      width: 300px;
      height: 300px;
      border: 1px solid black;
      background-color: greenyellow;
      position: relative;
    }

    .item {
      width: 100px;
      height: 100px;
      background-color: red;
      border: 1px solid black;
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      margin: auto;
    }

  </style>
</head>

<body>

  <div class="container">
    <div class="item"></div>
  </div>

</body>

</html>

显示结果:
在这里插入图片描述

3、position+transform

代码:

<!DOCTYPE html>
<html>

<head>
  <style>

    .container {
      width: 300px;
      height: 300px;
      border: 1px solid black;
      background-color: greenyellow;
      position: relative;
    }

    .item {
      width: 100px;
      height: 100px;
      background-color: red;
      border: 1px solid black;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%,-50%)
      /* 往左,往上移动自身长度的一半 */
    }

  </style>
</head>

<body>

  <div class="container">
    <div class="item"></div>
  </div>

</body>

</html>

显示结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值