为元素之前的内容设置自己的样式

时间:2019-06-19 21:05:08

标签: html css

我刚刚创建了一个按钮。我发现我的内容不适合放置在地方的问题。

这是我的代码:

.btn-red {
  display: inline-block;
  padding: 13px 20px;
  color: #fff;
  text-decoration: none;
  position: relative;
  background: #f42f2c;
  font: 10px "Oswald", sans-serif;
  letter-spacing: 0.4em;
  text-align: center;
  text-indent: 2px;
  text-transform: uppercase;
  transition: color 0.1s linear 0.05s;
  border-radius: 0;
  border: 1px solid #f42f2c;
}
.btn-red:focus {
  box-shadow:none !important;
}
.btn-red::before {
  content: "READ MORE";
  display: block;
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  height: 1px;
  background: #f9f9ff;
  color:#f42f2c !important;
  z-index: 1;
  opacity: 0;
  transition: height 0.2s ease, top 0.2s ease, opacity 0s linear 0.2s;
}
.btn-red::after {
  transition: border 0.1s linear 0.05s;
}
.btn-red .btn-red-inner {
  position: relative;
  z-index: 2;
}
.btn-red:hover {
  transition: color 0.1s linear 0s;
  text-decoration: none;
  color: #f42f2c !important;
}
.btn-red:hover::before {
  top: 0;
  height: 100%;
  opacity: 1;
  transition: height 0.2s ease, top 0.2s ease, opacity 0s linear 0s;
}
.btn-red:hover::after {
  transition: border 0.1s linear 0s;
}
<a href="o-nas.php" class="btn-red">
            <span class="btn-inner">READ MORE</span>
        </a>

我尝试将padding: 13px 20px添加到.btn-red::before,但是它存在一些错误,并且我不知道该如何解决。

1 个答案:

答案 0 :(得分:3)

我想不用使用:before来控制按钮文本就可以达到您想要的效果。看下面:

.btn-red {
  position: relative;
  display: inline-flex;
  box-sizing: border-box;
  padding: 13px 20px;
  text-decoration: none;
  background-color: #f42f2c;
  border: 1px solid #f42f2c;
}

.btn-red:before {
  position: absolute;
  top: 50%;
  left: 0;
  width: 100%;
  height: 0;
  content: "";
  background-color: #fff;
  transform: translateY(-50%);
  transition: height 0.2s ease-in-out;
}

.btn-red .btn-inner {
  font: 10px "Oswald", sans-serif;
  color: #fff;
  line-height: 1em;
  letter-spacing: 0.4em;
  text-transform: uppercase;
  z-index: 1;
  transition: color 0.2s ease-in-out;
}

.btn-red:hover:before {
  height: 100%;
}

.btn-red:hover .btn-inner {
  color: #f42f2c;
}
<a class="btn-red" href="#" title="Read More">
  <span class="btn-inner">Read more</span>
</a>

希望这可以为您提供帮助。如果您对代码有任何疑问,请告诉我:)

干杯!