html - 如何在其中放置一个包含两个 DIV 的 DIV,以便 DIV 填充屏幕的 90%?

标签 html css

很抱歉一直问同一个问题的不同版本,但这似乎很难实现。这是我到目前为止的代码:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>

   <style type="text/css">

       body, html{
    height: 100%;
}
        #outer {
            width: 90%;
            height: 90%;
            margin: 5% 5% 5% 5%;
            background-color: #333;
        }
        #left-content {
            height: 100%;
            width: 50%;
            float:left;
        }
        #right-content {
            height: 100%;
            width: 50%;
            float:left;
        }
    </style>

    <div id="outer">
      <div id="left-content" style="background-color: red;">xx</div>
      <div id="right-content" style="background-color: yellow;">xx</div> 

<!-- we need to clear -->
<br style="clear:both" />

    </div>

</body>
</html>

现在我似乎看到了滚动条,但我只希望外部 DIV 占据屏幕的 90%,并且没有滚动条。

找到 fiddle here .

最佳答案

这是一个我从未见过的非常有趣的错误。不用讨厌的 body { overflow:hidden; } 方法,我找到了一些修复方法:

1 - 使用 display:inline-block(不是真正想要的)

    #outer {
      display:inline-block;
        width: 90%;
        height: 90%;
        margin: 5% 5% 5% 5%;
        background-color: #333;
    }

2 - 使用填充而不是边距(不是真正想要的)

    #outer {
        width: 90%;
        height: 90%;
        padding: 5% 5% 5% 5%;
        background-color: #333;
    }

3 - 使用绝对位置(推荐)

    #outer {
        position:absolute;top: 5%;bottom: 5%;right: 5%;left: 5%;
        background-color: #333;
    }

我将在进一步调查此问题时编辑此答案。

根据 http://www.w3.org/TR/CSS2/box.html#box-dimensions

The percentage is calculated with respect to the width of the generated box's containing block. Note that this is true for 'margin-top' and 'margin-bottom' as well.

这意味着通过将 90% 的宽度放在 body 上,将导致 5% 的边距成为 90% 中的 5%,而不是预期的 100%,这会导致“错误”。 - 同样适用于填充。

关于html - 如何在其中放置一个包含两个 DIV 的 DIV,以便 DIV 填充屏幕的 90%?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14207600/

相关文章:

CSS剪辑使用GPU?

css - 响应式视频不适合视频容器

html - HTML中哪些字符需要转义?

javascript - 测试页面是否包含任何损坏的 JS/CSS

Javascript 游标替换

php - 如何根据 php 标题的长度调整 margin-top 属性?

javascript - 均匀间隔的响应式瓷砖网格 - 纯 css 有可能吗?

javascript - AngularJS 过滤器;将数字格式化为字符串,反之亦然?

javascript - 如何最好和快速地隐藏 JavaScript 不需要的选项

javascript - 如何将外部 Javascript 包含到 html5