【问题标题】:How to center caption with dynamic width button nearby?如何使用附近的动态宽度按钮使标题居中?
【发布时间】:2019-09-07 21:16:59
【问题描述】:

我有一个绿色的标题部分和两个对象:标题和按钮。我需要将标题相对于绿色部分居中,但它应该对按钮宽度增长做出反应,保持位置尽可能靠近中心而不相互重叠。 整个文本应该是可见的。 该按钮可以不存在 (*ngIf)。 标题文本也可以不同。

您可以更改 html 结构。仅限 CSS/sass。

在jsfiddle中查看我解决问题的尝试!

<div class="wrapper">
  <h1 class='title'>
    <span class='title-text'>
      Caption
    </span>
  </h1>

  <button>
    dynamic width
  </button>
</div>
.wrapper {
  display: flex;
  justify-content: space-between;
  background: green;
  padding: 10px;
}

h1 {
  margin: 0 auto;
  height: 40px;
}

【问题讨论】:

    标签: html css sass flexbox


    【解决方案1】:

    您应该将包装器设置为

    位置:相对; justify-content: 居中;

    然后绝对定位按钮

    位置:绝对; 右:10px;

    只需将 right 的值更改为您想要的结果即可。

    【讨论】:

      猜你喜欢
      • 2012-05-13
      • 2014-01-21
      • 1970-01-01
      • 2012-11-22
      • 2013-11-14
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      • 2021-08-22
      相关资源
      最近更新 更多