Css method 1
Html generation
- <Style type = "text/css">
- <! --
- * {Margin: 0; padding: 0}
- Div {
- Width: 500px;
- Height: 500px;
- Border: 1px solid #666;
- Overflow: hidden;
- Position: relative;
- Display: table-cell;
- Text-align: center;
- Vertical-align: middle
- }
- Div p {
- Position: static;
- + Position: absolute;
- Maximum: 50%
- }
- Img {
- Position: static;
- + Position: relative;
- Top:-50%; left:-50%;
- }
- -->
- </Style>
- <Div> <p> </p> </div>
Css method 2
Html generation
- <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <Html xmlns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- <Title> vertical center of images in a div </title>
- <Style type = "text/css">
- <! --
- Body {
- Margin: 0; padding: 0
- }
- Div {
- Width: 500px;
- Height: 500px;
- Line-height: 500px;
- Border: 1px solid #666;
- Overflow: hidden;
- Position: relative;
- Text-align: center;
- }
- Div p {
- Position: static;
- + Position: absolute;
- Maximum: 50%
- }
- Img {
- Position: static;
- + Position: relative;
- Top:-50%; left:-50%;
- Vertical-align: middle
- }
- P: after {
- Content: "."; font-size: 1px;
- Visibility: hidden
- }
- -->
- </Style>
- </Head>
- <Body>
- <Div> <p> </p> </div>
- </Body>
- </Html>
Css method 3
H
- <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <Html xmlns = "http://www.w3.org/1999/xhtml">
- <Head>
- <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
- <Title> vertical center of images in a div </title>
- <Style type = "text/css">
- <! --
- * {Margin: 0; padding: 0 ;}
- Div {
- Width: 500px; border: 1px solid #666;
- Height: 500px;
- Background: url ("http://www.google.com/intl/en/images/logo.gif") center no-repeat
- }
- -->
- </Style>
- </Head>
- <Body>
- <Div> </div>
- </Body>
- </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>