• 常见元素居中的五种方法


    块元素,且宽和高已知

    <!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>
        * {
          margin: 0;
          padding: 0;
        }
        html, body, .father {
           100%;
          height: 100%;
        }
        .father {
          position: relative;
        }
        .child {
             /* 使用绝对定位 */
          position: absolute;
          left: 50%;
          top: 50%;
          /* 盒子宽和高的一半 */
          margin-left: -150px;
          margin-top: -150px;
           300px;
          height: 300px;
          background-color: lightblue;
        }
      </style>
    </head>
    <body>
      <div class="father">
        <div class="child"></div>
      </div>
    </body>
    </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>
        * {
          margin: 0;
          padding: 0;
        }
        html, body, .father {
           100%;
          height: 100%;
        }
        .father {
          position: relative;
        }
        .child {
          position: absolute;
          left: 50%;
          top: 50%;
          /* 偏移50% */
          transform: translateX(-50%) translateY(-50%);
           300px;
          height: 300px;
          background-color: lightblue;
        }
      </style>
    </head>
    <body>
      <div class="father">
        <div class="child"></div>
      </div>
    </body>
    </html>

    flex布局

    <!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>
        * {
          margin: 0;
          padding: 0;
        }
        html, body, .father {
           100%;
          height: 100%;
        }
        .father {
          display: flex;
          justify-content: center;
          align-items: center;
        }
        .child {
           300px;
          height: 300px;
          background-color: lightblue;
        }
      </style>
    </head>
    <body>
      <div class="father">
        <div class="child"></div>
      </div>
    </body>
    </html>

    margin:auto

    <!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>
        * {
          margin: 0;
          padding: 0;
        }
        html, body, .father {
           100%;
          height: 100%;
        }
        .father {
         position: relative;
        }
        .child {
          position: absolute;
          top: 0;
          right: 0;
          bottom: 0;
          left: 0;
          margin: auto;
           300px;
          height: 300px;
          background-color: lightblue;
        }
      </style>
    </head>
    <body>
      <div class="father">
        <div class="child"></div>
      </div>
    </body>
    </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>
        * {
          margin: 0;
          padding: 0;
        }
        .father {
           500px;
          height: 500px;
          background-color: #cccccc;
        }
        .father {
          text-align: center;
          line-height: 500px;
        }
        .child {
          font-size: 26px;
          color: #f40;
          display: inline-block;
        }
      </style>
    </head>
    <body>
      <div class="father">
        <div class="child">一朵菊花,一个小鬼</div>
      </div>
    </body>
    </html>

    ————————————————
    版权声明:本文为CSDN博主「亦是木子也是雨」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_44162474/article/details/106626269

    虚心学习、丰富自己
  • 相关阅读:
    C++Primer第5版学习笔记(三)
    C++Primer第5版学习笔记(二)
    C++Primer第5版学习笔记(一)
    A*寻路算法的探寻与改良(三)
    A*寻路算法的探寻与改良(二)
    A*寻路算法的探寻与改良(一)
    html5页面js判断是否安装app,以及判断是否在app内部打开html5页面
    结合prototype和xmlhttprequest封装ajax请求
    前端常见的性能优化和浏览器兼容性问题
    常见的HTTP状态码
  • 原文地址:https://www.cnblogs.com/tkqq000/p/14837301.html
Copyright © 2020-2023  润新知