zoukankan      html  css  js  c++  java
  • 常见元素居中的五种方法

    块元素,且宽和高已知

    <!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

    虚心学习、丰富自己
  • 相关阅读:
    分治
    递归
    java三大特性之封装
    Java基础知识
    puk2367 拓扑排序
    puk1251 最小生成树
    puk1521 赫夫曼树编码
    DOSbox简单运行操作
    Mybatis初学经验----------------(2)
    mysql存储引擎MyISAM和InnoDB的区别
  • 原文地址:https://www.cnblogs.com/tkqq000/p/14837301.html
Copyright © 2011-2022 走看看