A few lines of CSS: Implement vertical and horizontal center of a DIV in a webpage

Source: Internet
Author: User

I saw it on my blog yesterday! Let's take a look at the close-up:

The key to achieving the requirement is to find the central axis of the current page size, that is, the upper: 50%, left: 50%. The coordinate point is the central axis, but our layer has width and height. if you place a layer with a fixed width and height on this central axis, the layer will not be vertically and horizontally centered. Now we will merge the central axis of the layer with the central axis of the current page size into one, the requirement can be fulfilled.

  1. <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <HTML xmlns = "http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
  5. <SCRIPT type = "text/JavaScript" src = "include/JS/common. js"> </SCRIPT>
  6. <Title> Member registration </title>
  7. <Style type = "text/CSS">
  8. <! --
  9. @ Import URL ("include/CSS/popmodel.css ");
  10. -->
  11. </Style>
  12. </Head>
  13. <Body>
  14. <Div id = "sitepage">
  15. <Div id = "recordlist">
  16. <Form name = "form2" method = "Post" Action = "validateform. asp? Model = register ">
  17. <DL>
  18. <DT> <span id = "userstatus"> your Logon account </span> </DT>
  19. <DD> User name: <input type = "text" name = "login_user" id = "login_user" size = "30" maxlength = "16"/> </DD>
  20. </Dl>
  21. <DL>
  22. <DT> <span id = "passwordstatus"> password of the account </span> </DT>
  23. <DD> password: <input type = "password" name = "login_password" id = "login_password" size = "30" maxlength = "20"/> </DD>
  24. </Dl>
  25. <DL>
  26. <DT> <span id = "mailstatus"> email address bound to the account </span> </DT>
  27. <DD> mailbox: <input type = "text" name = "login_mail" id = "login_mail" size = "30"/> </DD>
  28. </Dl>
  29. <Label class = "Panel"> <input type = "Submit" name = "subbutton" value = "register"/> </label>
  30. </Form>
  31. </Div>
  32. </Div>
  33. </Body>
  34. </Html>

 

Recordlistlayers: Which layer should we use in its horizontal vertical environment? See popmodel.css.

  1. Body {margin: 0; padding: 0 ;}
  2. # Sitepage {margin: auto; width: auto; Height: auto ;}
  3. # Recordlist {position: absolute; width: 420px; Height: 230px; left: 50%; top: 50%; margin-left:-250px; margin-top:-100px; Border: 1px solid # CCC ;}
  4. # Recordlist DL {float: Left; margin: 0; padding-top: 5px; width: 400px ;}
  5. # Recordlist DL dt {float: Left; margin: 0; padding-left: 10px; width: 400px; Height: 30px ;}
  6. # Recordlist DL dd {float: Left; margin: 0; padding-left: 10px; padding-top: 5px; width: pixel PX; Height: 25px ;}
  7. # Recordlist DL dd label {display: inline; margin: 0; padding: 0; width: auto ;}
  8. # Recordlist DL dt {font-family: Arial, Helvetica, sans-serif; font-size: 14px; color: #000 ;}
  9. # Recordlist DL dd {font-family: Arial, Helvetica, sans-serif; font-size: 12px; color: #666 ;}

 

Note: margin-left:-250pxt and margin-top:-100; indicates that the recordlist box is left, and the width of the box is moved up, half of the height. in this way, the recordlist box is combined with the central axis of the current page to meet the requirements.

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.