【问题标题】:Why does my CSS button default to a font that has no mention of it anywhere?为什么我的 CSS 按钮默认为在任何地方都没有提及的字体?
【发布时间】:2020-11-24 12:30:53
【问题描述】:

我一直在尝试更改此按钮上的字体,但它一直将其设置为默认设置。该代码引用了一个动画按钮,我希望将字体更改为“Roboto”。

.button {
  display: inline-block;
  border-radius: 0px;
  background-color: transparent;
  border: none;
  color: #ffffff;
  text-align: center;
  font-size: 28px;
  padding: 0px;
  width: 0px;
  font: roboto;
  transition: all 0.5s;
  cursor: pointer;
  margin: 0px;
}

.button span {
  cursor: pointer;
  display: inline-block;
  position: relative;
  transition: 0.5s;
}

.button span:after {
  content: '\00bb';
  position: absolute;
  opacity: 0;
  top: 0;
  right: 0px;
  transition: 0.5s;
}

.button:hover span {
  padding-right: 25px;
}

.button:hover span:after {
  opacity: 1;
  right: 0;
}
<a button class="button" style="vertical-align:middle" href="https://www.instagram.com/?hl=en"><span>Instagram</span></button>

【问题讨论】:

  • 您是否包含了 Roboto 字体文件?
  • 你为什么要用</button>关闭你的<a>text-align: centrefont: roboto; 都不是有效的 CSS 规则。是text-align: centerfont-family: Roboto;。使用browser console (dev tools)(点击F12)并阅读任何错误。
  • 当我从“

标签: html css fonts webfonts


【解决方案1】:

你需要使用 font-family 而不是字体。

.button {
  display: inline-block;
  /* Inserted font-family instead of font */
  font-family: Roboto;
  border-radius: 0px;
  background-color: transparent;
  border: none;
  color: #ffffff;
  text-align: centre;
  font-size: 28px;
  padding: 0px;
  width: 0px;
  transition: all 0.5s;
  cursor: pointer;
  margin: 0px;
}

.button span {
  cursor: pointer;
  display: inline-block;
  position: relative;
  transition: 0.5s;
}

.button span:after {
  content: '\00bb';
  position: absolute;
  opacity: 0;
  top: 0;
  right: 0px;
  transition: 0.5s;
}

.button:hover span {
  padding-right: 25px;
}

.button:hover span:after {
  opacity: 1;
  right: 0;
}
<a button class="button" style="vertical-align:middle" href="https://www.instagram.com/?hl=en"><span>Instagram</span></button>

【讨论】:

    【解决方案2】:

    font: roboto; 不起作用 - 它必须是 font-familiy: roboto,并且您最好添加至少一种替代方案,例如 font-familiy: roboto, sans-serif

    还要注意您的 HTML:您正在打开一个<a> 标记并关闭一个</ button> 标记。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多