• <table id="4yyaw"><kbd id="4yyaw"></kbd></table>
  • <td id="4yyaw"></td>
  • 發布時間:2020-09-28 23:23 原文鏈接: 一篇文章帶你了解CSS對齊方式(二)

    三、垂直居中,水平居中

    HTML代碼:

    <html lang="en"><head>  <meta charset="UTF-8">  <title>編程字典</title>
    </head><body>
     <h2>居中</h2>  <p>在這個實例中,     我們使用定位和transform屬性實現元素的水平和垂直居中:</p>
     <div class="center">    <!-- <p>我是垂直居中的.</p> -->    <p>我水平垂直居中.</p>      </div>
     <p>注意: transform屬性不支持IE8和更早的版本.</p>
    </body></html>

    1. 使用 padding

    有許多方法來中心垂直CSS元素. 一個簡單的解決方案是使用頂部和底部 padding:

    .center {    padding: 70px 0;    border: 3px solid green;}

    水平和垂直居中, 使用 padding 和 text-align: center:

    垂直居中

    .center {    padding: 70px 0;    border: 3px solid green;    text-align: center;}

    2. 使用 line-height

    另一個技巧是使用 line-height 屬性值等于 height 屬性值.

    .center {    line-height: 200px;    height: 200px;    border: 3px solid green;    text-align: center;}
    If the text has multiple lines, add the following: .center p {    line-height: 1.5;    display: inline-block;    vertical-align: middle;}3.  使用 position & transform

    如果padding和line-height不可選,第三種解決方案是使用定位和變換屬性:

    .center {    height: 200px;    position: relative;    border: 3px solid green;}
    .center p {    margin: 0;    position: absolute;    top: 50%;    left: 50%;    transform: translate(-50%, -50%);}

    四、總結

    本文基于Html基礎,主要介紹了Html中對齊的方式,對于對齊中的標簽做了詳細的講解,用豐富的案例 ,代碼效果圖的展示,幫助大家更好理解 。

    最后,希望可以幫助大家更好的學習CSS3。


  • <table id="4yyaw"><kbd id="4yyaw"></kbd></table>
  • <td id="4yyaw"></td>
  • 调性视频