伪元素的中心内容

时间:2014-03-31 23:11:25

标签: css pseudo-element

我在特定元素的:before上显示了:hover个伪元素。

我使用字体很棒,并希望垂直居中:before的内容,但垂直对齐,边距等并没有多大帮助。

有什么想法吗?

.tile:before {
  font-family: FontAwesome;
  font-size: 150px;
  color: white;
  text-align: center;
  border-radius: 15px;  
  opacity: 0;
  z-index: 9999;
  width: 100%;
  height: 100%;
  content: "\f16b";
  position: absolute;
  background-color: rgba(219,127,8, 0.7);
  -webkit-transition: all 0.3s ease-in-out;
  -moz-transition: all 0.3s ease-in-out;
  -o-transition: all 0.3s ease-in-out;
  -ms-transition: all 0.3s ease-in-out;
  transition: all 0.3s ease-in-out;
}
.tile:hover:before {
  opacity: 1;
}

2 个答案:

答案 0 :(得分:1)

以下是针对.tile的一些潜在建议:之前:

1 - 使用像素值而不是100%的高度:

height: 100px;

2 - 确保将其显示为可以接受边距,填充等的元素。

display: block;

-OR -

display: inline-block;

3 - 我知道你说你试过边距,但是你尝试过填充顶部吗?

padding-top: 20px;

4 - 尝试将溢出设置为隐藏或可见。这通常会迫使元素表现得更好。"

overflow:hidden;

我会一起尝试所有这些,看看会发生什么。

最后,我可能会尝试设置" top:"价值,因为你有" position:absolute;"已经。也许尝试与"位置:相对;"太

top: 10px;

真的需要所有代码(HTML)来说明什么可行。

答案 1 :(得分:1)

使用:before作为封面背景显示在 tile 元素的顶部,使用:after显示:

.tile:after {
  top: 50%;
  left: 50%;
  /* Both half of font-size */
  margin-left: -75px;
  margin-top: -75px;
  height: 150px;
  line-height: 1;
}

似乎要做的伎俩。谢谢大家。

相关问题