html - 带有填充的代码元素扩展到其容器之外

标签 html css

当我将代码元素上的填充设置得足够大时,它会导致填充超出其父容器的边界:

html:

<div class="parent">
  <div class="top">
    Top Line
  </div>
  <div>
    <p>
      <code>
        Bottom Line
      </code>
    </p>
  </div>
</div>

CSS:

.parent {
  display: flex;
  flex-direction: column;
}

.top {
  margin-bottom: 5px;
}

code {
  background-color: #c0c0c0;
  padding: 10px;
}

p {
    margin-bottom: 5px;
    margin-top: 0px;
}

https://jsfiddle.net/Deepview/ag6977mL/6/

要查看此内容,请将代码元素上的内边距设置为 30 像素。它向上延伸并超过第一条线。

这似乎是因为代码元素嵌套在段落中,而段落又嵌套在 div 中。如果我删除 div 和段落,问题就会得到纠正,并且代码元素不会超出顶行。我真的需要保留 div 和段落。如果填充很大,我如何才能避免这种情况的发生并让代码元素的内部文本始终向下移动?

最佳答案

.parent {
  display: flex;
  flex-direction: column;
}

.top {
  margin-bottom: 5px;
}

code {
  background-color: #c0c0c0;
  padding: 30px;
  display: inline-block;
}

p {
    margin-bottom: 5px;
    margin-top: 0px;
}

You just add display:inline-block;

关于html - 带有填充的代码元素扩展到其容器之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45610135/

相关文章:

html - 在开始时声明文档类型更改标题的外观

html - 相对于 DIV 的固定容器——这可能吗?

html - 设置在ie7中工作的div的背景颜色的不透明度

HTML 表格边框间距

html - 如果不注释掉返回,平均划分列是行不通的,有没有更好的方法?

javascript - 将透明图像 (PNG) 保存为背景图像

javascript - Bootstrap 4 崩溃在一半停止然后打开时无法顺利工作

html - 在导航栏中排列标题

php - 如何通过单选按钮创建列表评级帖子?

css - 为什么此文本不显示 CSS 中指定的颜色?