【问题标题】:Flexbox from SCSS working correctly on Firefox but not on other browsers (e.g. Chrome, Edge, ...)SCSS 的 Flexbox 在 Firefox 上正常工作,但在其他浏览器上不能正常工作(例如 Chrome、Edge 等)
【发布时间】:2020-11-27 21:04:35
【问题描述】:

我目前正在使用 Gatsby.js 和 SASS 进行样式设计的项目。创建输入字段时,我们遇到了容器及其项目仅在 Firefox 上正确显示的问题,而在 Chrome、Edge、IE 等其他浏览器上则无法正确显示。有趣的是,它在任何浏览器的移动视图上都能正确显示。

我已经尝试了一些方法,当从页面 SCSS 文件中删除 display: flex; 时,它在所有使用的浏览器上都以相同的方式显示(当然,看起来很糟糕)。所有页面都从主/全局文件中获取样式,但也有各自的 SCSS 文件。

取自其组件的输入字段:

<form onSubmit={this.handleSubmit} className={styles.formBox}>
            <fieldset className={styles.formGroup}>
                <label htmlFor="production">Text</label>
                <input
                    type="number"
                    name="production"
                    value={this.state.production}
                    onChange={this.handleInputChange}
                    className={styles.numberInput}
                    placeholder="Enter input"
                />
                <label htmlFor="unit">Text</label>
                <select
                    name="unit"
                    className={styles.selectionField}
                    value={this.state.unit}
                    onChange={this.handleInputChange}
                >
                    <option value="Value1" className={styles.optionField}>Value1</option>
                    <option value="Value2" className={styles.optionField}>Value2</option>
                </select>
                <label htmlFor="unit">Text</label>
                <select
                    name="weight"
                    className={styles.selectionField}
                    value={this.state.weight}
                    onChange={this.handleInputChange}
                >
                    <option value="value1" className={styles.optionField}>value1</option>
                    <option value="value2" className={styles.optionField}>value2</option>
                </select>
                <button type="submit" className="btn text-uppercase btn-primary--orange hover-effect btn-block-xs-only">Text</button>
            </fieldset>
        </form>

SCSS 文件:

$border-radius: .25rem;
$button-border-radius: 290486px;
$input-border-width: 2px;
$dark-blue: #000460;
$font-color: #07112a;

.form-box {
    border-radius: $border-radius;
    box-shadow: 0 22px 45px 0 rgba(0, 0, 0, 0.11);
    background-color: #ffffff;
    padding: 24px;
}

.form-group {
    display: inline-flex;
    justify-content: space-between;
    // display: -webkit-flex;
    // display: -ms-flex;
    // display: -webkit-box;
    // display: flex;

    label {
        position: absolute;
        visibility: hidden;
        text-align: left;
    }

    .number-input {
        border: $input-border-width solid $dark-blue;
        border-radius: $border-radius;
        color: $font-color;
        font-size: 16px;
        margin-right: 1em;
        padding-left: 1em;
        // width: 100%;

        &::-webkit-input-placeholder {
            /* Edge */
            color: $font-color;
            opacity: 1;
        }

        &:-ms-input-placeholder {
            /* Internet Explorer 10-11 */
            color: $font-color;
            opacity: 1;
        }

        &::placeholder {
            color: $font-color;
            opacity: 1;
        }
    }


    .selection-field {
        background-color: $dark-blue;
        background-image:
            linear-gradient(45deg, transparent 50%, $dark-blue 50%),
            linear-gradient(135deg, $dark-blue 50%, transparent 50%),
            radial-gradient(white 70%, transparent 72%);
        background-position:
            calc(100% - 20px) calc(1.25em + 2px),
            calc(100% - 15px) calc(1.25em + 2px),
            calc(100% - .5em) .75em;
        background-size:
            5px 5px,
            5px 5px,
            1.5em 1.5em;
        background-repeat: no-repeat;
        border: $input-border-width solid $dark-blue;
        border-radius: $border-radius;
        color: white;
        font-size: 16px;
        margin-right: 1em;
        min-width: 100px;
        padding-left: .5em;

        -moz-appearance: none;
        -webkit-appearance: none;
        appearance: none;

        &::-ms-expand {
            display: none;
        }

        &:hover {
            cursor: pointer;
        }

        &:focus {
            background-image:
                linear-gradient(45deg, $dark-blue 50%, transparent 50%),
                linear-gradient(135deg, transparent 50%, $dark-blue 50%),
                radial-gradient(white 70%, transparent 72%);
            background-position:
                calc(100% - 15px) 1.25em,
                calc(100% - 20px) 1.25em,
                calc(100% - .5em) .75em;
            background-size:
                5px 5px,
                5px 5px,
                1.5em 1.5em;
            background-repeat: no-repeat;
            color: white;
            outline: 0;
        }

        .option-field {
            text-align: center;
            background-color: white;
            border-radius: 10px;
            color: $font-color;
        }
    }

    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }

    input[type=number] {
        -moz-appearance: textfield;
    }

    button {
        border-radius: $button-border-radius;
    }
}

@media (max-width: 768px) {
    .form-group {
        flex-direction: column;

        .number-input,
        .selection-field {
            height: 55px;
            margin: 0 0 1em 0;
        }
    }
}

Firefox(它的外观:

铬:

我无法完全理解为什么会发生这种情况。接下来我可以尝试什么?

【问题讨论】:

  • 在我看来,Chrome 屏幕截图的缩放比例超过 100%。
  • 不,两张截图都是以 100% 缩放全屏拍摄的。
  • 这是您发布的代码所产生的结果:codepen.io/3rror404/pen/eYZmreY
  • 你能至少发布 HTML 和 CSS 以便它可以运行吗?
  • 您可以轻松地将您发布的代码转换为 HTML 和 CSS。

标签: javascript css reactjs sass flexbox


【解决方案1】:

问题很可能与此有关。

@media (max-width: 768px) {
    .form-group {
        flex-direction: column;

        .number-input,
        .selection-field {
           height: 55px;

您的 Chrome 视口可能较小,导致它们堆叠为列,并且还可以解释输入的裁剪高度

【讨论】:

  • 很抱歉,这对桌面上的全屏视图有何影响?因为它可以在任何移动浏览器上正确显示。
  • 即使您在桌面上,您的视口仍然可能比您想象的要小,从而导致问题。请记住,视口不一定是您的屏幕尺寸。
  • 那么 Firefox 中的视口和其他浏览器(Chrome、Edge、...)有区别吗?
  • 有可能。您是否检查了每个浏览器上的视口大小?
  • 我检查了不同浏览器的视口大小,每个 (1920x969) 的视口大小都相同。是不是 Firefox 的 flexbox 工作方式与其他浏览器不同/更好?因为如前所述,如果我切换到移动视图,它可以在所有浏览器上正常工作。 (如果我从表单组中删除 display: inline-flex ,它在 Firefox 上看起来与在其他浏览器上相同。所以它似乎只适用于 Firefox)。
猜你喜欢
  • 2011-11-06
  • 2016-09-15
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 2015-11-14
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
相关资源
最近更新 更多