【问题标题】:Centered inline block sibling stretch to the remaining width [duplicate]居中的内联块兄弟拉伸到剩余宽度[重复]
【发布时间】:2017-08-14 20:16:12
【问题描述】:

我有 3 个内联块元素;

中间的居中。我想拉伸其他人以获取剩余的宽度容器。

这是我迄今为止尝试过的:

.container {
  width: 400px;
  height: 100px;
  text-align: center;
  background-color: yellow;
}

.line {
  border: dashed 1px #C7C9C7;
  display: inline-block;
  width: 38%;
}

.content {
  display: inline-block;
  background-color: red;
}
<div class="container">
  <hr class="line" />
  <div class="content">This is a text</div>
  <hr class="line" />
</div>

即使中间宽度发生变化,我怎样才能得到相同的结果?

【问题讨论】:

  • 这只是一个例子,或者您真的只是想在文本的两侧放置一条虚线?
  • 确实是一条虚线,最好让它也垂直对齐。我正在寻找一种中间带有文本的
    。谢谢
  • 为什么不直接使用:pseudo 元素:before:after 来制作边框呢?
  • 您能在您的提案中发布答案吗?

标签: html css


【解决方案1】:

您可以在.container 上使用display: flex; 来获得想要的输出:See this fiddle

    .container{
        width: 400px;
        height: 100px;
        text-align: center;
        background-color: yellow;
        display: flex;
        -ms-flex-align: center;
        -webkit-align-items: center;
        -webkit-box-align: center;

        align-items: center;
    }

如果你改变中间的文字,它会保持居中并拉伸另外两个以占据容器的剩余宽度。

【讨论】:

  • 感谢您的快速回答,有没有办法保持虚线与以前一样?
  • 不客气,我编辑了我的答案
猜你喜欢
  • 2013-07-31
  • 1970-01-01
  • 2019-09-28
  • 2019-06-18
  • 2018-10-28
  • 2017-06-30
  • 2013-04-20
  • 1970-01-01
  • 2020-01-12
相关资源
最近更新 更多