简体   繁体   English

Flexbox和Firefox flex-basis

[英]Flexbox and Firefox flex-basis

I've been doing some browser testing on my flexbox layout, and it looks great in all evergreen browsers except Firefox. 我一直在对flexbox布局进行一些浏览器测试,在除Firefox之外的所有常绿浏览器中看起来都很棒。 It should look like this - all Grid-Cell items being given an auto flex-basis based on the item content. 它看起来应该像这样-根据项目内容为所有Grid-Cell项目提供自动伸缩基础。 在此处输入图片说明 However on Firefox 51 (macOS) it's all one huge single column. 但是,在Firefox 51(macOS)上,这是一个巨大的单一列。 在此处输入图片说明

Here's a Codepen: http://codepen.io/toddsby/pen/jyZabo with the full example. 这是一个Codepen: http ://codepen.io/toddsby/pen/jyZabo及其完整示例。

Here's the code I'm using 这是我正在使用的代码

CSS 的CSS

.Grid {
  display: -moz-box;
  display: -ms-flexbox;  
  display: -webkit-flex;  
  display: flex;
  -webkit-flex-wrap: wrap;
  flex-wrap: wrap;
  list-style: none;
  margin: 0;
  padding: 0;
}

.Grid-fit > .Grid-cell {
  -webkit-flex: 0 0 auto; 
  -ms-flex: 0 0 auto; 
  flex: 0 0 auto;
}

HTML 的HTML

<ul class="Grid Grid-fit">
  <li class="Grid-cell">
    ...{content}
  </li>
</ul>

I looked through Philip Walton's Flexbugs but none of those suggestions made a difference. 我浏览了Philip Walton的Flexbug,但是这些建议都没有改变。 Anyone else run into this? 还有其他人遇到吗? Any help is greatly appreciated! 任何帮助是极大的赞赏!

The issue lies on the svg percentage, since they are floating, dimensions are not being considered: 问题在于svg百分比,因为它们是浮动的,因此未考虑尺寸:

svg {
  width:100%;
  height:100%;
}

Change it to desired px , but note not all browsers render svgs the same: 将其更改为所需的px ,但请注意,并非所有浏览器都将svgs呈现为相同:

svg {
  width: 300px;
  height: 300px;
}

Here's a code pen that maintains the layout in all browsers, although firefox has a bit more margin-bottom due to svg height. 这是一支代码笔,可在所有浏览器中维护布局,尽管由于svg高度,firefox的底边有些偏高。

Note also the .Grid-cell typo as mentioned by Michael_B. 还请注意Michael_B提到的.Grid-cell错字。

http://codepen.io/anon/pen/MJQBNJ http://codepen.io/anon/pen/MJQBNJ

This is Safari: 这是Safari:

在此处输入图片说明

You can reduce that row gap by lowering height to let's say 200px : 您可以通过将高度降低到200px来减小200px

在此处输入图片说明

But Firefox will not let you impact height only and will reduce width as well: 但是Firefox不仅会影响高度,还会减小宽度:

在此处输入图片说明

So you could try to workaround that gap by removing preserveAspectRatio="xMinYMin meet" on the svg and re-styling it or leave it in all browsers with a moderate gap: 因此,您可以尝试通过消除svg上的preserveAspectRatio="xMinYMin meet"并重新设置其样式或将其留在所有浏览器中以适当的间隔来解决该差距:

在此处输入图片说明

In your demo, one key selector isn't working: 在您的演示中,一个键选择器不起作用:

.Grid-cell (CSS) vs .Grid-Cell (HTML)

Hence, the flex: 0 0 auto isn't working, in any case. 因此,在任何情况下, flex: 0 0 auto都不起作用。

Once that is fixed, try using flex: 1 instead of flex: 0 0 auto . 解决此问题后,请尝试使用flex: 1而不是flex: 0 0 auto

Solution

The key was @Syden's mention of browsers handling SVG rendering differently. 关键是@Syden提到浏览器以不同方式处理SVG渲染。 It appears Firefox requires a height attribute to properly render the element. Firefox似乎需要一个height属性才能正确呈现该元素。 The simple solution to the broken layout was updating the .item svg css selector with a max-height property as follows: 解决损坏的布局的简单方法是使用max-height属性更新.item svg css选择器,如下所示:

.item svg {
  float: left;
  width: 60%;
  max-height: 300px;
}

Here's the full code example w/ bugfix on Codepen: http://codepen.io/toddsby/pen/PWRqXO 这是在Codepen上带有错误修正的完整代码示例: http ://codepen.io/toddsby/pen/PWRqXO

Firefox now rendering layout correctly: Firefox现在可以正确呈现布局: 在此处输入图片说明

Related Note: Here's a great explanation of how the SVG preserveAspectRatio works: http://unmatchedstyle.com/news/svg-and-the-preserveaspectratio-property.php 相关说明:这是有关SVG的prepareAspectRatio工作方式的很好解释: http : //unmatchedstyle.com/news/svg-and-the-preserveaspectratio-property.php

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

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