【问题标题】:Why does my button still have underlined text?为什么我的按钮仍然有下划线文本?
【发布时间】:2022-12-19 18:30:04
【问题描述】:

我正在创建一个链接到 Youtube 的按钮,但按钮内的文本不响应 css 代码

我试过这个html代码:

<button class = "button"><a href = https://www.youtube.com>This is a button to youtube</a></button>

这个CSS:

.button {
  width:100px;
  height:100px;
  border-radius:12px;
  font-family: arial;
  color: black;
  font-size:15px;
  text-decoration: none;
  background-color:red;
  border-left: solid transparent;
  border-right: solid transparent;
  border-top: solid transparent;
  border-bottom: solid transparent;
  
  position:relative;
  top:10px;
}

【问题讨论】:

    标签: html css button


    【解决方案1】:

    应用文本样式时,您必须选择 a 元素,而不是 button 元素:

    .button {
      width: 100px;
      height: 100px;
      border-radius: 12px;
      background-color: red;
      border-left: solid transparent;
      border-right: solid transparent;
      border-top: solid transparent;
      border-bottom: solid transparent;
      position: relative;
      top: 10px;
    }
    
    .button > a {
      font-family: arial;
      color: black;
      font-size: 15px;
      text-decoration: none;
    }
    &lt;button class="button"&gt;&lt;a href = https://www.youtube.com&gt;This is a button to youtube&lt;/a&gt;&lt;/button&gt;

    【讨论】:

      【解决方案2】:

      如果使用标签,则不需要使用按钮标签

      <a href="https://www.youtube.com" class="button">This is a button to youtube</a>
      

      【讨论】:

        【解决方案3】:

        使用标签,浏览器有一些默认行为。如果你不喜欢,你可以使用 JavaScript 导航到 youtube

        <button class = "button" onclick="window.open('https://www.youtube.com', '_blank')">This is a button to youtube</button>
        

        【讨论】:

        猜你喜欢
        • 2014-08-03
        • 2016-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-04
        • 1970-01-01
        • 2015-10-21
        相关资源
        最近更新 更多