简体   繁体   English

显示:flex无法在Firefox或Safari浏览器中使用

[英]Display: flex won't work in firefox or safari

I've seen this issue several places on here, but the solutions always had some little fix (eg including -moz-, typos, etc), all of which (I believe) I have checked. 我在这里已经在多个地方看到了这个问题,但是解决方案总是有一些小问题(例如-moz-,错别字等),我已经检查了所有这些问题。 I'm trying to use flex to center some text vertically within its container. 我正在尝试使用flex在其容器中垂直放置一些文本。 It works 100% in Chrome, but in both Firefox and Safari, I haven't had as much luck - the text I'm trying to display always ends up outside my container (typically to the right). 它可以在Chrome中100%正常运行,但是在Firefox和Safari中,我运气都不太好-我尝试显示的文本始终在容器外部(通常在右侧)结束。 In the pen at http://codepen.io/drewtadams/pen/XjYrGB , I'm using squares with set heights/widths, but I need it to be able to work more dynamically than "top: 65px; left: 65px;" http://codepen.io/drewtadams/pen/XjYrGB的笔中,我正在使用设置了高度/宽度的正方形,但我需要它能够比“ top:65px; left:65px; ” - the inline elements (black squares) will be text. -内联元素(黑色正方形)将为文本。 Is there a fix to the display: flex, or is there a better/more conventional way to do this? 显示屏是否可以固定:弯曲,还是有更好/更传统的方式来做到这一点?

HTML: HTML:

<div class="flex-container valign--center">
    <div class="box-container valign--center">
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
    <div class="box-container valign--center">
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
    <div class="box-container valign--center">
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
    <div class="box-container valign--center">
        <div class="box1"></div>
        <div class="box2"></div>
    </div>
</div>

CSS: CSS:

.valign {
    display: -webkit-flex;
    display: -moz-flex;
    display: flex;
    -webkit-justify-content: center;
    -moz-justify-content: center;
    justify-content: center;
}
.valign--center {
    @extend .valign;
    align-items: center;
    -webkit-align-items: center;
    -moz-align-items: center;
    -ms-align-items: center;
}

.flex-container {
    border: 1px dashed red;

    .box-container {

        .box1 {
            background-color: green;
            height: 100px;
            width: 100px;
            margin: 25px;
        }
        .box2 {
            background-color: black;
            display: none;
            height: 20px;
            width: 20px;
            position: absolute;
            z-index: 2;
        }

        &:hover {
            .box1 {
                opacity: 0.6;
            }
            .box2 {
                display: inline;
            }
        }
    }// end .box-container
}// end .flex-container

PS I have a second container I'm playing with below the flex-container in the pen. PS:笔筒内的flex容器下面有一个我正在玩的第二个容器。

Why don't you set their heights to be the same, and then the second container set to vertically align in the center. 为什么不将它们的高度设置为相同,然后将第二个容器设置为在中心垂直对齐。 I would use a plugin like matchHeight.js. 我会使用像matchHeight.js这样的插件。 It's super easy and you just put a matching data tag on each element and it will set the heights. 这非常容易,您只需在每个元素上放置一个匹配的数据标签,它就会设置高度。 for example and . 例如和。 That will match the heights for you and then just use the basic flex box to vertically center them. 这将为您匹配高度,然后只需使用基本的柔韧性框将它们垂直居中。

https://github.com/liabru/jquery-match-height https://github.com/liabru/jquery-match-height

here is some flexbox vertical center code. 这是一些flexbox垂直中心代码。

.vertical_center{
        display:flex;
        flex-direction:column;
        justify-content:center;
        resize:vertical;
        align-items:center;
        min-height:25px;
    }

Found a really simple solution at https://css-tricks.com/snippets/sass/centering-mixin/ using some scss: 使用一些scs在https://css-tricks.com/snippets/sass/centering-mixin/上找到了一个非常简单的解决方案:

@mixin centerer {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

HTML: HTML:

.parent {
    position: relative;
}
.child {
    @include centerer;
}

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

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