首页 > 解决方案 > 文字不显示

问题描述

.sport
{
  content: url("../img/sport.png");
  background-color: rgba(0,0,0,1.0);
  top: 61px;
  height: 310px;
  width: 300px;
  position: absolute;
  margin: 0;
  left: 0;
  border-radius: 4px;
  overflow: hidden;
}

.sportsandactivis
{
  background-color: rgba(255,255,255,0.0);
  top: 142px;
  height: auto;
  width: auto;
  position: absolute;
  margin: 0;
  left: 48px;
  font-family: 'Montserrat',Helvetica,Arial,serif;
  font-weight: 700;
  font-style: normal;
  font-size: 22.0px;
  color: rgba(255,255,255,1.0);
  text-align: center;
  line-height: 27.0px;
}
<div class="sport">
  <p class="sportsandactivis">Sports and Activis</p>
</div>


   

图片显示正确,但“sports and activitis”里面的文字没有出现,为什么?

标签: htmlcss

解决方案


您的文字没有出现,因为您正在使用content! 这完全取代了你.sport元素的内容!…</p>

content通常用于在伪元素上生成内容::before::after但不应用于更改 HTML 元素的内容。

https://developer.mozilla.org/en-US/docs/Web/CSS/content

…您必须要background-image改用:

.sport
{ /* Changed "content" to "background-image", and added a working meow image */ 
  background-image: url("http://placekitten.com/300/310"); 
  background-color: rgba(0,0,0,1.0);
  top: 61px;
  height: 310px;
  width: 300px;
  position: absolute;
  margin: 0;
  left: 0;
  border-radius: 4px;
  overflow: hidden;
}

.sportsandactivis
{
  background-color: rgba(255,255,255,0.0);
  top: 142px;
  height: auto;
  width: auto;
  position: absolute;
  margin: 0;
  left: 48px;
  font-family: 'Montserrat',Helvetica,Arial,serif;
  font-weight: 700;
  font-style: normal;
  font-size: 22.0px;
  color: rgba(255,255,255,1.0);
  text-align: center;
  line-height: 27.0px;
}
<div class="sport">
  <p class="sportsandactivis">Sports and Activis</p>
</div>

希望能帮助到你。


推荐阅读