gpt4 book ai didi

html - 在 CSS 中使用百分比大小和填充时无法防止嵌套 Div 溢出?

转载 作者:行者123 更新时间:2023-11-27 22:35:18 26 4
gpt4 key购买 nike

我希望能够使用这些属性对嵌套的 div 进行布局:

  • 宽度:100%
  • 高度:100%
  • 内边距:10px

我希望它是这样的,子元素是 剩余 空间的 100% 宽度和高度 计算填充之后,而不是之前。否则,当我有一个如下例所示的文档时, child 会使滚动条出现。但滚动条不是主要问题,子项超出父容器宽度的事实才是。

我可以使用所有 position: absolute 声明,但这似乎不对。这是代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=7">
<title>Liquid Layout</title>
<style>
body, html {
width:100%;
height:100%;
margin:0;
padding:0;
background-color:black;
}
#container {
position:relative;
width:100%;
height:100%;
background-color:red;
opacity:0.7;
}
#child1 {
position:relative;
width:100%;
height:100%;
padding:10px;
background-color:blue;
}
#nested1 {
position:relative;
background-color: white;
width:100%;
height:100%;
}
</style>
</head>
<body>
<div id="container">
<div id="child1">
<div id="nested1"></div>
</div>
</div>
</body>
</html>

我如何做到这一点,使用 position:relativeposition:static 和百分比大小,根据父项的宽度/高度减去子项的百分比大小填充和边距?我必须求助于 position:absolute 和 left/right/top/bottom 吗?

感谢您的帮助,兰斯

最佳答案

I want it to be such that, the children are 100% width and height of the remaining space after padding is calculated, not before.

Shiny 的 future 派方法是:

#something {
width: 100%; height: 100%; padding: 10px;
box-sizing: border-box; -moz-box-sizing: border-boz; -webkit-box-sizing: border-box;
}

然而,这在版本 8 之前的 IE 上不起作用。

Do I have to resort to position:absolute and left/right/top/bottom?

这是另一种方式,但是“边缘定位”(定位 topbottom 但不是 heightleft< 也类似/right/width) 在版本 7 之前的 IE 上不起作用。

the scrollbars are not the main issue, the fact that the child stretches beyond the width of the parent container is.

横向上没问题。将 width 保留为默认 auto ,它将接收其父级的完整宽度减去填充。唯一的问题是您不会通过 height 获得该行为。

混合方法:auto-width100% height with box-sizing,并添加一些hack 只在 IE6-7 运行的 JS 来修复那里的高度?

关于html - 在 CSS 中使用百分比大小和填充时无法防止嵌套 Div 溢出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2703631/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com