【问题标题】:Centering text in a button without underlining the hover state. Why is this so hard?在按钮中居中文本而不强调悬停状态。为什么这么难?
【发布时间】:2026-02-22 06:10:01
【问题描述】:

我希望文本居中而不是装饰。正如你所看到的,我有很多text decoration:none CSS,但仍然没有骰子......我很困惑。 div 应该在链接内部还是外部?

HTML -

 <a href=""
       data-toggle="modal"
       data-target="#videoModal"
       data-theVideo="http://www.youtube.com/embed/loFtozxZG0s">
      <div class="round-button">
           <i class="fa fa-play"></i>  Watch Trailer
      </div>
    </a>

CSS -

.round-button {
  width:20%;
  width: 15em;
  height: 3em;
  margin-top: 1em;
  moz-border-radius: 15px;
  -webkit-border-radius: 15px;
  background: #f49131;
  vertical-align: middle;
  text-align:center;
  color:white;
  font-size: 1.4em;
  text-decoration:none;
  cursor:pointer;

}

.round-button:hover {
  background:#f46800;
  text-decoration:none;
}
.round-button a {
  display:block;
  vertical-align: middle;
  text-decoration:none;
}

.round-button a:hover {
  text-decoration: none;
}

常规状态 -

光标状态 -

【问题讨论】:

  • 它必须从文档中的其他位置拉入悬停类 - 看看浏览器中的开发人员控制台,您应该能够从那里定位/覆盖适当的类
  • @darrelbarrel 尝试 text-decoration:none!important;看看这是否是一个覆盖问题。
  • 是的,还有一个覆盖问题
  • @darrellbarrell 避免重要。看看你能不能正确选择它,否则你很快就会成为i.imgur.com/q4g0SYM.jpg这个家伙!
  • important 可以用来测试你的选择器是否正确

标签: html css button hover


【解决方案1】:

您的选择器不正确。

.round-button a 假定链接是 .round-button 类的后代,但实际上反过来也是如此。

a .round-button {
  width: 20%;
  width: 15em;
  height: 3em;
  line-height: 3em;
  margin-top: 1em;
  moz-border-radius: 15px;
  -webkit-border-radius: 15px;
  background: #f49131;
  vertical-align: middle;
  text-align: center;
  color: white;
  font-size: 1.4em;
  text-decoration: none;
  cursor: pointer;
}
a {
  text-decoration: none;
}
<a href="#" data-toggle="modal" data-target="#videoModal" data-theVideo="http://www.youtube.com/embed/loFtozxZG0s">
  <div class="round-button">
    <i class="fa fa-play"></i> Watch Trailer
  </div>
</a>

事实上,您可以通过不使用 div 并仅设置链接样式来简化整个事情

JSfiddle Demo

【讨论】:

  • 天哪,真的。在元级别上,为什么链接应该在 div 之外,反之亦然?
  • @darrellbarrell 你希望整个 div 变成一个链接,所以你把它放在 a 标签中。标签内的所有内容都成为链接。如果你愿意,你可以链接你的整个身体:)
  • 我还应该指出Links are not buttons