CSS如何让一个盒子或内容在指定区域中上下左右居中

        要使用CSS让盒子或内容在其父元素中上下左右居中,可以使用多种方法。例如:flexbox布局、使用grid布局、box布局、使用position定位和transform结合、以及表格属性等等,相关属性来实现内容的上下左右的居中。

        接下来我们使用以上的方法,将下图中的发电率和发电量内容进行上下左右居中。

一、创建页面

        首先创建一个demo目录,新建html页面和存放图标的image目录。

        html页面代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style type="text/css">
    * {
      margin: 0;
      padding: 0;
    }

    html,
    body {
      font-size: 14px;
      padding: 20px;
    }

    .item-list-box {
      display: inline-block;
      width: 500px;
      border: 2px solid rgba(0, 0, 0, .5);
      background-color:
        rgba(0, 0, 255, .6);
    }

    .item-list-box .title,
    .item-list-box .content {
      width: 100%;
      box-sizing: border-box;
      padding: 12px 15px;
    }

    .item-list-box .title {
      border-bottom: 1px solid rgba(0, 0, 0, .5);
    }

    .item-list-box .title h3 {
      font-size: 18px;
      color: #fff;
      position: relative;
      line-height: 1;
      padding-left: 15px;
    }

    .item-list-box .title h3::before {
      display: block;
      content: '';
      width: 4px;
      height: 100%;
      background-color: #fff;
      position: absolute;
      left: 0;
    }

    .item-list-box .content {
      height: 200px;
      position: relative;
    }

    .item-list-box .content ul.list li {
      list-style: none;
      font-size: 16px;
      color: #fff;
      display: inline-block;
      vertical-align: middle;
      padding: 5px 10px;
      text-align: center;
    }

    .item-list-box .content ul.list li img {
      width: 50px;
      margin-bottom: 10px;
    }
  </style>
</head>

<body>
  <div class="item-list-box">
    <div class="title">
      <h3>电量统计</h3>
    </div>
    <div class="content">
        <ul class="list">
          <li>
            <img src="image/icon_white_01.png" alt="">
            <p>发电率:95%</p>
          </li>
          <li>
            <img src="image/icon_white_02.png" alt="">
            <p>发电量:25329338</p>
          </li>
        </ul>
    </div>
  </div>

</body>
</html>

        效果如下图:

二、实现水平和垂直方向居中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

觉醒法师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值