【问题标题】:CSS element created with :before and :after doesn't show使用 :before 和 :after 创建的 CSS 元素不显示
【发布时间】:2021-04-14 08:28:15
【问题描述】:

我尝试显示这样的 html 元素: here

这是一个底部带有箭头的正方形,我使用这种样式创建了它:

.square {
  min-width: 60px;
  height: 60px;
  margin-left: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #1db911;
  border-radius: 15px;
  font-size: 14px;
  color: #ffffff;
}

.square:after,
.square:before {
  bottom: 30%;
  left: 31%;
  border: solid transparent;
  content: ' ';
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
  display: block;
}

.square:after {
  border-color: rgba(238, 238, 238, 0);
  border-top-color: #1db911;
  border-width: 10px;
  margin-left: -10px;
  display: block;
}
.square:before {
  border-color: rgba(204, 204, 204, 0);
  border-top-color: #1db911;
  border-width: 10px;
  margin-left: -10px;
  display: block;
}

但是有一个问题。当我水平滚动三角形时: here 这是我的父元素: here 可以水平滚动。 我也尝试将position: relative 用于square 类,但它存在问题。它没有在正方形上显示三角形。 像这样: here(我将三角形涂成黑色以更好地显示) 这是我在这个州的风格

.square {
  min-width: 60px;
  height: 60px;
  margin-left: 5px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #1db911;
  border-radius: 15px;
  font-size: 14px;
  color: #ffffff;
  position: relative;
}

.square:after,
.square:before {
  bottom: -22%;
  left: 50%;
  border: solid transparent;
  content: ' ';
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
  display: block;
}

.square:after {
  border-color: rgba(238, 238, 238, 0);
  border-top-color: black;
  border-width: 10px;
  margin-left: -10px;
  display: block;
}

.square:before {
  border-color: rgba(204, 204, 204, 0);
  border-top-color: #1db911;
  border-width: 10px;
  margin-left: -10px;
  display: block;
}
<div class="square">
  <span>sample</span>
</div>

谢谢!

【问题讨论】:

标签: html css css-selectors pseudo-element


【解决方案1】:

您可以使用top:100%而不是底部,如果您希望正方形仅与内容一样宽,您可以使用inline-flex

.square {
  min-width: 60px;
  height: 60px;
  margin-left: 5px;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  background-color: #1db911;
  border-radius: 15px;
  font-size: 14px;
  color: #ffffff;
  position: relative;
}

.square:after,
.square:before {
  top: 100%;
  left: 50%;
  border: solid transparent;
  content: ' ';
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
  display: block;
}

.square:after {
  border-color: rgba(238, 238, 238, 0);
  border-top-color: #1DB911;
  border-width: 10px;
  margin-left: -10px;
  display: block;
}

.square-container:before {
  border-color: rgba(204, 204, 204, 0);
  border-top-color: #1db911;
  border-width: 10px;
  margin-left: -10px;
  display: block;
}
<div class="square">
  <span>sample</span>
</div>

【讨论】:

  • 如果你运行 sn-p,它显然会起作用 - 如果它在你的代码中不起作用,那么你需要创建一个 minimal reproducible example 来展示代码中不起作用的东西提供,以上解决了问题
  • 谢谢。它不起作用,因为它的父宽度。感谢投票问题
猜你喜欢
  • 2015-09-12
  • 2016-05-24
  • 2021-12-30
  • 1970-01-01
  • 2020-09-20
  • 2012-12-17
  • 1970-01-01
  • 2014-02-08
相关资源
最近更新 更多