伪元素的内容属性中的垂直中心文本

问题描述 投票:0回答:1

我试图在我的content pseudo-elementafter属性中居中垂直文本。我搜索了一些答案,找到了属性vertical-align: baseline;display: inline-block;,但它没有用。我也尝试了vertical-align: middle;vertical-align: -50%display: block;(也来自其他答案)的合并,但无法弄清楚为什么它不起作用。目前我的"!"在底部并没有像我预期的那样垂直居中。有任何想法吗?

.wrapper {
  width: 25px;
  height: 25px;
  border-radius: 25px;
  border: 1px solid #93B8C2;
  padding: 5px;
  position: relative;
}

.wrapper::after {
  content: "!";
  width: 10px;
  height: 10px;
  border-radius: 10px;
  background: #DD3322;
  display: inline-block;
  color: white;
  text-align: center;
  position: absolute;
  right: 0;
  top: 25px;
  vertical-align: baseline;
  padding: 3px;
}
<div class="wrapper"></div>
html css pseudo-element
1个回答
1
投票

找到了我自己的答案:我用flexbox属性display: flex;align-items: center;justify-content: center;解决了它,以解决pseuo-element的垂直对齐问题。我也可以删除text-alignvertical-align属性。

方案如下:

.wrapper {
  width: 25px;
  height: 25px;
  border-radius: 25px;
  border: 1px solid #93B8C2;
  padding: 5px;
  position: relative;
}

.wrapper::after {
  content: "!";
  width: 10px;
  height: 10px;
  border-radius: 10px;
  background: #DD3322;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: absolute;
  right: 0;
  top: 25px;
  padding: 3px;
}
<div class="wrapper"></div>
© www.soinside.com 2019 - 2024. All rights reserved.