2017-09-11 118 views
1

我不明白爲什麼下面的代碼片段不會讓我一直向上滾動。內部容器似乎超過外部容器,但這種溢出部分被忽略。這是什麼造成的?頂部溢出是不可滾動的

body{ 
 
    margin: 0; 
 
} 
 

 
.outer { 
 
    height: 100vh; 
 
    display: flex; 
 
    overflow-y: auto; 
 
    justify-content: center; 
 
    align-items: center; 
 
} 
 

 
.inner { 
 
    display: flex; 
 
    flex-direction: column; 
 
} 
 

 
.inner>div { 
 
    height: 50px; 
 
}
<div class="outer"> 
 
    <div class="inner"> 
 
    <div>heck 1</div> 
 
    <div>heck 2</div> 
 
    <div>heck 3</div> 
 
    <div>heck 4</div> 
 
    <div>heck 5</div> 
 
    <div>heck 6</div> 
 
    <div>heck 7</div> 
 
    <div>heck 8</div> 
 
    <div>heck 9</div> 
 
    <div>heck 10</div> 
 
    </div> 
 
</div>

回答

1

經過搜索,我發現解決方案,我一直在尋找here,原來在Flexbox的汽車利潤率也垂直工作。感謝大家的幫助!

+0

很高興聽到+1 :) – kukkuz

2

Flex是殺了你。只要刪除它並正常對齊 - 將溢出與錯誤混淆。

body{ 
 
    margin: 0; 
 
} 
 

 
.outer { 
 
    height: 100vh; 
 
    overflow-y: auto; 
 
    align-items: center; 
 
    text-align:center; 
 
} 
 

 

 
.inner>div { 
 
    height: 50px; 
 
}
<div class="outer"> 
 
    <div class="inner"> 
 
    <div>heck 1</div> 
 
    <div>heck 2</div> 
 
    <div>heck 3</div> 
 
    <div>heck 4</div> 
 
    <div>heck 5</div> 
 
    <div>heck 6</div> 
 
    <div>heck 7</div> 
 
    <div>heck 8</div> 
 
    <div>heck 9</div> 
 
    <div>heck 10</div> 
 
    </div> 
 
</div>

0

先給內部容器的高度..

body{ 
 
    margin: 0; 
 
} 
 

 
.outer { 
 
    height: 100vh; 
 
    display: flex; 
 
    overflow-y: auto; 
 
    justify-content: center; 
 
    align-items: center; 
 
} 
 

 
.inner { 
 
    display: flex; 
 
    flex-direction: column; 
 
    height: 100%; 
 
} 
 

 
.inner>div { 
 
    height: 50px; 
 
}
<div class="outer"> 
 
    <div class="inner"> 
 
    <div>heck 1</div> 
 
    <div>heck 2</div> 
 
    <div>heck 3</div> 
 
    <div>heck 4</div> 
 
    <div>heck 5</div> 
 
    <div>heck 6</div> 
 
    <div>heck 7</div> 
 
    <div>heck 8</div> 
 
    <div>heck 9</div> 
 
    <div>heck 10</div> 
 
    </div> 
 
</div>

+0

你正在避免這種溢出問題(沒有想到這一點,很好),儘管這可能是一個有效的解決方案。如果OP決定這是他喜歡的(我認爲溢出是必須的),你會得到我的投票。 – kabanus

+0

我實際上需要溢出,內容會太擠壓,所以我恐怕這種做法是不行的。 –

0

你不需要outer元素align-items: center - 這會導致問題溢出。

默認stretchalign-items會導致溢出行爲。您還必須將flex-shrink: 0設置爲.inner > div以防止默認的收縮以維持溢出。

編輯:在沒有溢出時,將auto邊距加到inner的中間內容上。同樣居中裏面的內容使他們成爲一個flexbox。

請參見下面的演示:

body { 
 
    margin: 0; 
 
} 
 

 
.outer { 
 
    height: 100vh; 
 
    display: flex; 
 
    overflow-y: auto; 
 
    justify-content: center; 
 
    /*align-items: center;*/ 
 
} 
 

 
.inner { 
 
    display: flex; 
 
    flex-direction: column; 
 
    margin: auto; /* ADDED */ 
 
} 
 

 
.inner>div { 
 
    height: 50px; 
 
    /* ADDED */ 
 
    flex-shrink: 0; 
 
    display: flex; 
 
    align-items: center; 
 
}
<div class="outer"> 
 
    <div class="inner"> 
 
    <div>heck 1</div> 
 
    <div>heck 2</div> 
 
    <div>heck 3</div> 
 
    <div>heck 4</div> 
 
    <div>heck 5</div> 
 
    <div>heck 6</div> 
 
    <div>heck 7</div> 
 
    <div>heck 8</div> 
 
    <div>heck 9</div> 
 
    <div>heck 10</div> 
 
    </div> 
 
</div>

+0

這確實修復了溢出,但是如果沒有溢出,則不會將內容居中。將'min-height:100%'和'justify-content:center'設置爲'.inner'可以讓它居中,但是會再次混淆溢出。看起來flex並不是最好的方法。 –

+0

@ K.Kowalczyk好的,你已經找到了我想要的解決方案...更新的答案使用自動邊距中心的內容,當沒有溢出......謝謝! – kukkuz