html - (margin/padding)-right 不为溢出容器创建空间

标签 html css

由于某种原因,在溢出的容器中,右侧的填充没有显示。

.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>

enter image description here

现在通过添加 overflow:scrolloverflow:auto 你将简单地添加滚动来查看溢出部分并且你不会有异常(exception)的填充:

.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>

enter image description here

与 margin right 相同的逻辑。当元素溢出时,内部元素和父元素之间没有空间添加边距。

关于html - (margin/padding)-right 不为溢出容器创建空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50075176/

相关文章:

html - 未根据内部 div 设置 div 高度

javascript - 如何更改动态表中的特定图像?

html - 圆形加载动画

javascript - 使用简单脚本进行 HTML 和 JavaScript (jQuery) 过滤

javascript - 滚动时隐藏 Bootstrap 导航栏

jquery - 如果字体样式为斜体,IE7 在 slideToggle 上失败?

css - 更改 Bootstrap 中复选框的大小

javascript - 转换打破父溢出

css - 多个 nth-last-child 和/或 last-child 不工作

html - 为什么溢出: hidden affect width