简体   繁体   English

超出窗口宽度的背景溢出

[英]Overflow background beyond window's width

I have to design something like below: 我必须设计如下:

  • A parent container with 'n' childs that may very well go beyond the window's width and in that case page should scroll and not wrap to next line. 一个有'n'个子级的父容器很可能超出了窗口的宽度,在这种情况下,页面应该滚动而不换行到下一行。

  • The above container will be rendered multiple times below one another. 上面的容器将在彼此之下多次渲染。

  • The page should scroll as a whole, ie scroll should be at a wrapper (div with class .overflow ) level and not at individual parent level. 页面应该整体滚动,即滚动应该在包装器(类为.overflow div)级别上,而不是在单个父级别上。

Scroll horizontally in the snippet below to see the behavior. 在下面的代码段中水平滚动以查看行为。

 .overflow { overflow-x: auto; } .parent { background: #ccc; border: 4px solid blue; display: flex; margin: 10px 0; } .child { display: inline-flex; height: 50px; background: white; margin: 10px; flex: 1 0 300px; border: 2px solid red; } 
 <div class="overflow"> <div class="parent"> <div class="child"> 1 </div> <div class="child"> 2 </div> <div class="child"> 3 </div> </div> <div class="parent"> <div class="child"> 1 </div> <div class="child"> 2 </div> <div class="child"> 3 </div> </div> </div> 

The issue now is that the grey background on parent is not overflowing behind the children beyond the window's width. 现在的问题是,父级上的灰色背景不会在子级后面超出窗口的宽度溢出。

How to achieve this (make .parent div background overflow for all its children) and possibly with retaining the flex-box ? 如何实现这一目标(使所有子项的.parent div背景溢出),并可能保留flex-box

Use inline-flex for parent and replace flex-basis by width 对父级使用inline-flex并按width替换flex-basis

 .overflow { overflow-x: auto; } .parent { background: #ccc; border: 4px solid blue; display: inline-flex; min-width:100%; /*To keep the block behavior*/ box-sizing:border-box; margin: 10px 0; } .child { display: inline-flex; height: 50px; background: white; margin: 10px; width:300px; flex-shrink:0; flex-grow:1; border: 2px solid red; } 
 <div class="overflow"> <div class="parent"> <div class="child"> 1 </div> <div class="child"> 2 </div> <div class="child"> 3 </div> </div> <div class="parent"> <div class="child"> 1 </div> <div class="child"> 2 </div> <div class="child"> 3 </div> </div> </div> 

Remove overflow div 删除溢出div

Change your parent class css to 将您的父类CSS更改为

.parent {
  overflow-x: auto;
  background: #ccc;
  border: 4px solid blue;
  display: flex;
  margin: 10px 0;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM