html - Flexbox 大小问题

标签 html css flexbox

我有一个 flex box,里面有 2 个并排的元素 通常 flexbox 中的元素会自动占用所有空间...

但不是这里...我尝试手动将高度设置为 height:100%...什么都不行

我哪里错了? (菜单元素应该采用所有可用的高度,例如我人为地为蓝色元素设置了固定高度......但在我的代码中它应该根据内容而变化所以设置菜单像素不是一个选项)

html:

<html>
<head>
    <link rel="stylesheet" type="text/css" href="./test2.css">
</head>
<body>

        <div class="framehome">
            <div class="myaccountwrapper">
                <div class="myaccountleft">
                    Menu
                </div>
                <div class="myaccountright">
                    <div id="windowmyaccount" class="submenu" id="boxaccount">
                      Change my Handle
                    </div>
                    <div id="windowhandle" class="submenu" id="boxaccount">
                       Change my Handle
                    </div>
                </div>
            </div>
        </div>
</body>

CSS:

    .myaccountwrapper{
    display:flex;
    flex-direction:row;
    align-items:center;
    color:lightgrey;
    background-color:green;
}

.myaccountleft{
    display: flex;
    flex-direction: column;
    width:250px;
    height:100%;
    background-color:red;
}

.myaccountright{
    flex-grow: 1;
    background-color:blue;
    height:300px;
}

最佳答案

您将父 .myaccountwrapperalign-items 属性设置为 center 的值,这会阻止 flex- 的默认行为项,即填充父级的高度

默认情况下,align-items 属性的值设置为 stretch,这会导致 flex-items 沿垂直/交叉轴拉伸(stretch)。

因此,要解决您的问题,只需删除或注释掉父级的 align-items 属性即可。

我假设您想填充整个视口(viewport)高度,所以我将父级的 height 设置为 100vh

* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100%}

.myaccountwrapper {
  display: flex;
  /*align-items: center; the culprit */
  color: lightgray;
  background-color: green;
  height: 100vh; /* needs to be set if you want that flex-items fill the viewport height */
}

.myaccountleft {
  display: flex;
  flex-direction: column;
  width: 250px;
  background-color: red;
}

.myaccountright {
  flex: 1;
  background-color: blue;
}
<div class="framehome">
  <div class="myaccountwrapper">
    <div class="myaccountleft">
      Menu
    </div>
    <div class="myaccountright">
      <div id="windowmyaccount" class="submenu" id="boxaccount">
        Change my Handle
      </div>
      <div id="windowhandle" class="submenu" id="boxaccount">
        Change my Handle
      </div>
    </div>
  </div>
</div>

align-items: center 的比较:

* {margin:0;padding:0;box-sizing:border-box}
html, body {width:100%}

.myaccountwrapper {
  display: flex;
  align-items: center;
  color: lightgray;
  background-color: green;
  height: 100vh;
}

.myaccountleft {
  display: flex;
  flex-direction: column;
  width: 250px;
  background-color: red;
}

.myaccountright {
  flex: 1;
  background-color: blue;
}
<div class="framehome">
  <div class="myaccountwrapper">
    <div class="myaccountleft">
      Menu
    </div>
    <div class="myaccountright">
      <div id="windowmyaccount" class="submenu" id="boxaccount">
        Change my Handle
      </div>
      <div id="windowhandle" class="submenu" id="boxaccount">
        Change my Handle
      </div>
    </div>
  </div>
</div>

关于html - Flexbox 大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47517924/

相关文章:

html - 如何动态更改 Jekyll _config.yml 中的变量?

html - CSS 渐变质量差

html - Bootstrap : Get Row-span like behavior?

html - 如何在悬停时更改多个元素?

html - 我怎样才能一起翻转和动画图标?

javascript - 使用 Express 将 javascript 代码注入(inject) html

html - 为什么第 nth-of-type/nth-child 不适用于嵌套元素?

css - IE7 中工具提示的 Z-index 问题

html - 如何在不使用 float 的情况下向右浮动 inline-flex 的 div ?

html - css Flex div 随着子文本区域的增长而增长(无jquery)