美文网首页
居中问题浅谈

居中问题浅谈

作者: _MChao | 来源:发表于2016-08-26 02:16 被阅读0次

在CSS的世界里居中一直都是人们抱怨的一件事。“居个中为什么就这么难呢?”人们无奈的说。我认为困难的不是怎么做他,而是怎样在不同的情况下,用不同的方法去达到这相同的目的---居中。

所以 让我们来解决这个问题吧

水平居中(Horizontally)

  • 行内块元素(inline or inline-* element)

.center-children {
text-align: center;
}
```

  • 一个块级元素(one block level element)

.center-me {
margin: 0 auto;
}
```

  • 多个块级元素(some block level elements)

    HTML

    <main class="inline-block-center">
        <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
        </div>
        <div>
        I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
        </div>
        <div>
        I'm an element that is block-like with my siblings and we're centered in a row.
        </div>
    </main>
    
        <main class="flex-center">
          <div>
            I'm an element that is block-like with my siblings and we're centered in a row.
          </div>
          <div>
            I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do.
          </div>
          <div>
            I'm an element that is block-like with my siblings and we're centered in a row.
          </div>
        </main>
    

    CSS

        body {
          background: #f06d06;
          font-size: 80%;
        }
    
        main {
          background: white;
          margin: 20px 0;
          padding: 10px;
        }
    
        main div {
          background: black;
          color: white;
          padding: 15px;
          max-width: 125px;
          margin: 5px;
        }
    
        .inline-block-center {
          text-align: center;
        }
        .inline-block-center div {
          display: inline-block;
          text-align: left;
        }
    
        .flex-center {
          display: flex;
          justify-content: center;
        }
    

    总之:

    • text-align

垂直居中(Verticallly)

  • 行内块元素(inline or inline-* element)

    • 单行情况

      padding

          .link {
              padding-top: 30px;
              padding-bottom: 30px;
          }
      

      line-height

          .center-text-trick {
            height: 100px;
            line-height: 100px;
            white-space: nowrap;
          }
      
    • 多行情况

      HTML

      <table>
        <tr>
          <td>
            I'm vertically centered multiple lines of text in a real table cell.
          </td>
        </tr>
      </table>
      
      <div class="center-table">
        <p>I'm vertically centered multiple lines of text in a CSS-created table layout.</p>
      </div>
      

      CSS

      body {
        background: #f06d06;
        font-size: 80%;
      }
      
      table {
        background: white;
        width: 240px;
        border-collapse: separate;
        margin: 20px;
        height: 250px;
      }
      
      table td {
        background: black;
        color: white;
        padding: 20px;
        border: 10px solid white;
        /* default is vertical-align: middle; */
      }
      
      .center-table {
        display: table;
        height: 250px;
        background: white;
        width: 240px;
        margin: 20px;
      }
      .center-table p {
        display: table-cell;
        margin: 0;
        background: black;
        color: white;
        padding: 20px;
        border: 10px solid white;
        vertical-align: middle;
      }
      

      总之:

      • padding
      • line-height
      • vertical-align
      • flex
  • 一个块级元素(one block level element)

    • 知道元素高度
    .parent {
      position: relative;
    }
    .child {
      position: absolute;
      top: 50%;
      height: 100px;
      margin-top: -50px; /* account for padding and border if not using box-sizing: border-box; */
    }
  • 不知道元素高度
    .parent {
        position: relative;
    }
    .child {
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
    }
  • 无所谓,flex解决一切
    .parent {
        display: flex;
        flex-direction: column;
        justify-content: center;
    }

水平与垂直同时居中(Both Horizontally and Vertically)

  • 知道元素尺寸

    .parent {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
  • 不知道元素尺寸

    .parent {
        position: relative;
    }
    .child {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }
    
  • 无所谓,flex解决一切

    .parent {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    

总结(Conclusion)

只要你愿意,css会帮你完成这一切...

参考资料

相关文章

  • 居中问题浅谈

    在CSS的世界里居中一直都是人们抱怨的一件事。“居个中为什么就这么难呢?”人们无奈的说。我认为困难的不是怎么做他,...

  • 页面布局居中问题

    css页面布局水平垂直居中问题 居中问题

  • 居中问题

  • 居中问题

    css图片居中 1 tanslate居中 2 text-align属性水平居中 3 绝对定位元素居中 4 css3...

  • 居中问题

    水平布局(父元素和子元素的宽度未知) 优点:兼容性好缺点:子元素文本继承了text-align属性,子元素要额外加...

  • 居中问题

    CSS中的居中问题理解与归纳 水平居中 (1)inline或inline-block、inline-table、i...

  • 前端面试重点——居中问题

    前端面试重点——居中问题 在页面布局开发中,居中问题是我们经常碰到的问题,掌握居中问题对于解决页面布局非常重要,同...

  • 居中的那些事

    在前端开发过程中,我们不可避免的会遇到一些水平居中和垂直居中的问题,现就居中问题做以下总结。 a:水平居中 1,宽...

  • 垂直居中问题、多行居中

    实现效果 实现代码: .d1{ text-align: center; width:...

  • 设置居中样式

    在css中居中效果可以分为:水平居中、垂直居中、水平垂直居中三种。最近小程序开发项目中也经常遇到居中效果设置问题,...

网友评论

      本文标题:居中问题浅谈

      本文链接:https://www.haomeiwen.com/subject/migasttx.html