居中的内嵌块兄弟伸展到剩余的宽度

时间:2017-03-22 15:39:28

标签: html css

我有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>

即使中间宽度会改变,我怎么能得到相同的结果?

1 个答案:

答案 0 :(得分:1)

您可以使用display: flex;上的.container来获取所需的输出: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;
    }

如果您更改中间的文字,它会使其居中并拉伸其他两个文字以获取容器的剩余宽度。