html - (margin/padding)-right 不会为溢出容器创建空间
问题描述
由于某种原因,在溢出的容器中,右侧的填充没有显示。
.parent {
background-color: orange;
width: 150px;
height: 50px;
overflow: auto;
padding-right: 15px;
}
.child {
background-color: blue;
width: 250px;
height: 100%;
margin: 0 10px;
}
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
<div class="parent">
<div class="child">
</div>
</div>
我希望滚动到最后时会出现橙色(右)
解决方案
让我们从不应用任何overflow
属性开始。我们显然在它的父容器之外有元素(添加容器的填充将保留在里面):
.parent {
background-color: orange;
width: 150px;
height: 100px;
padding:15px;
}
.child {
background-color: blue;
width: 250px;
height: 100%;
}
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
<div class="parent">
<div class="child">
</div>
</div>
现在通过添加overflow:scroll
oroverflow:auto
您只需添加滚动即可查看溢出部分,并且您不会将填充视为例外:
.parent {
background-color: orange;
width: 150px;
height: 100px;
overflow:auto;
padding:15px;
}
.child {
background-color: blue;
width: 250px;
height: 100%;
}
body {
margin: 0;
padding: 0;
}
* {
box-sizing: border-box;
}
<div class="parent">
<div class="child">
</div>
</div>
与右边距相同的逻辑。当元素溢出时,没有空间在内部元素和父元素之间添加边距。
推荐阅读
- java - 如何获取我的 java 应用程序所需的传递依赖项列表?
- ios - 无法让社交媒体提要在 firebase 和 swift 中工作
- python - 图像在调整大小的最后一步变为空
- c - 我需要一个函数来初始化这个数组
- java - Android:从文本文件读取 - 完成异常打开失败:ENOENT(没有这样的文件或目录)
- sql - 如何设计用于存储目的地的表格或集合?
- c - 为什么在程序下面运行时,void print_size(int a[]) 函数中 sizeof(a) 的输出是 8,而 int main() 函数中 sizeof(a) 是 12?
- c++ - GLFWwindow * window 是否仍然指向有效的 glfw 对象?
- android - 如何在后按时以编程方式关闭 SearchView?在片段中
- google-cloud-firestore - Firestore 安全规则:如何访问令牌/自定义身份验证声明