【问题标题】:Flexbox and absolute positioning not working in Chrome and SafariFlexbox 和绝对定位在 Chrome 和 Safari 中不起作用
【发布时间】:2018-09-12 00:52:00
【问题描述】:

我有一个具有以下样式的按钮类:

.button {
  align-items: center;
  background-color: #fff;
  border: 2px solid;
  border-color: #f48120;
  color: #f48120;
  cursor: pointer;
  display: inline-flex;
  font-size: 20px;
  font-weight: bold;
  justify-content: center;
  margin-bottom: 5px;
  padding: 3px 10px;
  position: relative;
  text-transform: lowercase;
}

.button::after {
  background-color: #fff;
  bottom: -4px;
  content: '\f111';
  display: flex;
  font-family: 'FontAwesome';
  font-size: 5px;
  justify-content: center;
  position: absolute;
  width: 25%;
}
<button class="button">Enviar</button>

如果我取消选中然后再次检查检查器中的“位置:绝对”属性,则圆圈将在中心对齐。这是 Blink/Webkit 错误吗?

【问题讨论】:

    标签: html css flexbox css-position


    【解决方案1】:
    【解决方案2】:

    绝对元素不会被父对齐定位。只需将其放在左侧并将其转换为居中即可。

    只是一个旁注,没有必要在绝对元素上使用display: flex;

    .button {
      align-items: center;
      background-color: deeppink;
      border: 2px solid;
      border-color: aqua;
      cursor: pointer;
      display: inline-flex;
      font-size: 20px;
      font-weight: bold;
      justify-content: center;
      margin-bottom: 5px;
      padding: 3px 10px;
      position: relative;
      text-transform: lowercase;
    }
    
    .button:after {
      content: '';
      background-color: deepskyblue;
      bottom: -4px;
      font-size: 5px;
      position: absolute;
      width: 25%;
      height: 10px;
      left: 50%;
      transform: translateX(-50%);
    }
    <button type="button" class="button">Submit</button>

    【讨论】:

    • 谢谢!有效。但是,我认为这更像是一种解决方法,因为根据this,样式应该像我拥有的​​那样工作。你使用display: flex 是对的(我试图让圆圈居中); text-align: center 完成这项工作。
    • @JuanS。我的解决方案不是解决方法,这就是您定位绝对项目并永远拥有的方式。您参考的文章只会适用与其兼容的浏览器,我猜IE和Edge属于该类别。
    • 是的,你是对的,对不起。我对元素的定位方式感到困惑。 left 相对于其父元素定位元素,translate 相对于自身定位,这确实有意义。
    猜你喜欢
    • 2016-10-26
    • 1970-01-01
    • 2017-09-28
    • 2017-10-02
    • 2017-05-15
    • 2013-07-19
    • 2014-10-14
    • 2013-09-30
    • 1970-01-01
    相关资源
    最近更新 更多