css - 溢出:隐藏不适用于伪元素

标签 css overflow pseudo-element

<分区>

我有一个红色方 block (div) 和一个橙色条作为伪元素 (before)。
我想隐藏父方 block 之外的橙色栏部分,所以我在父方 block 上使用了 overflow: hidden;,但它不起作用。

.square {
  width: 3.5em;
  height: 3.5em;
  
  background-color: red;
  overflow: hidden;
}

.square::before {
  content: "";
  position: absolute;
  transform: translate(2em);
  width: 4.95em;
  height: .65em;
  background-color: orange;
}
<div class="square"></div>

最佳答案

您需要将position: relative设置为.square

.square {
  width: 3.5em;
  height: 3.5em;
  position: relative; /* Added */
  background-color: red;
  overflow: hidden;
}

.square::before {
  content: "";
  position: absolute;
  transform: translate(2em);
  width: 4.95em;
  height: .65em;
  background-color: orange;
}
<div class="square"></div>

关于css - 溢出:隐藏不适用于伪元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51222184/

上一篇:html - 如果高度太大,如何限制显示多少图像?

下一篇:javascript - 如何编写 JS 事件和 CSS 过渡时间?

相关文章:

javascript - 将 div 对象标记为选中

javascript - Canvas 对齐问题

html - 用 flex 连接对 Angular 线 div

jQuery : Scroll To - Change the div ID

html - "pseudo"在 CSS 中是什么意思?

Javascript动态获取盒子的高度

java - "<"或 ">"运算符如何溢出?

c++ - 如何增加 Visual Studio C++ 中的堆栈大小?

CSS 不是带有后内容的选择器

html - 将悬停事件链接到 2 个或更多元素