Center div positioning in CSS

Source: Internet
Author: User

All along, I think a width, and then write a margin:0 auto, it can, but sometimes it will not work.

For example, when I want to set a div with a width of 700, set it in the middle with relative positioning. Any stretch you have is centered.

See the Real Example

<!doctype Html>"Utf-8"><title> Untitled Document </title><script type="Text/javascript"Src="Jquery-2.0.3.js"></script><style>. scroll{height:2000px;}. Main{width: -%;height:550px; background-color: #fe3a08; position:relative;} . Cent{width:700px;color: #fff; font-size:46px; Font-family:"Young Circle"; Position:absolute; Left -%; margin-left:-350px;line-height:500px;top:0px;}</style><script>function Changetop (obj, value) {varOffsetTop = Obj.offset (). Top +value; varScrollTop =$ (window). scrolltop ();    Console.log (OFFSETTOP);    Console.log (scrolltop); varresult = ScrollTop-OffsetTop; if((OffsetTop <scrolltop)) {Obj.find (". Bg-image"). Stop (true,true). Animate ({"Top": result}, -); }    if(OffsetTop +550<scrolltop) {Obj.find (". Bg-image"). Stop (true,true). Animate ({"Top":"0px"}, -); }}$ (function () {$ (window). Scroll (function () {Changetop ($ (". Main"), the); })    })</script>class="Scroll"> <divclass="Main"> <divclass="cent Bg-image">R6 BRANCH OFFICE|Engineering Management</div> </div></div></body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.