html - 页面中间的框?

标签 html css

我想用css在窗口中间放一个盒子,但是不行。例如,html 元素的高度似乎不是 100%。

<!DOCTYPE HTML>

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Box</title>

        <style type="text/css">
            html {
                width: 100%;
                height: 100%;
                display: table-cell;
                vertical-align: middle;
            }
            body {
                margin: auto;
                width: 300px;
                height: 300px;
                border: 1px solid black;
            }
        </style>

    </head>

    <body>
        box
    </body>
</html>

最佳答案

乱用 html 和 body 使其显示:table-cell 或其他东西不是一个好主意

相反,试试这个代码:

<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <title>Box</title>

        <style type="text/css">
            html {
                width: 100%;
                height: 100%;
            }
            div {
                width: 300px;
                height: 300px;
                position: absolute;
                left: 50%;
                top: 50%;
                margin: -150px 0 0 -150px;
                border: 1px solid black;
            }
        </style>

    </head>

    <body>
        <div>box</div>
    </body>
</html>

关于html - 页面中间的框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14775367/

相关文章:

html - 嵌套 <p> 和 <img>,如 '<p><img src=' '></p>'

asp.net - 自动调整文本区域大小(多行 asp :TextBox)

html - css 样式未应用于 html 中的 div

css - 首先 CSS 样式不会响应 HTML

java - 丰富的 :dataTable zebra cells with css

jquery - addClass 函数没有按预期工作

javascript - 滚动到每个 span 元素

javascript - 文件系统 API 在 Chrome v27 和 v29 中不起作用

javascript - 避免 navbar-toggler 在选择时改变颜色

java - 无法将 CSS 应用于 MouseEvent 上以编程方式创建的 Pane