【问题标题】:Align div to page center with dynamic size使用动态大小将 div 与页面中心对齐
【发布时间】:2023-03-03 01:52:01
【问题描述】:

我看到了很多如何将 div 居中到页面中间的示例,但在所有示例中,div 的大小都是固定的。
如何将 div 放在页面中心且 div 高度未知大小?
(中间的灰色 div)

My code:

<!DOCTYPE HTML>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <style>
            html,body{
                margin: 0;
                padding: 0;
                border: 0;
            }
            body{
                background-color: transparent; 
                overflow: hidden;
                font-family: "Helvetica"
                    ;
                    color: #359115;
                    font-weight:bold;
                    }

                    #wrapper {
                        position: absolute;
                        width: 100%;
                        height: 100%;
                        text-align: center;
                    }

                    #container {
                        background-color: #dddddd;
                        /*height: 100%;
                        widows: 100%;*/
                        margin: 0,auto;
                        text-align: center;
                    }



        </style>
    </head>
    <body>
        <div id="wrapper">
            <div id="container" align="center">
                <span class="currency">$ </span><span class="integer">4,080,048.</span><span class="decimal">00</span>
            </div>
        </div>
    </body>
</html>

编辑:

我需要它来为 Android 和 IOS 中的 webView 工作,由于某种原因,数字总是出现在浏览器的顶部。
注意:浏览器不是全屏的!

截图在我的另一个question

【问题讨论】:

  • 您的 CSS 中存在语法错误:margin: 0,auto; 应为 margin: 0 auto;
  • 还是不行
  • 在您的示例代码中,#container 是全宽的,从技术上讲是“居中”。你想达到什么目的?
  • 您想让#container 填满屏幕并使内容垂直和水平居中吗?
  • 我假设他想将内容垂直居中,因为他提到了未知的高度

标签: android html ios css webview


【解决方案1】:

如何将 div 放在页面中心,div 高度大小未知?

涉及二维变换的简单解决方案(X 轴和 Y 轴的平移值为负):

http://jsbin.com/itifad/1/edit

CSS

div {
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);    /* Older Gecko browser */
  -ms-transform: translate(-50%, -50%);     /* IE9+ */
  -o-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}

这就是全部:无需使用 javascript 或嵌套元素来定义父容器的高度或使用棘手的显示属性。

进一步参考:http://css-tricks.com/centering-percentage-widthheight-elements/

浏览器支持:http://caniuse.com/transforms2d(不适用于IE&lt;=8

【讨论】:

    【解决方案2】:

    这应该可行:

    <!DOCTYPE HTML>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
            <style>
                body{
                    margin: 0; /* There is no margin, padding or border on html, only margin: 8px; on body... */
                    background-color: #FFFFFF; /* Why transparent? Changed to white... */ 
                    font-family: "Helvetica";
                    color: #359115;
                    font-weight: bold;
                }
                #wrapper {
                    position: relative;
                    width: 50%;
                    margin: auto;
                    text-align: center;
                }
                #container {
                    background-color: #dddddd;
                    margin: 0 auto;
                    text-align: center;
                }
    
    
    
            </style>
        </head>
        <body>
            <div id="wrapper">
                <div id="container" align="center">
                    <span class="currency">$ </span><span class="integer">4,080,048.</span><span class="decimal">00</span>
                </div>
            </div>
        </body>
    </html>
    

    如果你想让它在绝对中心,x 和 y,你需要在样式中改变这个:

            #wrapper {
                position: relative;
                width: 50%;
                margin: auto;
                text-align: center;
            }
            #container {
                background-color: #dddddd;
                margin: 0 auto;
                text-align: center;
            }
    

    到这里:

            #wrapper {
                position: relative;
                width: 50%;
                height: 50%;
                margin: auto;
                text-align: center;
            }
            #container {
                background-color: #dddddd;
                margin: auto;
                text-align: center;
            }
    

    【讨论】:

      【解决方案3】:

      display:table 是完成此任务的最简单方法。见这里:http://jsfiddle.net/bRgrf/4/

      html,body{
                      margin: 0;
                      padding: 0;
                      border: 0;
          height:100%;
          width:100%;
                  }
                  body{
                      background-color: transparent; 
                      overflow: hidden;
                      font-family: "Helvetica";
                          color: #359115;
                          font-weight:bold;
          display:table;
                          }
      
      
                          #container {
                              background-color: #dddddd;
                              height: 100%;
                              width: 100%;
                              margin: 0;
                              text-align: center;
                              vertical-align:middle;
          display:table-cell;
                          }
      

      【讨论】:

        【解决方案4】:

        我会这样做。对于 HTML,在您的内容周围添加一个 .wrapper 容器:

        <div id="container"> 
            <div class="wrapper">
            <span class="currency">₪</span><span class="integer">4,080.</span><span class="decimal">00</span>
            </div>
        </div>
        

        假设您想将此页面(视口)居中,我将使用以下 CSS

        html, body {
            margin: 0;
            padding: 0;
            border: 0;
            height: 100%;
        }
        body {
            background-color: transparent;
            overflow: hidden;
            font-family:"Helvetica";
            color: #359115;
            font-weight:bold;
        }
        #container {
            display: table;
            height: 100%;
            width: 100%;
            background-color: #dddddd;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
        }
        #container .wrapper {
            display: table-cell;
            height: 100%;
            vertical-align: middle;
            text-align: center;
        }
        

        这是如何工作的

        #container 的显示类型设置为table,并使用绝对定位使其拉伸到视口的边缘。

        设置.wrapper为显示类型table-cell,设置高度为100%,然后使用vertical-align: middle得到垂直 居中,text-align: center 用于水平居中。

        演示小提琴:http://jsfiddle.net/audetwebdesign/mBDcg/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-10-14
          • 1970-01-01
          • 2013-09-27
          • 1970-01-01
          • 2022-09-23
          • 2015-03-03
          • 2012-02-15
          相关资源
          最近更新 更多