2016-01-09 105 views
1

我有一個寬度,高度和overflow-x auto的容器div。我想能夠水平滾動內部的元素。裏面我有一個容納所有元素的內部容器。如何使內部div調整其子元素的寬度

如果我給內部div一個固定的寬度,在這種情況下3000像素,它的工作原理,但我希望它動態調整其寬度。我怎樣才能做到這一點?

這是我的小提琴。

https://jsfiddle.net/95yb8k1f/

<div class="outer" style="width:100%; height:500px;overflow-x:auto;"> 
    <div class="inner" style="height:100%; width:3000px;"> 
     <div class="item" style="height:100%; width:300px; float:left;"> 
     <div class="item" style="height:100%; width:300px; float:left;"> 
     <div class="item" style="height:100%; width:300px; float:left;"> 
     <div class="item" style="height:100%; width:300px; float:left;"> 
    </div> 
</div> 
+0

我愛多'的高度上.innerdisplay: inline-block;使用white-space:nowrap;:100%;':d – CoderPi

回答

2

您需要在.item

div.outer { 
 
    width:100%; 
 
    height:400px; 
 
    overflow-x:scroll; 
 
    overflow-y: hidden; 
 
} 
 

 
div.inner { 
 
    position:relative; 
 
    height:100%; 
 
    white-space:nowrap; 
 
} 
 

 
div.item { 
 
    width:300px; 
 
    height:100%; 
 
    display: inline-block; 
 
} 
 

 
div.item:nth-child(2n+1){ 
 
    background:blue; 
 
} 
 

 
div.item:nth-child(2n+2){ 
 
    background:green; 
 
}
<div class="outer"> 
 
    <div class="inner"> 
 
    <div class="item"></div> 
 
    <div class="item"></div> 
 
    <div class="item"></div> 
 
    <div class="item"></div> 
 
    <div class="item"></div> 
 
    <div class="item"></div> 
 
    <div class="item"></div> 
 
    <div class="item"></div> 
 
    <div class="item"></div> 
 
    </div> 
 
</div>

+0

那訣竅!非常感謝! –

+0

很高興我能幫到你。 –

+2

要刪除'inline-bock'元素之間的空格,將'font-size:0'添加到父元素,然後再次爲子元素添加'font-szie' - [JSFiddle](https://jsfiddle.net/ 19pu2zL9 /) – Anonymous

相關問題