【问题标题】:<button> element display-property differently than <a> element [duplicate]<button> 元素显示属性不同于 <a> 元素 [重复]
【发布时间】:2019-09-10 09:15:10
【问题描述】:

我正在尝试设计自定义按钮。为此,我创建了以下 css(参见下面的 sn-p)。

当我将 css 与 &lt;button&gt; 一起使用时,它会按预期工作。如果我将它与 &lt;a&gt; 元素一起使用,则按钮的宽度为 100%。我知道使用display-block 是这个原因,但我不知道为什么它适用于我的按钮而不适用于我的&lt;a&gt; 元素。它们都具有相同的显示属性,我认为它们的行为方式应该相同。

(按钮应在彼此下方对齐并放置在父.buttonRow的中间)。

两个按钮应该看起来像第一个示例(使用&lt;button&gt;

.buttonRow {
  display: block;
  text-align: center;
}

.button {
  display: block;
  margin: 0 auto;
  margin-bottom: 15px;
  background-color: #ccad91;
  color: white;
  font-size: 18px;
  padding: 17px 50px;
  border-radius: 35px;
  border: none;
  outline: none;
  outline: 0;
}
<div class="buttonRowl">
  <button class="button">Proceed</button>
  <a class="button" href="index.html">Cancel</a>
</div>

【问题讨论】:

  • 你不需要使用 flexbox ...只需考虑 display:table 用于按钮和 a

标签: html css twitter-bootstrap


【解决方案1】:

按钮行为不同的原因是按钮是所谓的“replaced element”,其(外观和)尺寸由操作系统/浏览器定义。无论如何,我建议在这里使用 flex-box。

.buttonRow {
  display: flex;
  flex-direction: column;
}

.button {
  margin: 0 auto 15px auto;
  background-color: #ccad91;
  color: white;
  font-size: 18px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  padding: 17px 50px;
  border-radius: 35px;
  border: none;
  outline: none;
  text-decoration: none;
}
<div class="buttonRow">
  <button class="button">Proceed</button>
  <a class="button" href="index.html">Cancel</a>
</div>

【讨论】:

  • Flexbox 似乎可以工作。只是好奇:你知道为什么它在我的例子中不起作用吗?除了调节按钮显示方式的 display-property 之外,似乎还有其他东西
  • 我更新了我的答案来回答你关于为什么它不能像执行那样工作的问题
猜你喜欢
  • 1970-01-01
  • 2020-09-08
  • 2019-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多