代码之家  ›  专栏  ›  技术社区  ›  ilhan

如何使窗体水平和垂直居中?

  •  2
  • ilhan  · 技术社区  · 14 年前

    我有一个 形式 身体 . 我正在尝试替换屏幕中间/中间的窗体。有没有一个合适的方法在CSS中做到这一点?

    -----------------------
    -                     -
    -                     -
    -                     -
    -          M          -
    -                     -
    -                     -
    -                     -
    -----------------------
    
    2 回复  |  直到 13 年前
        1
  •  1
  •   Mahdi.Montgomery    14 年前

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html>
     <head>
         <title>Vertical/Horizontal Align</title>
         <style type="text/css">
             form {
                 height:400px;
                 width:400px;
                 position: absolute;
                 left: 50%;
                 top: 50%;
                 background-color:#CCC;
                 /* Set margins to offset 50% of the w/h */
                 margin: -200px 0 0 -200px;
             }
         </style>
     </head>
     <body>
         <form action="/something">
             <p>
              <label>NAME: </label> <input type="text" name="Name" />
             </p>
             <p>
             <label>PHONE: </label> <input type="text" name="Phone" />
             </p>
         </form>
     </body>
     </html>
    

        2
  •  -1
  •   lock    14 年前

    form{
       width: 50%;
       margin: 0 auto;
    }