简体   繁体   English

大小不同的浮动流体正方形的大小调整问题

[英]Resizing issues with floated fluid squares of different sizes

I hit a wall when coding my design that I can't get past since yesterday afternoon. 自从昨天下午以来,我在编码设计时碰壁了。 I have a menu with a classic unordered list and I'm trying to style it into a tile "gallery". 我有一个带有经典无序列表的菜单,并且我试图将其样式化为图块“图库”。 I'm working on a 12 column grid and the square tiles are fluid . 我正在使用12列网格,并且正方形瓷砖是流动的

This is what it looks like when the squares are floated : http://jsfiddle.net/Lzvna/1/ 这是正方形浮动时的样子: http : //jsfiddle.net/Lzvna/1/

As you can see, the li:first-child square takes up the space of 4 regular sized li squares on the right. 如您所见, li:first-child正方形在右侧占据4个规则大小的li正方形的空间。 When you resize the window, the squares flex as expected, due to their percentage width and padding values, but when you hit certain resolutions, the last square in this list will get bumped under the 4 squares on the right. 调整窗口大小时,由于它们的百分比widthpadding值,正方形会按预期弯曲,但是当您达到某些分辨率时,此列表中的最后一个正方形将在右侧的4个正方形下方碰撞。

The problem is actually with the 1st square because it doesn't resize "in sync" with the 4 squares on the right when the elements are floated. 问题实际上出在第一个正方形上,因为浮动元素时它不会与右侧的四个正方形“同步”调整大小。 When you inspect it you'll see that the 1st square is ~1px larger than it should be. 当您检查它时,您会看到第一个正方形比应有的要大〜1px。 It seems to be worst on Opera and Safari. 对于Opera和Safari来说,情况似乎最糟。 Chrome, FF and IE will break it too, but Chrome seems to be the most benevolent. Chrome,FF和IE也会破坏它,但Chrome似乎是最仁慈的。

I tried reducing the width and padding on the 1st square up to 3px, but it would still break in Opera and the layout would start to get ugly. 我尝试将第一个正方形的宽度和填充减小到3px,但在Opera中仍然会中断,并且布局开始变得难看。

Instead of floating, I also tried displaying the squares inline with display: inline-block . 除了浮动之外,我还尝试使用display: inline-block 内联显示正方形。 Here the resizing issue disappears (the squares resize "in-sync"), but in turn I have a problem with flowing the 4 squares on the right on the same line with the 1st square: http://jsfiddle.net/UaCPN/ 在这里,调整大小的问题消失了(正方形调整为“ in-sync”大小),但是依次使右侧的4个正方形与第1个正方形在同一行上出现问题: http : //jsfiddle.net/UaCPN/

Does anybody know how to fix this resizing issue when floating? 有人知道浮动时如何解决此大小调整问题吗?

If not, is there a way to get the 4 squares to flow the same way when displaying inline-block? 如果不是,在显示行内块时,是否有办法使4个正方形以相同的方式流动?

-- Edit -- -编辑-

Additional info 附加信息

  • For smaller screens I plan to reduce the number of columns and flow them under the li:first-child , which will take care of this resizing issue, as I haven't spotted a problem when the elements beside each other have the same width and height, ie: google "Fluid Squares V2" (sorry, can't post more links yet). 对于较小的屏幕,我计划减少列数,并将其流到li:first-child ,这将解决此大小调整问题,因为当彼此相邻的元素具有相同的宽度和宽度时,我没有发现任何问题高度,即:google“ Fluid Squares V2”(抱歉,无法发布更多链接)。

  • The ammount of list items is not fixed. 列表项的数量不是固定的。 I want to be able to add more to the top of the stack in the markup and have the styling take care of the rest. 我希望能够在标记的堆栈顶部添加更多内容,并让样式负责其余部分。

I Believe this happens because it's not possible to render portion of pixel, hence you don't need such great precision in you percentage. 我相信发生这种情况是因为不可能渲染像素的一部分,因此您不需要百分比那么高的精度。 Because of the same reason the smaller the screen the more chance your proportions will not break even. 由于相同的原因,屏幕越小,您的比例不会达到收支平衡的机会就越大。 So to make it look smoother make different proportions for smaller srceens. 因此,要使其看起来更平滑,请为较小的场景设置不同的比例。

#portfolio-links li:first-child {
  padding: 0 0 47.9% 0;
  width: 47.9%;
}
@media screen and (max-width: 500px){
    #portfolio-links li:first-child {
      padding: 0 0 47.6% 0;
      width: 47.6%;
}
}
@media screen and (max-width: 250px){
    #portfolio-links li:first-child {
      padding: 0 0 47.1% 0;
      width: 47.1%;
    }
}

Downside is that it will not be of exact same height. 缺点是高度不会完全相同。

Fiddle : http://jsfiddle.net/Lzvna/4/ 小提琴http : //jsfiddle.net/Lzvna/4/

Edit: 编辑:
Option 2: 选项2:
Another solution is if amount of elements is fixed, and you don't mind setting positioning for each of them you can position elements absolutely and take advantage of margin-top as it's calculated relative to the width: 另一个解决方案是,如果元素的数量是固定的,并且您不介意为每个元素设置定位,则可以绝对定位元素并利用margin-top相对于宽度的计算:

Eg: 例如:

#portfolio-links li:nth-child(4) {
  margin-top: 27%;
  left:50%;
}

Downside: very manual. 缺点:非常手动。 Upside: very precise looks. 优势:非常精确的外观。

Fiddle : http://jsfiddle.net/UaCPN/1/ 小提琴http : //jsfiddle.net/UaCPN/1/

Yes! 是! Gotcha :D 陷阱:D

So in the end, all I had to do was clear the 6th <li> in the stack: http://jsfiddle.net/Lzvna/5/ 所以最后,我要做的就是清除堆栈中的第六个<li>http : //jsfiddle.net/Lzvna/5/

I think that clearing the element (returning it to normal flow) enables it's top margin to be sacrificed when the 1st element pushes down on it. 我认为清除元素(使其恢复正常流动)可以在第一个元素向其下推时牺牲其顶部边距。 Unlike when it's floated because according to the spec when an element is floated, its margins never collapse. 与浮动时不同,因为根据规范,元素浮动时,其边距永远不会崩溃。

Margins between a floated box and any other box do not collapse (not even between a float and its in-flow children). 浮动框与任何其他框之间的边距不会折叠(甚至在浮动框及其流入子框之间也不会折叠)。

Source: http://www.w3.org/TR/CSS2/box.html#collapsing-margins 资料来源: http//www.w3.org/TR/CSS2/box.html#collapsing-margins

Everytime I think I have normal document flow, floats and clearing under my belt, something like this comes along... Please correct me, if my understanding is wrong. 每当我认为我的文件流正常,漂浮并清理时,就会出现类似的情况……如果我的理解是错误的,请纠正我。

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

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