【问题标题】:Layout: one column as wide as it needs, the other taking the remaining space布局:一列根据需要宽,另一列占用剩余空间
【发布时间】:2017-06-12 04:27:52
【问题描述】:

我需要一个两列布局,就像一个表格,我不知道其中的列有多宽。

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

右栏没问题,如果内容需要,左 div 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>

【问题讨论】:

标签: html css flexbox alignment


【解决方案1】:

请检查示例。我想它会对你有所帮助。

#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>

【讨论】:

    【解决方案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>

    【讨论】:

    • 在没有父级的情况下使用 display: inline-cell 是否正确 display: table?
    • 真正长的内容很短,“小内容”没有对齐
    • @cdarwin 对您的第一个问题是肯定的,您可以将父 div 的显示更改为 table 并将详细信息 div 的文本右对齐
    【解决方案3】:

    您可以为此使用 flexbox

    1. display: flex添加到容器post

    2. white-space: nowrap 添加到details 以防止其换行

    3. flex: 1 添加到mainInfos 以使其占用剩余空间。

    请看下面的演示:

    #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>

    【讨论】:

      【解决方案4】:

      使用Flexbox,先去掉右列的float:right。添加到父元素(#post)这个,

      display: flex
      

      Documentation here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 2011-10-02
        • 2012-07-07
        • 1970-01-01
        • 2019-01-19
        • 1970-01-01
        相关资源
        最近更新 更多