【问题标题】:How do I ensure the contents of my flex div stay within it's container?如何确保我的 flex div 的内容留在它的容器中?
【发布时间】:2017-09-01 13:46:33
【问题描述】:

我有一个 flexbox div,里面有 3 个项目。它的方向是列。

flexbox div 没有高度,但它的容器有一个最大高度。

maxheight 适用于容器,但 flexbox div 中的中间项很大并且超出了容器的边界。

这是一个说明我的问题的代码笔: http://codepen.io/jaman4dbz/pen/BWedNN

您会看到 body 的计算高度为 600 像素或类似的高度(取决于您的窗口高度),而应用 div 更大。

最终目标是让大 div 拉伸 flex div 的内容,直到达到最大高度,然后停止。本质上,我希望中间的大 div 能够响应。

* { box-sizing: border-box; }

body { 
    height: 100vh;
    margin: 0; 
    font-family: helvetica, arial, sans-serif;
}

.twocol {
    display: flex;
}

.app {
    height: auto;
    width: 100%;
    /* height: 100vh; */
    background: cadetblue;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    margin: auto;
}

/**
 * Note that this demo will actually work fine in IE10. This is because in IE10, flex-shrink is 0 instead of 1.  See: https://github.com/philipwalton/flexbugs#6-the-default-flex-value-has-changed
 **/
.header {
    width: 100%;
    background: orchid;
}

/**
 * 1. IE needs display: block on the main element.
 * 2. IE10 sets flex-shrink to 0. This was originally according to spec, but has since changed. See: https://github.com/philipwalton/flexbugs#6-the-default-flex-value-has-changed
 **/
.main {
    display: block; /* 1 */
    flex-grow: 1;
    flex-shrink: 1; /* 2 */
    background: blanchedalmond;
    overflow: auto;
    padding: 0 20px;
}

.footer {
    width: 100%;
    background: coral;
}

h1 { font-size: 36px; }
p { font-size: 16px; }
<body class="twocol">
    <div class="app">
        <header class="header">
            <p>Hello</p>
        </header>
        <main class="main">
            <h1>Cupcake ipsum dolor sit amet</h1>
            <p>Fruitcake macaroon candy canes marshmallow gummi bears biscuit I love dessert. Oat cake sugar plum marzipan gummi bears marshmallow I love brownie jelly bonbon. Sweet roll apple pie wafer fruitcake. Gummi bears lemon drops sesame snaps. Biscuit tiramisu sugar plum. Carrot cake pastry cupcake tootsie roll chocolate jujubes. Pudding muffin sweet. Cheesecake candy canes wafer gingerbread sweet.</p>        <p>Fruitcake macaroon candy canes marshmallow gummi bears biscuit I love dessert. Oat cake sugar plum marzipan gummi bears marshmallow I love brownie jelly bonbon. Sweet roll apple pie wafer fruitcake. Gummi bears lemon drops sesame snaps. Biscuit tiramisu sugar plum. Carrot cake pastry cupcake tootsie roll chocolate jujubes. Pudding muffin sweet. Cheesecake candy canes wafer gingerbread sweet.</p>        <p>Fruitcake macaroon candy canes marshmallow gummi bears biscuit I love dessert. Oat cake sugar plum marzipan gummi bears marshmallow I love brownie jelly bonbon. Sweet roll apple pie wafer fruitcake. Gummi bears lemon drops sesame snaps. Biscuit tiramisu sugar plum. Carrot cake pastry cupcake tootsie roll chocolate jujubes. Pudding muffin sweet. Cheesecake candy canes wafer gingerbread sweet.</p>        <p>Fruitcake macaroon candy canes marshmallow gummi bears biscuit I love dessert. Oat cake sugar plum marzipan gummi bears marshmallow I love brownie jelly bonbon. Sweet roll apple pie wafer fruitcake. Gummi bears lemon drops sesame snaps. Biscuit tiramisu sugar plum. Carrot cake pastry cupcake tootsie roll chocolate jujubes. Pudding muffin sweet. Cheesecake candy canes wafer gingerbread sweet.</p>        <p>Fruitcake macaroon candy canes marshmallow gummi bears biscuit I love dessert. Oat cake sugar plum marzipan gummi bears marshmallow I love brownie jelly bonbon. Sweet roll apple pie wafer fruitcake. Gummi bears lemon drops sesame snaps. Biscuit tiramisu sugar plum. Carrot cake pastry cupcake tootsie roll chocolate jujubes. Pudding muffin sweet. Cheesecake candy canes wafer gingerbread sweet.</p>
            <p>Liquorice candy canes cake. Gummies wafer tart topping pastry soufflé I love pie. Cake cookie croissant cupcake chocolate cake. Liquorice jelly beans pudding sugar plum biscuit fruitcake. Soufflé tootsie roll macaroon pudding. Chocolate cake cake sesame snaps sugar plum biscuit I love croissant icing. Caramels jelly dessert candy bonbon bear claw chocolate bar cheesecake icing. Toffee I love cake gingerbread jelly-o tootsie roll. Sesame snaps pie cupcake sweet. Croissant danish brownie gummies lollipop chupa chups gummies danish.</p>
            <p>Chupa chups pudding topping caramels pie. Tart jujubes ice cream muffin sugar plum. Soufflé candy canes I love lollipop pastry brownie danish apple pie. Apple pie chupa chups biscuit chocolate bar I love muffin I love. Caramels cupcake I love sugar plum icing candy canes I love macaroon. Marzipan danish cheesecake marshmallow jelly. Jelly-o sugar plum liquorice donut I love pastry candy donut. I love gummi bears chocolate.</p>
            <p>Gingerbread toffee pastry gingerbread I love toffee. Biscuit jelly wafer I love cheesecake candy canes chocolate cake. I love cake I love cotton candy muffin oat cake I love I love. I love bonbon macaroon toffee powder chupa chups cotton candy tootsie roll chocolate. Croissant dessert chocolate bar. Apple pie I love croissant sugar plum cotton candy I love. Candy canes jelly I love. Jelly beans cupcake brownie pudding lemon drops</p>
            <p>Cheesecake macaroon toffee sesame snaps cotton candy cotton candy I love. Cake sugar plum sweet marshmallow I love. Soufflé liquorice ice cream candy canes I love tootsie roll marzipan biscuit jelly. I love I love tiramisu donut. I love bear claw pastry toffee. Cheesecake apple pie bonbon bear claw jelly-o topping tootsie roll chocolate bar. Cake bonbon toffee donut macaroon chocolate lemon drops gummies gummies. Jelly beans chocolate bar cotton candy halvah fruitcake.</p>
        </main>
        <footer class="footer">
            Goodbye
        </footer>
    </div>
    <div>Col2</div>
</body>

【问题讨论】:

  • 如果某些东西太大而无法容纳,它当然会溢出。问题是你想让它做什么?
  • 为什么不在.app 上使用height: 100vh?您已将其注释掉,但这解决了问题。 (顺便说一句,你不需要height: 100vh 上的body。)
  • 这个例子令人困惑。我做了一个更好的。我会用更好的例子发布一个新问题。
  • @Paulie_D .main { overflow: auto } 应该创建一个滚动条。

标签: html css flexbox


【解决方案1】:

设置html, body, .app { height: 100%; }(并删除这些元素的所有其他高度设置)。需要设置html以及其他两个。

【讨论】:

  • 几乎成功了!这是我正在使用的更好的例子。 codepen.io/jaman4dbz/pen/GWaMZW虽然我想我会重写我的问题,因为我更具体地知道问题是什么。
  • 我不清楚你要做什么了——你是display:flexing 容器,里面只有一个元素。但是,如果您添加.twocol { overflow: hidden; max-height: 100%; },新的“笔”看起来会更好
  • 是的,我认为我的主要项目的 css 根本就坏了。我最新的代码笔按我的意图工作:codepen.io/jaman4dbz/pen/LWozeZ 抱歉,我的问题没有更有用,但是,奇怪的是,你确实引导我通过高度找到解决方案:100%。我将尝试在我们成千上万行 sass 的丛林中寻找,看看是什么打破了这些风格。我会回来提供一个解决方案,它可能非常具体,以至于对 SO =P 毫无用处
  • 诀窍是继承所有的东西。与身高非常相似:理论上是 100%。再次感谢 Blaze 的帮助!
【解决方案2】:

我的问题是缺乏继承所有形式的高度。 我需要为我的模态和我的大 div 之间的每个 div 设置高度:继承和最大高度:继承。 React 有时会用额外的 div 烧毁你。

这是最终的代码笔: http://codepen.io/jaman4dbz/pen/LWozeZ

.unnecessary {
  height: inherit;
  max-height: inherit;
}

.allstuff {
  display: flex;
  height: inherit;
  max-height: inherit;
}

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

编辑:此解决方案附带一个警告。您不能在具有高度的 div 上进行任何填充,也不能在其直接子级上设置边距。如果你这样做了,你的子元素就会超出它的父元素的范围,因为它具有相同的高度,尽管它应该被压缩到一个更小的区域中。

【讨论】:

  • 这不是真正的 React 问题。默认情况下,CSS height 属性不继承值。查看规范中的属性定义:w3.org/TR/CSS22/visudet.html#the-height-property
  • 这是组件设计的反应问题。 React 不会插入 div,开发人员会这样做,因为每个组件都需要一个包含元素。这意味着我们所有精心划分的组件都会导致大量 div,并且需要大量“继承”值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 1970-01-01
  • 2021-10-25
  • 2017-02-17
相关资源
最近更新 更多