Vertical center of img in div

Source: Internet
Author: User

Css method 1

Html generation
  1. <Style type = "text/css">
  2. <! --
  3. * {Margin: 0; padding: 0}
  4. Div {
  5. Width: 500px;
  6. Height: 500px;
  7. Border: 1px solid #666;
  8. Overflow: hidden;
  9. Position: relative;
  10. Display: table-cell;
  11. Text-align: center;
  12. Vertical-align: middle
  13. }
  14. Div p {
  15. Position: static;
  16. + Position: absolute;
  17. Maximum: 50%
  18. }
  19. Img {
  20. Position: static;
  21. + Position: relative;
  22. Top:-50%; left:-50%;
  23. }
  24. -->
  25. </Style>
  26. <Div> <p> </p> </div>

 

 

Css method 2

Html generation
  1. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <Html xmlns = "http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  5. <Title> vertical center of images in a div </title>
  6. <Style type = "text/css">
  7. <! --
  8. Body {
  9. Margin: 0; padding: 0
  10. }
  11. Div {
  12. Width: 500px;
  13. Height: 500px;
  14. Line-height: 500px;
  15. Border: 1px solid #666;
  16. Overflow: hidden;
  17. Position: relative;
  18. Text-align: center;
  19. }
  20. Div p {
  21. Position: static;
  22. + Position: absolute;
  23. Maximum: 50%
  24. }
  25. Img {
  26. Position: static;
  27. + Position: relative;
  28. Top:-50%; left:-50%;
  29. Vertical-align: middle
  30. }
  31. P: after {
  32. Content: "."; font-size: 1px;
  33. Visibility: hidden
  34. }
  35. -->
  36. </Style>
  37. </Head>
  38. <Body>
  39. <Div> <p> </p> </div>
  40. </Body>
  41. </Html>

 

 

Css method 3

H
  1. <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2. <Html xmlns = "http://www.w3.org/1999/xhtml">
  3. <Head>
  4. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
  5. <Title> vertical center of images in a div </title>
  6. <Style type = "text/css">
  7. <! --
  8. * {Margin: 0; padding: 0 ;}
  9. Div {
  10. Width: 500px; border: 1px solid #666;
  11. Height: 500px;
  12. Background: url ("http://www.google.com/intl/en/images/logo.gif") center no-repeat
  13. }
  14. -->
  15. </Style>
  16. </Head>
  17. <Body>
  18. <Div> </div>
  19. </Body>
  20. </Html>

 

Js Control Method 1:

<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "Default3.aspx. cs" Inherits = "Default3" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled faces </title>

<Script type = "text/javascript">
Function setCenter (Xelement)
{
Var parent = Xelement. parentNode;
Parent. style. position = "relative ";
Xelement. style. position = "absolute ";
Var left = (parent. clientWidth-Xelement.clientWidth)/2;
Var top = (parent. clientHeight-Xelement.clientHeight)/2;
Xelement. style. left = left + "px ";
Xelement. style. top = top + "px ";
}
</Script>

</Head>
<Body>
<Form id = "form1" runat = "server">
<Div id = "picbox" style = "width: 200px; height: 200px; background: # ccc;">

</Div>
</Form>
</Body>
</Html>

 

 

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.