css水平置中,豎直置中技巧,css豎直置中技巧

來源:互聯網
上載者:User

css水平置中,豎直置中技巧,css豎直置中技巧

要點1:容器設定【position: absolute;】

要點2:容器設定【top: 50%;left: 50%;】

要點3:要知道容器實際的width和heigh(可以是預設的,也可以是運行時動態擷取到的)

要點4:水平置中:margin-left = -1*(width/2),例如width是280px,則設定【margin-left:140px;】

要點5:豎直置中:margin-top = -1*(height/2),例如height是110px,則設定【margin-top:55px;】

 

例子如下:

html

<html><head></head><body><div id="alert">    <div class="alert-header" style="display: none;">資訊提示</div>    <div class="alert-message">GOOD</div>    <div class="alert-footer">        <div class="btn-close">確定</div>    </div></div></body></html>

css

    #alert {        position: absolute;        width: 280px;        top: 50%;        left: 50%;        margin-top: -55px;        margin-left: -140px;        border-radius: 5px;        background: #fff;        z-index: 1000000;        padding: 20px;    }

 運行效果

運行時實際尺寸

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.