亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

flex容器內居中的元素正在增長并且溢出到頂部之外

flex容器內居中的元素正在增長并且溢出到頂部之外

慕運維8079593 2019-11-20 14:16:35
我必須忘記垂直和水平居中的flexbox的一些基本知識。容器位于具有垂直滾動的父對象內,并且當容器太高時,它會超出父對象頂部,剪切內容。底部保持不變。嘗試調整視圖的高度或添加更多行以查看實際效果。body,html {  height: 100%;  width: 100%;  margin: 0;}* {  box-sizing: border-box;}#wrapper {  background: grey;  height: 100%;  width: 100%;  max-height: 100%;  display: flex;  overflow-y: auto;  align-items: center;  justify-content: center;}#box {  margin: 30px 0;  background: white;  border: 1px solid #dfdfdf;}<div id="wrapper">  <div id="box">    First line    <br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br> Last linje  </div></div>如何防止其被修剪?另外,我嘗試在容器上方和下方設置30px的邊距。
查看完整描述

3 回答

?
慕村225694

TA貢獻1880條經驗 獲得超4個贊

我認為您想要的是使flex項目(#box)具有高度并設置其溢出范圍,而不是flex容器。另外,要在上方和下方添加30px,我會從框中刪除邊距,而是向容器中添加填充。


因此,更新后的樣式將如下所示:


#wrapper {

  background: grey;

  height: 100%;

  width: 100%;

  max-height: 100%;

  display: flex;      

  align-items: center;

  justify-content: center;

  padding: 30px 0; /*added*/

}


#box {

  background: white;

  border: 1px solid #dfdfdf;

  overflow-y: auto; /*added*/

  height: 100%; /*added*/

}

body,

html {

  height: 100%;

  width: 100%;

  margin: 0;

}


* {

  box-sizing: border-box;

}


#wrapper {

  background: grey;

  height: 100%;

  width: 100%;

  max-height: 100%;

  display: flex;      

  align-items: center;

  justify-content: center;

  padding: 30px 0;

}


#box {    

  background: white;

  border: 1px solid #dfdfdf;

  overflow-y: auto;

  height: 100%;

}

<div id="wrapper">

  <div id="box">

    First line

    <br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br>line<br> Last linje

  </div>

</div>


查看完整回答
反對 回復 2019-11-20
?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

我認為您在box類中設置了頂部邊距,從而擴展了容器的高度。您可以將其設置為padding而不是margin。希望這可以幫助。謝謝。


查看完整回答
反對 回復 2019-11-20
  • 3 回答
  • 0 關注
  • 853 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號