简体   繁体   English

CSS仅动态DIV宽度,即。 如果浏览器宽度允许,则每行更多DIV

[英]CSS only dynamic DIV widths ie. more DIVs per row if browser width allows

I'm making a layout to display blocks of content side-by-side in a way where the browser window is filled 100% with DIV blocks 我正在制作一种布局,以并排显示内容块,其中浏览器窗口100%填充了DIV块

I remember seeing a CSS only method that adjusts the width of DIVs depending on the browser window. 我记得曾经看到过一种仅CSS的方法,该方法可以根据浏览器窗口调整DIV的宽度。 If space allows, 3 DIVs are displayed, if you decrease the width of the browser, it will change to 2 DIVs per row, pushing the 3rd down, while keeping the DIVs at an ideal width always filling the window. 如果空间允许,将显示3个DIV,如果减小浏览器的宽度,它将更改为每行2个DIV,将第3个向下推,同时将DIV保持在理想宽度,始终填充窗口。

I've been trying to recreate this 我一直在尝试重新创建

Say I need a min-width of 500px. 假设我需要最小宽度为500px。 If there is 1000px available, it would show 2 blocks per row. 如果有1000像素可用,则每行将显示2个块。 If 1500px, 3 blocks per row, and so on. 如果为1500像素,则每行3块,依此类推。 But based on how many blocks can fit, I need the width of each block was dynamic so as to keep the browser window filled. 但是基于可容纳的块数,我需要每个块的宽度是动态的,以保持浏览器窗口充满。

Ive seen this done before exactly. 我已经完全看到了这一点。 I remember even with javascript disabled the effect could be seen, so I figure the answer must be CSS 我记得即使禁用了javascript也可以看到效果,所以我认为答案必须是CSS

You can use media queries to achieve what you want. 您可以使用媒体查询来实现所需的功能。

Basically set the jumps where you want to skip from 1 to 2, from 2 to 3 and from 3 to 4, etc. based on window width. 基本上根据窗口宽度设置要从1跳到2,从2跳到3以及从3跳到4等的跳转。

And put the width in %. 并以%表示宽度。 This way the existing items will stretch to the available width. 这样,现有项目将延伸到可用宽度。

If you want to fit 3 in a row, use width 33%, to fit 2 use width 50% and so on. 如果要连续容纳3个,请使用宽度33%,适合2个使用50%宽度,依此类推。

If you can't make them to break the line, you can use nth-child to force them. 如果您不能使它们中断,则可以使用nth-child强制它们。

Use media queries. 使用媒体查询。

 /* Display 3 items per row for browsers with width >= 1024px */ @media (min-width: 1024px) { .items { width: 33.33%; } } /* Display 2 items per row for browsers with width >= 768px and width <= 1023px */ @media (min-width: 768px) and (max-width: 1023px){ .items { width: 50%; } } /* Display 1 item per row for browsers with width <= 767px */ @media (max-width: 767px){ .items { width: 100%; } } 

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

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