Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
7.3k views
in Technique[技术] by (71.8m points)

flexbox中align-items的异常行为解释

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<style>
#main {
    width: 200px;
    height: 200px;
    border: 1px solid #c3c3c3;
    display: -webkit-flex; /* Safari */
    display: flex;
}
#right {
    flex: 1;
    overflow: auto;
}
p {
    margin: 0
}
</style>
</head>
<body>

<div id="main">
  <div style="width: 100px; height: 100px; background-color:coral;">A</div>
  <div id="right"> 
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
      <p>asadadadasd</p>
  </div>
</div>

</body>
</html>

上面可以正常生成左侧固定,右侧滚动的局部
image.png

但当我加入

#main {
    align-items: center / flex-start / flex-end;
}

右侧滚动区域就会消失而且会溢出
image.png

想问下造成这种现象的原因是什么,谢谢!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

这种时候你要学会学习的方法。

  1. 首先到 MDN 上查找 align-items 的默认属性是什么:https://developer.mozilla.org...
  2. 得知是 normal,在你的场景下等效于 stretch
  3. 了解 stretch 的意思是按照容器的宽高缩放,所以滚动是正常
  4. 再看 center/flex-start/flex-end 的意思,只规定了起止位置,没有限制尺寸,所以撑开也是正常的
  5. 容器默认都是 overflow: visible,所以伸出去也是正常的。

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...