在Chrome和Safari上,CSS的display:flex属性无法正常工作。

3
我使用了弹性盒子属性来让我的部分看起来像这样:

enter image description here

在Chrome上运行良好,但我注意到在Firefox和Safari上有一些差异。

这是Chrome的外观: enter image description here

但在Firefox上,我无法像我想要的那样应用1%的边距,如红色信号所示:

enter image description here

在Safari上,这些框都是一个接一个的:

enter image description here

这是一个WordPress网站,但尚未上线。以下是我的html结构:
    <section id="services">

        // here goes the title of the container

        <div class="main-container col-lg">  

               // here go all the box

         <div class="services-container">    

               // this one of the boxes              
         </div>
        </div>
    </section>

并且 CSS:

#services {
    background-image: url("img/Services-background.jpg");
    background-color: red;
}
.col-lg {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    margin: initial;
    max-width: 100%;
}
.services-container {
    color: #d6d6d6;
    margin: 1%;
    max-width: 100%;
    width: 30%;
}

如何让这在所有浏览器上都能正常工作?

1
Flexbox在现代浏览器中运行良好。它只是新的,一些浏览器需要特别注意。在这种情况下,使用百分比边距会导致问题。 - Michael Benjamin
4个回答

4

确保flex在所有浏览器上平等工作的最佳方法是使用前缀。

这里是来自MDN的图表,显示了flex box可用的不同浏览器前缀(以及一般的浏览器支持通知)


链接似乎跳来跳去的,只需单击右侧边栏上的“浏览器兼容性”链接,即可查看带前缀的表格。 - pixeldesu

3
 display: flex;
-webkit-display: flex;
-moz-display: flex;
-ms--display: flex;

1
有时候HTML版本可能是原因(在我这种情况下是这样):
我查看了源代码顶部的。我的HTML版本是4.0,这很可能是flex不起作用的原因。一旦更改了它,就正常工作了。
祝你好运...

1
我强烈建议您不要使用flexbox,而是使用floats。 删除所有flex属性,您的CSS应该如下所示:
#services{
    background-image: url(img/Services-background.jpg);
    overflow: auto;
}
.services-container {
    color: #d6d6d6;
    width: 30%;
    float: left;
    margin: 1%;
}

然后您可以添加剩余的样式。它将在所有浏览器上工作。

我正在使用浮动属性,确实在所有浏览器上都能正常工作。感谢。 - user agent
快速2019更新,使用flexbox在打印样式表中仍然表现不可预测。 - Matej Janovčík

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接