css中將div定位置中

來源:互聯網
上載者:User

標籤:c   style   class   blog   code   java   

        一直,我是認為定一個width,然後寫一句margin:0 auto,就可以,但是有時也會不管用。

 

       例如當我要定一個寬度為700的div,用相對定位定在中間。任你怎麼展開都是置中。

        

   看執行個體子

 

     

<!doctype html><html><head><meta charset="utf-8"><title>無標題文檔</title><script type="text/javascript" src="jquery-2.0.3.js"></script><style>.scroll{height:2000px;}.main{width:100%;height:550px; background-color:#fe3a08; position:relative;}    .cent{width:700px;color:#fff; font-size:46px; font-family:"幼圓"; position:absolute; left:50%; margin-left:-350px;line-height:500px;top:0px;}</style><script>function changeTop(obj, value) {    var offsetTop = obj.offset().top + value;    var scrollTop = $(window).scrollTop();    console.log(offsetTop);    console.log(scrollTop);    var result = scrollTop - offsetTop ;    if ((offsetTop < scrollTop) ) {        obj.find(".bg-image").stop(true,true).animate({ "top": result }, 300);    }    if (offsetTop +550 < scrollTop) {        obj.find(".bg-image").stop(true,true).animate({ "top": "0px" }, 300);    }}$(function(){    $(window).scroll(function () {        changeTop($(".main"),80);    })    })</script></head><body><div class="scroll">    <div class="main">        <div class="cent bg-image">                R6 BRANCH OFFICE | 工程管理        </div>    </div></div></body></html>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.