简体   繁体   English

调整窗口大小时,Div 不显示为 4 列并缩小为 2

[英]Div not getting displayed into 4 column and shrinking into 2 when adjusting window size

I have got a div which has 4 items and want to display them in a single row for large devices.我有一个 div 有 4 个项目,并希望在大型设备的单行中显示它们。 It does display as I want, but there is a scroll bar on the page which makes this annoying.它确实按我的意愿显示,但是页面上有一个滚动条,这很烦人。 I need to scroll from left to right to see all the items if that makes sense.如果有意义的话,我需要从左到右滚动以查看所有项目。

Here's the code:这是代码:

 .container { display: flex; width: 100%; height: 534px; } @media only screen and (max-device-width: 1080px) { .container { flex-direction: row; flex-wrap: wrap; width: 100%; } } .item{ width: 250px; position: relative; margin: 50px auto; }
 <div class="container"> <div class="item">1</div> <div class="item">1</div> <div class="item">1</div> <div class="item">1</div> </div>

You have several options depending on exactly what outcome you want.您有多种选择,具体取决于您想要的结果。

The simplest is to just allow the items (which have a fixed width) to wrap to the next line when the window is too small to accommodate them all.最简单的是当窗口太小而无法容纳所有项目时,只允许项目(具有固定宽度)换行到下一行。 This means you may sometimes get 3 on the first line and 1 on the second.这意味着您有时可能会在第一行得到 3,在第二行得到 1。

With more control you can switch to making sure there are either 4 or 2 (or, on really narrow windows, 1) item in a row.通过更多控制,您可以切换到确保连续有 4 个或 2 个(或者,在非常窄的窗口上,1 个)项目。

This snippet uses a grid to do this with breakpoints set using max-width (see note below).此代码段使用网格来执行此操作,并使用 max-width 设置断点(请参阅下面的注释)。

 .container { display: grid; grid-template-columns: repeat(4, 1fr); width: 100%; height: 534px; } @media only screen and (max-width: 1080px) { .container { grid-template-columns: repeat(2, 1fr); } } @media only screen and (max-width: 270px) { .container { grid-template-columns: repeat(1, 1fr); } } .item { width: 250px; position: relative; margin: 50px auto; }
 <div class="container"> <div class="item">1</div> <div class="item">1</div> <div class="item">1</div> <div class="item">1</div> </div>

Note: device-width is deprecated (see for example [https://developer.mozilla.org/en-US/docs/Web/CSS/@media/device-width][1]注意: device-width已弃用(参见例如 [https://developer.mozilla.org/en-US/docs/Web/CSS/@media/device-width][1]

And the width of a device is not really relevant - what we need to adjust for is the width of the window.并且设备的宽度并不真正相关——我们需要调整的是窗口的宽度。 This is done in a media query with max-width.这是在具有最大宽度的媒体查询中完成的。

Note also that both your original code and this snippet lessen the height of each item for narrower viewports as you have set a fixed height for the container.另请注意,由于您为容器设置了固定高度,因此您的原始代码和此代码段都会减少每个项目的高度以用于较窄的视口。 If you want the items to maintain full height then set height on the item (or adjust the height of container accordingly).如果您希望项目保持全高,请在项目上设置高度(或相应地调整容器的高度)。 [1]: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/device-width [1]: https ://developer.mozilla.org/en-US/docs/Web/CSS/@media/device-width

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

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