2015-09-07 60 views
0

我试图复制eBay的“今日”特色卖方布局与4方形图像组成一个盒子(见图片),但使用Bootstrap。我真的很难理解我如何才能做到这一点。我可以或多或少地获得正方形,并且在lg,md和sm规则上看起来不错,因为当然事情不需要按比例放置,我可以每个方格都固定宽度和高度。但是,当涉及到移动设备时,由于需要根据窗口大小调整窗口大小,因此会熄灭该窗口。4个图像的响应式网格

enter image description here

,我已经想出了目前的HTML基本上是一个网格分成9和3,(COL-XS-9和Col-XS-3),用一套高度给出在每个桌面/平板电脑宽度。

图像有时会是平方图像,其他时候它们将是横向或纵向格式的图像,在这种情况下,它们将保持纵横比并扩展到其最大尺寸,但在包含div的范围内。

有没有一种方法,我可以实现这个使用Bootstrap或我需要看看替代品使用JavaScript例如?下面

代码,以防万一,你需要看一看:

HTML

<div id="featured-merchant-container"> 
    <div class="featured-merchant-listings"> 
    <div class="row"> 
     <div class="primary-img-container col-xs-9 col-sm-9 col-md-9 col-lg-9 no-padding-right"> 
     <div class="big-hero-image"> 
      <a ng-if="merchant.userListings[0].primaryImage" ng-href="/#!/users/{{merchant._id}}"> 
      <span class="thumb"> 
       <img ng-src="{{merchant.userListings[0].primaryImage}}" /> 
      </span> 
      </a> 
      <a ng-if="!merchant.userListings[0].primaryImage" ng-href="/#!/users/{{merchant._id}}"> 
      <span class="thumb"> 
       <img ng-src="img/placeholder.png" /> 
      </span> 
      </a> 
     </div> 
     </div> 
     <div class="secondary-img-container col-xs-3 col-sm-3 col-md-3 col-lg-3 no-padding-left"> 
     <div class="big-hero-images"> 
      <div class="first"> 
      <a ng-if="merchant.userListings[1].primaryImage" ng-href="/#!/users/{{merchant._id}}"> 
       <span class="thumb"> 
       <img ng-src="{{merchant.userListings[1].primaryImage}}" /> 
       </span> 
      </a> 
      <a ng-if="!merchant.userListings[1].primaryImage" ng-href="/#!/users/{{merchant._id}}"> 
       <span class="thumb"> 
       <img ng-src="img/placeholder.png" /> 
       </span> 
      </a> 
      </div> 
      <div class="second"> 
      <a ng-if="merchant.userListings[2].primaryImage" ng-href="/#!/users/{{merchant._id}}"> 
       <span class="thumb"> 
       <img ng-src="{{merchant.userListings[2].primaryImage}}" /> 
       </span> 
      </a> 
      <a ng-if="!merchant.userListings[2].primaryImage" ng-href="/#!/users/{{merchant._id}}"> 
       <span class="thumb"> 
       <img ng-src="img/placeholder.png" /> 
       </span> 
      </a> 
      </div> 
      <div class="last"> 
      <a ng-if="merchant.userListings[3].primaryImage" ng-href="/#!/users/{{merchant._id}}"> 
       <span class="thumb"> 
       <img ng-src="{{merchant.userListings[3].primaryImage}}" /> 
       </span> 
      </a> 
      <a ng-if="!merchant.userListings[3].primaryImage" ng-href="/#!/users/{{merchant._id}}"> 
       <span class="thumb"> 
       <img ng-src="img/placeholder.png" /> 
       </span> 
      </a> 
      </div> 
     </div> 
     </div> 
     </div> 
    </div> 
</div> 

CSS:

#featured-merchant-container { 
    border: 1px solid green-grey; 


    .big-hero-image { 
      text-align:center; 
      .thumb { 
       display:block; 
       border-right:1px solid green-grey; 
       height:413px; 
       display:table-cell; 
       vertical-align:middle; 
       text-align:center; 
       img { 
        width:100%; 
        height:413px; 
       } 
      } 
    } 

    .big-hero-images { 
      text-align:center; 
      // border-left:1px solid green-grey; 
      .thumb { 
       display:block; 
       height:137px; 
       display:table-cell; 
       vertical-align:middle; 
       text-align:center; 
       img { 
        width:100%; 
        height:137px; 
       } 
      } 
      .first, .second { 
        border-bottom:1px solid green-grey; 
       } 
    } 
} 

请帮助我。这是推动我疯了:(。谢谢!

+0

嵌套的CSS是不是有效的CSS是这个SASS(http://sass-lang.com)或LESS(http://lesscss.org)? –

+0

道歉,这是手写笔https://learnboost.github.io/stylus/,但这与LESS和SASS非常相似。 –

回答

0

感谢您的回答所有人。

我设法做到这一点的方法是完全取消对网格的Bootstrap的使用。我反而围绕百分比构建网格。我在网上阅读了一些关于如何提供百分比作为填充顶部的值可以基本上为您提供具有某种纵横比的容器的内容。例如,如果您想要一个保持宽高比为4:3的div,您要做的是在该div的样式表中添加

padding-top:75%; 

只要div具有给定的宽度,就可以使用padding-top百分比技巧提供它的高度。由于我希望网格中的所有div均为正方形,因此我向其中的每一个添加了

`padding-top:100%;` 

。因此,在电网大图像时,CSS(笔)如下:

.big-image { 
     width: 75%; 
     /* whatever width you want */ 
     display: inline-block; 
     position: relative; 
     float:left; 
     -moz-box-sizing: border-box; 
     -webkit-box-sizing: border-box; 
     -ms-box-sizing: border-box; 
     box-sizing: border-box; 
     font-size:0px; 

     &:after { 
      padding-top: 100%; 
      /* 16:9 ratio */ 
      display: block; 
      content: ''; 
     } 
} 

当然,你可以在下面看到,在div需要被相对定位,并填充有待内添加:在CSS选择器之后。

我希望这有助于某人。谢谢

0

你可以做一些与flex-box布局?

.cols {overflow: hidden;} 
 
.cols .col {float: left; width: 25%;} 
 
.cols .col:first-child {width: 75%;} 
 
.cols .col img {width: 100%;} 
 

 
.flex-container { 
 
    display: -ms-flexbox; 
 
    display: -webkit-flex; 
 
    display: flex; 
 
    -webkit-flex-direction: column; 
 
    -ms-flex-direction: column; 
 
    flex-direction: column; 
 
    -webkit-flex-wrap: nowrap; 
 
    -ms-flex-wrap: nowrap; 
 
    flex-wrap: nowrap; 
 
    -webkit-justify-content: flex-start; 
 
    -ms-flex-pack: start; 
 
    justify-content: flex-start; 
 
    -webkit-align-content: stretch; 
 
    -ms-flex-line-pack: stretch; 
 
    align-content: stretch; 
 
    -webkit-align-items: flex-start; 
 
    -ms-flex-align: start; 
 
    align-items: flex-start; 
 
} 
 

 
.flex-item { 
 
    -webkit-order: 0; 
 
    -ms-flex-order: 0; 
 
    order: 0; 
 
    -webkit-flex: 1 0 auto; 
 
    -ms-flex: 1 0 auto; 
 
    flex: 1 0 auto; 
 
    -webkit-align-self: auto; 
 
    -ms-flex-item-align: auto; 
 
    align-self: auto; 
 
}
<div class="cols"> 
 
    <div class="col"> 
 
    <img src="http://placehold.it/350x350" alt="" /> 
 
    </div> 
 
    <div class="col"> 
 
    <div class="flex-container"> 
 
     <div class="flex-item"><img src="http://placehold.it/350x350" alt="" /></div> 
 
     <div class="flex-item"><img src="http://placehold.it/350x350" alt="" /></div> 
 
     <div class="flex-item"><img src="http://placehold.it/350x350" alt="" /></div> 
 
    </div> 
 
    </div> 
 
</div>

+0

感谢您的回答。 这里的问题是,图像并不总是正方形。希望他们是正方形的,但如果他们不是,我需要居中并水平对齐它们,所以我可能需要使用display:table和display:table-cell css规则。我基本上需要大平方和3个小方格,不管大小和图像本身的纵横比如何,它们都保持为正方形,并且尺寸相互成正比。 –

+0

@JamieMclaughlan欢迎。它是否适合你? –

+0

对不起,我刚刚编辑了我上面的答案。 –

0

如果您的图片将是各种尺寸的,那么你需要要么使用背景图像的背景大小设置为覆盖。

.flex-item 
    background-size cover 
    background-image url(http://placehold.it/350x350) 

或者您需要使用object-fit,这是真棒,但在IE中有可怕的支持。

.flex-item img 
    object-fit cover