简体   繁体   English

允许绝对容器宽度溢出屏幕宽度

[英]Allow absolute container width to overflow screen width

I have a container that is absolutely positioned at the bottom right side of my screen. 我有一个绝对位于屏幕右下方的容器。

The width and height are set to auto. widthheight设置为自动。

Inside the container are elements, which are set to inline-block . 容器内部是元素,它们被设置为inline-block Imagine facebook's chat windows that popup, that is exactly what I have. 想象一下facebook弹出的聊天窗口,这正是我所拥有的。

When I add elements, that work as expected, displaying next to each other on the right side of the screen. 当我添加元素时,它按预期工作,在屏幕右侧显示彼此相邻。 The issue is that when I add enough, and the container reaches the width of the screen, they wrap. 问题是,当我添加足够的,并且容器达到屏幕的宽度时,它们会换行。 Instead of continuing left outside of the screen. 而不是继续留在屏幕外面。

How can I achieve this? 我怎样才能做到这一点?

<div class='container'>
    <div class='element'></div> 
    <!-- Adding too many of these will cause them to wrap
     above each other instead of flowing off the left side of the screen --> 
</div>

.container{
    position: absolute;
    right: 0:
    bottom: 0;
    width: auto;
    height: auto;
}

.element{
    width: 300px;
    height: 500px;
}

The idea is to make your element s inline-block and then force them into a single line using the white-space: nowrap property - see demo below: 我们的想法是让你的element inline-block ,然后使用white-space: nowrap属性将它们强制成一行 - 请参阅下面的演示:

 .container{ position: absolute; right: 0; bottom: 0; width: auto; height: auto; white-space: nowrap; } .element{ width: 300px; height: 500px; display: inline-block; border: 1px solid red; } 
 <div class='container'> <div class='element'>element 1</div> <div class='element'>element 2</div> <div class='element'>element 3</div> <div class='element'>element 4</div> </div> 

If you want to set the direction of the element s from right to left, you can use a nice trick using scale transform - see demo below: 如果你想从右到左设置element的方向,你可以使用scale transform一个很好的技巧 - 见下面的演示:

 .container{ position: absolute; right: 0; bottom: 0; width: auto; height: auto; white-space: nowrap; transform: scaleX(-1); } .element{ width: 300px; height: 500px; display: inline-block; border: 1px solid red; transform: scaleX(-1); } 
 <div class='container'> <div class='element'>element 1</div> <div class='element'>element 2</div> <div class='element'>element 3</div> <div class='element'>element 4</div> </div> 

A nice property of Flexbox : with flex-wrap: nowrap (which is the default value): a flex container won't allow its flex items to occupy 2+ lines and will force them to overflow a container even if the sum of their widths is way larger than their parent. Flexbox的一个不错的属性:使用flex-wrap: nowrap (默认值):flex容器不允许其flex项占用2+行,并且即使它们的宽度总和也会强制它们溢出容器比他们的父母大。
And to display them from right to left, out of the viewport on the left, it's as easy as flex-direction: row-reverse . 要从右到左显示它们,从左侧的视口中显示它,就像flex-direction: row-reverse一样简单flex-direction: row-reverse
Advantage over inline-block : no whitespace/gap between items (generally 4px) inline-block优势:项目之间没有空格/间隙(通常为4px)

 .container{ position: absolute; right: 0; bottom: 0; display: flex; flex-direction: row-reverse; width: auto; height: auto; } .element{ width: 300px; height: 200px; /* flex: 0 0 300px; not needed */ border: 1px solid red; } 
 <div class='container'> <div class='element'>element 1</div> <div class='element'>element 2</div> <div class='element'>element 3</div> <div class='element'>element 4</div> </div> 

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

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