简体   繁体   English

我如何阻止 Parent Div 因为它的孩子而扩大?

[英]How Do I Stop Parent Div From Expanding Because of Its Children?

在此处输入图片说明

So child div is has a list of items next to each other, each with a fix width.所以子 div 有一个彼此相邻的项目列表,每个项目都有固定的宽度。

In this image child div extends past MainContent but in the actual app, MainContent extends to fit the width of child div.在此图像中,子 div 扩展到 MainContent 之后,但在实际应用中,MainContent 扩展以适应子 div 的宽度。

I would like to make it so that when child overflows, it just has a scroll wheel instead of becoming so large.我想让它当孩子溢出时,它只有一个滚轮而不是变得那么大。

I have tried setting Child Div to 'overflow-x : auto'我试过将 Child Div 设置为 'overflow-x : auto'

Child div width is at 100%, as is MainContent.子 div 宽度为 100%,MainContent 也是如此。

JSX/HTML JSX/HTML

<div className = 'app'>
  <div className = 'navbar'></div>
    <div className = 'maincontent'>
      <div className = 'child'>
        <div className = 'childlist'>
          <button className = 'btn'></button>
          <button className = 'btn'></button>
          <button className = 'btn'></button>
        </div>
      </div>
    </div>
  </div>

CSS CSS

.app : {
    display : flex,
    width: 100vw,
    height :100vh
  }
  .navbar : {
    display : flex,
    flex-direction :column,
    width : 112
  }
  .maincontent:{
    display : flex,
    width : 100%
  }
  .child: {
    width :100%
  }
  .childlist:{
    display : flex,
    width :90%
  }
  .btn:{
    width : 200,
    height :200
  }

All your containers widths are relative, so it will adjust, not overflow.你所有的容器宽度都是相对的,所以它会调整,而不是溢出。 If your child's width is % of the parent's, then it'll never overflow.如果您孩子的宽度是父母的 %,那么它永远不会溢出。

You need to have fixed width to make it overflow.您需要具有固定宽度才能使其溢出。

Plus your code looks really messy, you may want to try Prettier加上你的代码看起来很乱,你可能想试试 Prettier

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当孩子们这样做时,我如何让父 div 调整大小? - How do I get a parent div to resize when the children do? 如何在 JavaScript 中将父级的值放入其子级函数中? - How do I get the value of the parent into its children function in JavaScript? 如何获得父div中子代的总和? - How do I get the sum of children within a parent div? 将iFrame放入后,如何阻止父DIV拉伸? - How do I stop a parent DIV from stretching when I put an iFrame into it? 如何在父组件重新加载时阻止子组件重新渲染 - How do I stop a child component from re-rendering when its parent reloads 如何阻止我的网格超出其容器 div? - how do I stop my grid from going outside its container div? 无论多深,我如何将 Parent 样式组件中的 prop 传递给其子样式组件 - How do I pass a prop from a Parent styled component to its children's styled components no matter how deep 如何将父Div的背景图像覆盖其子 - How to Overlap a Parent Div's Background Image Over its Children 当子div变为可见时,如何停止父div垂直扩展 - How to stop a parent div expanding vertically when child div becomes visible 在动画元素以使其从屏幕上滑入时,如何停止页面展开? - How do I stop the page from expanding when animating an element so it slides in from off screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM