简体   繁体   English

根据窗口大小用浮动div填充容器并调整div的宽度以设置最大值

[英]fill container with floating divs according to window size & adjust width of divs to set max

im working on a page layout for a magazine. 我正在为杂志制作页面布局。 it now has 5 different categories - each of the categories is displayed on the main site inside a separate div floating left. 现在,它具有5个不同的类别-每个类别都显示在主网站上的一个单独的div中,向左浮动。 so it looks like this 所以看起来像这样

--------------page width-------------
-category1--category2--category3-
-category4--category5-

now i would like to have the three categories in first and second line stretch to take all the space until they reach a set amount of width and then fall back to a lower width to give more categories room in the first row on a page resize: (4 divs with min width does NOT fit inside the page width) 现在我想让第一行和第二行中的三个类别拉伸以占据所有空间,直到它们达到设定的宽度,然后回落到较低的宽度以在页面大小调整的第一行中为更多类别提供空间: (最小宽度的4格不适合页面宽度)

-----------------page width-----------
-category1  --category2  --category3 -
-category4        --category5        -

then on resize (as soon as 4 elements with the min-width fit in): 然后调整大小(只要最小宽度的4个元素适合):

------------------------page width----------
-category1--category2--category3--category4-
-category5-

is this possible with css? CSS可能吗? (i don't think so) or with some javascript calculation. (我不这样认为)或使用一些javascript计算。 i tried a lot, but my java skills are just really bad ... 我尝试了很多,但是我的Java技能真的很糟糕...

thanks so much! 非常感谢! nice greets from vienna 维也纳的好问候

I don't think you need any javascript to achieve this. 我认为您不需要任何JavaScript即可实现此目的。 I think what you're after is a media query within your css. 我认为您追求的是CSS中的媒体查询。

CSS has the ability to target css rules at specific canvas widths, device widths or orientations. CSS能够将CSS规则定位到特定的画布宽度,设备宽度或方向。 So you could have markup like this 所以你可以有这样的标记

<div class="container">
    <div class="category-wrapper"></div>
    <div class="category-wrapper"></div>
    <div class="category-wrapper"></div>
    <div class="category-wrapper"></div>
    <div class="category-wrapper"></div>
</div>

And then change the widths of the category wrappers with CSS like this 然后像这样用CSS更改类别包装的宽度

.category-wrapper{
    float:left;
    width:100px;
}

@media (min-width:500px) and (max-width:950px){ 
    .category-wrapper{
         width:200px;
    }
}

@media (min-width:950px) and (max-width:1024px){ 
    .category-wrapper{
         width:400px;
    }
}

and so on. 等等。

See the spec for media queries here http://www.w3.org/TR/css3-mediaqueries/ a great primer on the concept of this way of working here http://www.alistapart.com/articles/responsive-web-design/ 有关媒体查询的规范,请参见http://www.w3.org/TR/css3-mediaqueries/有关此工作方式概念的入门知识, 网址为http://www.alistapart.com/articles/sensitive-web -设计/

and since not all browsers support them, use Scott Jehl's amazing polyfill https://github.com/scottjehl/Respond to help out the laggers. 并且由于并非所有浏览器都支持它们,请使用Scott Jehl的惊人的polyfill https://github.com/scottjehl/Respond来帮助解决这些问题。

Hope this helps. 希望这可以帮助。

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

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