如何使锚标签和按钮标签具有相同的样式?

问题描述 投票:0回答:1

我正在使用React,我想呈现一个按钮元素或一个Link(转换为锚标记)

if (this.props.link) {
  e = (
    <Link
      className={["button link", this.props.className].join(' ')}
      to={this.props.link}
    >
      {this.props.buttonLabel}
    </Link>
  )
} else {
  e = (
    <button
      className={["button", this.props.className].join(' ')}
      type={this.props.type}
    >
      {this.props.buttonLabel}
    </button>
  );
}

return e;

我查找了默认按钮和锚点样式并对其进行了规范化。我仍然有一个问题。上的文字垂直和水平居中,但上的文字仅水平居中。我什至可以将所有计算出的样式复制到按钮上,并将它们应用于anchor标签所共享的同一类。为了安全起见,我还标准化了.link类,我相信它是由React Links添加的。

这里出了什么问题?这是CSS供参考。

.button, .link {
  /* Normalize anchors nad buttons */
  display: inline-block;
  cursor: pointer;
  outline: 0;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  background-color: var(--primary);
  color: black;
  text-decoration: none !important;
  text-shadow: none;
  font: unset;
  font-family: 'Source Sans Pro';
  font-size: 1rem;
  text-align: center;
  text-rendering: auto;
  text-indent: 0;
  text-transform: none;
  align-items: unset;
  justify-content: center;
  vertical-align: middle;
  line-height: 1rem;
  height: 3rem;
  width: 8rem;
  writing-mode: horizontal-tb;
  word-spacing: 0;
  font-stretch: 100%;
  font-style: normal;
  font-variant-caps: normal;
  font-variant-east-asian: normal;
  font-variant-ligatures: normal;
  font-variant-numeric: normal;
  letter-spacing: normal;
  -webkit-appearance: none;
  -webkit-border-image: none;
}
javascript html css reactjs react-router
1个回答
0
投票

您有两个选择;

  1. 设置display: tabel-cell;,使您的vertical-align: middle;适用;或
  2. 设置line-height: 3rem;,使其与您的按钮高度匹配。
© www.soinside.com 2019 - 2024. All rights reserved.