Flex盒最后一个项目占据容器的整个宽度。我该如何解决这个问题?

时间:2017-09-04 08:04:39

标签: html css list css3 flexbox

最后一行采用全宽,但我希望最后一行的宽度与所有其他项的宽度相同。 我附上了一个截图,说明了我的问题。

使用flexbox是否可行,或者我必须使用简单的CSS来满足我的要求?或者我应该使用显示表和表格单元属性?

提前致谢!

enter image description here

* {
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
}

.container {
  width: 1170px
}

.listingResult {
  list-style: none;
  margin: 0px;
  padding: 0px;
  display: flex;
  /* NEW, Spec - Opera 12.1, Firefox 20+ */
  display: -webkit-box;
  /* OLD - iOS 6-, Safari 3.1-6 */
  display: -moz-box;
  /* OLD - Firefox 19- (buggy but mostly works) */
  display: -ms-flexbox;
  /* TWEENER - IE 10 */
  display: -webkit-flex;
  /* NEW - Chrome */
  -webkit-flex-wrap: wrap;
  /* Safari 6.1+ */
  flex-wrap: wrap;
}

.listingResult>li {
  width: 33.33%;
  -webkit-box-flex: 1 33.33%;
  /* OLD - iOS 6-, Safari 3.1-6 */
  -moz-box-flex: 1 .33%;
  /* OLD - Firefox 19- */
  -webkit-flex: 1 33.33%;
  /* Chrome */
  -ms-flex: 1 33.33%;
  /* IE 10 */
  flex: 1 33.33%;
  /* NEW, Spec - Opera 12.1, Firefox 20+ */
  margin: 0 0 30px;
  padding: 10px;
  display: flex;
  /* NEW, Spec - Opera 12.1, Firefox 20+ */
  display: -webkit-box;
  /* OLD - iOS 6-, Safari 3.1-6 */
  display: -moz-box;
  /* OLD - Firefox 19- (buggy but mostly works) */
  display: -ms-flexbox;
  /* TWEENER - IE 10 */
  display: -webkit-flex;
  /* NEW - Chrome */
  -webkit-flex-wrap: wrap;
  /* Safari 6.1+ */
  flex-wrap: wrap;
}

.listingResult>li .listingResultPic img {
  max-width: 100%;
  height: auto;
}

.listingResult>li .listingResultInfo {
  margin-top: auto;
}
<div class="container">
  <ul class="listingResult">
    <li>
      <div class="listingResultPic"><img src="http://rets.906mls.com/mls_photos/1102784_1.JPG" alt="No Image" width=""></div>
      <div class="listingResultInfo">
        <h4 class="price">$12,000</h4>
        <p class="address info">Co Rd AFChampion, MI 49814</p>
        <p class="listingNumber info">Listing Number: <a href="http://182.73.133.220/elder/listing-details/1103145">1103145</a></p>
        <ul class="basicInfo">
          <li class="bed">3</li>
          <li class="bath">1</li>
          <li class="area">1103 sqft</li>
        </ul>
      </div>
    </li>
    <li>
      <div class="listingResultPic"><img src="http://rets.906mls.com/mls_photos/1102784_1.JPG" alt="No Image" width=""></div>
      <div class="listingResultInfo">
        <h4 class="price">$12,500</h4>
        <p class="address info">GenevaIronwood, MI 49938</p>
        <p class="listingNumber info">Listing Number: <a href="http://182.73.133.220/elder/listing-details/1100143">1100143</a></p>
        <ul class="basicInfo">
          <li class="bed">3</li>
          <li class="bath">2</li>
          <li class="area">1717 sqft</li>
        </ul>
      </div>
    </li>
    <li>
      <div class="listingResultPic"><img src="http://rets.906mls.com/mls_photos/1102784_1.JPG" alt="No Image" width=""></div>
      <div class="listingResultInfo">
        <h4 class="price">$12,500</h4>
        <p class="address info">SaginawNorway, MI 49870</p>
        <p class="listingNumber info">Listing Number: <a href="http://182.73.133.220/elder/listing-details/1104097">1104097</a></p>
        <ul class="basicInfo">
          <li class="bed">3</li>
          <li class="bath">1</li>
          <li class="area">828 sqft</li>
        </ul>
      </div>
    </li>
    <li>
      <div class="listingResultPic"><img src="http://rets.906mls.com/mls_photos/1102784_1.JPG" alt="No Image" width=""></div>
      <div class="listingResultInfo">
        <h4 class="price">$12,900</h4>
        <p class="address info">NorthWakefield, MI 49968</p>
        <p class="listingNumber info">Listing Number: <a href="http://182.73.133.220/elder/listing-details/1101426">1101426</a></p>
        <ul class="basicInfo">
          <li class="bed">3</li>
          <li class="bath">2</li>
          <li class="area">935 sqft</li>
        </ul>
      </div>
    </li>
  </ul>
</div>

2 个答案:

答案 0 :(得分:5)

将flex-grow设置为0.在列表元素上将flex: 1 33.33%更改为flex: 0 1 33.33%

以下是您的工作代码:https://jsfiddle.net/ecgx58ep/2/

有关flex-grow的更多信息,请参阅此链接:https://developer.mozilla.org/en/docs/Web/CSS/flex-grow

答案 1 :(得分:1)

您可以将flex: 1 33.33%更改为flex: 0 1 33.33%。现在flex-grow禁用,最后一行将显示。

由于您已在flex-basis中设置了{3}的33.33%,因此您可以删除flex: 0 1 33.33%

见下面的演示:

width: 33.33%
* {
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
}

.container {
  width: 1170px
}

.listingResult {
  list-style: none;
  margin: 0px;
  padding: 0px;
  display: flex;
  /* NEW, Spec - Opera 12.1, Firefox 20+ */
  display: -webkit-box;
  /* OLD - iOS 6-, Safari 3.1-6 */
  display: -moz-box;
  /* OLD - Firefox 19- (buggy but mostly works) */
  display: -ms-flexbox;
  /* TWEENER - IE 10 */
  display: -webkit-flex;
  /* NEW - Chrome */
  -webkit-flex-wrap: wrap;
  /* Safari 6.1+ */
  flex-wrap: wrap;
}

.listingResult>li {
  -webkit-box-flex: 0 1 33.33%;
  /* OLD - iOS 6-, Safari 3.1-6 */
  -moz-box-flex: 0 1 .33%;
  /* OLD - Firefox 19- */
  -webkit-flex: 0 1 33.33%;
  /* Chrome */
  -ms-flex: 0 1 33.33%;
  /* IE 10 */
  flex: 0 1 33.33%;
  /* NEW, Spec - Opera 12.1, Firefox 20+ */
  margin: 0 0 30px;
  padding: 10px;
  display: flex;
  /* NEW, Spec - Opera 12.1, Firefox 20+ */
  display: -webkit-box;
  /* OLD - iOS 6-, Safari 3.1-6 */
  display: -moz-box;
  /* OLD - Firefox 19- (buggy but mostly works) */
  display: -ms-flexbox;
  /* TWEENER - IE 10 */
  display: -webkit-flex;
  /* NEW - Chrome */
  -webkit-flex-wrap: wrap;
  /* Safari 6.1+ */
  flex-wrap: wrap;
}

.listingResult>li .listingResultPic img {
  max-width: 100%;
  height: auto;
}

.listingResult>li .listingResultInfo {
  margin-top: auto;
}