布局:一列尽可能宽,另一列占用剩余空间

时间:2017-01-26 16:11:12

标签: html css css3 flexbox alignment

我需要一个两列布局,就像一张表,我不知道列的宽度是多少。

我需要正确的列与其内容一样宽(没有自动换行),左列要占用剩余空间,如果没有可用空间则进行自动换行。

如果内容需要,右栏就可以了,而左栏mainInfos则在其上方。

我希望这些列并排。我该怎么做才能达到这个结果?

容器有一个固定的宽度。我不想使用jquery。

#post {
	overflow: hidden;
	width: 400px;
	border: solid 1px;
}
#post .mainInfos {
	overflow: hidden;
}
#post .details {
	float: right;
}
<div id="post">
	<div class="mainInfos">really really long content that should be wrapped and should be all at the left of "small content", on the same line</div>
	<div class="details">small content</div>
</div>

4 个答案:

答案 0 :(得分:2)

在新旧浏览器中使用的Flexbox的一种替代方法是在内部div上使用display:table-cell

#post {
  overflow: hidden;
  width: 400px;
  border: solid 1px;
}
#post > div {
  display: table-cell;
}
#post .details {
  white-space: nowrap;
}
<div id="post">
  <div class="mainInfos">really really long content that should be wrapped and should be all at the left of "small content", on the same line</div>
  <div class="details">small content</div>
</div>

答案 1 :(得分:1)

您可以使用 flexbox

  1. display: flex添加到容器post

  2. white-space: nowrap添加到details以阻止其包装

  3. flex: 1添加到mainInfos,以便占用余下的空间。

  4. 见下面的演示:

    #post {
      display: flex;
      width: 400px;
      border: solid 1px;
    }
    #post .mainInfos {
      flex: 1;
    }
    #post .details {
      white-space: nowrap;
    }
    <div id="post">
      <div class="mainInfos">really really long content that should be wrapped and should be all at the left of "small content", on the same line</div>
      <div class="details">small content</div>
    </div>

答案 2 :(得分:0)

使用Flexbox,首先删除浮动:右侧列右侧。添加到父元素(#post)this,

display: flex

Documentation here

答案 3 :(得分:0)

请检查示例。我认为这会对你有所帮助。

#post {
  -webkit-box-align: center;
  -webkit-align-items: center;
  -moz-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 400px;
  -moz-box-flex: 0;
  -ms-flex: 0 0 400px;
  flex: 0 0 400px;
  overflow: hidden;
  width: 400px;
  border: solid 1px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 50px;
  padding: 5px;
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
  -moz-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
}

#post .mainInfos,
#post .details {
  -webkit-box-align: center;
  -webkit-align-items: center;
  -moz-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  background-color: #f6f6f6;
  border: 1px solid #989898;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: start;
  -webkit-justify-content: flex-start;
  -moz-box-pack: start;
  -ms-flex-pack: start;
  justify-content: flex-start;
  padding: 10px;
}
<div id="post">
            <div class="mainInfos">
                really really long content that should be wrapped and should be all at the left of "small content", on the same line
                <br />
                really really long content that should be wrapped and should be all at the left of "small content", on the same line
            </div>
            <div class="details">
                small content<br />
                small content<br />
                small content<br />
                small content<br />
                <br />
                really really long content that should be wrapped and should be all at the left of "small content", on the same line
            </div>
        </div>