【问题标题】:Button height with display flex is not uniform in SafariSafari 中显示 flex 的按钮高度不统一
【发布时间】:2017-07-20 10:02:33
【问题描述】:

我有一个button 封装在div 下,结构如下:

HTML:

 <div class="parent-container">
      <div class="child child-1 col-xs-6">
        <button class="btn btn-primary">Customize Feed</button>
      </div>
      <div class="child child-2 col-xs-6">
        <button class="btn btn-default">Really Really Really Really Really Really Long CTA text</button>
      </div>
    </div>

CSS:

    .parent-container {
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-start;
    }

    .child-1 {
      order: 2;
    }

    .child-2 {
      order: 1;
    }

    .child button {
      height: 100%;
      white-space: normal;
    }

    .child button {
      width: 100%;
    }

这两个按钮(无论是长文本还是短文本)都应该占据相同的高度,无论其内容的长度如何。它适用于所有浏览器除了 macOS 中的 Safari。

Demo

问题截图 (来自演示)

【问题讨论】:

  • 谢谢。希望其他用户回答您的问题。编码愉快。

标签: html css flexbox vertical-alignment


【解决方案1】:

我对您的 CSS 进行了一些更改并使其正常工作。

button 的父元素需要有弹性框才能使按钮变得灵活

.parent-container {
  display: flex;
  flex-direction: row;
}

.child-1 {
  display: flex;
  flex: 1;
  order: 2;
}

.child-2 {
  order: 1;
}

.child button {
  flex: 1;
  white-space: normal;
  width: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="parent-container">
  <div class="child child-1 col-xs-6">
    <button class="btn btn-primary">Customize Feed</button>
  </div>
  <div class="child child-2 col-xs-6">
    <button class="btn btn-default">Really Really Really Really Really Really Long CTA text</button>
  </div>
</div>

JSFiddle

【讨论】:

  • 虽然它可以按需要工作,但如果 button 被设置为单行显示(对于移动设备),它不会占用相同的高度。猜猜为什么?
  • 不幸的是,元素具有相同高度的唯一方法是它们位于同一行。为了在它们位于不同行时匹配高度,您需要使用 JavaScript。
  • 是的,这是唯一的方法。谢谢!
猜你喜欢
  • 2021-03-10
  • 2017-06-15
  • 2014-04-20
  • 2021-07-30
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 2016-05-15
  • 2022-01-12
相关资源
最近更新 更多