【问题标题】:IE custom input showing the default appearance显示默认外观的 IE 自定义输入
【发布时间】:2020-07-19 12:42:19
【问题描述】:

我正在尝试创建一个自定义input,它可以是radiocheckbox 类型。但我在 IE 浏览器上遇到 css 问题。

预期结果:

IE浏览器结果

输入 JSX

const CheckBox2: FC<ICheckBox2Props> = ({ label, type = "checkbox", className, labelClassName, ...props }) => {
    return (
        <label className={[styles.container, className ?? ""].join(" ")}>
            <input
                {...props}
                type={type}
            />
            {label && <span className={[styles.label, labelClassName ?? ""].join(" ")} >{label}</span>}
        </label>
    );
};

export default CheckBox2;

输入 CSS

.container {
    display: inline-flex;
    align-items: center;
    margin-right: 48px;

    >input {
        cursor: pointer;

        height: 17px;
        width: 17px;

        box-sizing: border-box;
        border: 1px solid #ABACAD;
        border-radius: 2px;

        -webkit-appearance: none;
        -moz-appearance: none;
        -ms-appearance: none;
        -o-appearance: none;
        appearance: none;

        outline: none;

        &:checked {
            height: 17px;
            width: 17px;

            box-sizing: border-box;
            border: 1px solid #2D3641;
            border-radius: 2px;
            background-color: #2D3641;

            background-image: url(../../../../assets/images/icons/ico_check.png);
            background-size: 10px 9px;
            background-repeat: no-repeat;
            background-position: center;
        }
    }

    .label{
        margin-left: 16px;
    }
}

【问题讨论】:

    标签: javascript css reactjs typescript internet-explorer


    【解决方案1】:

    我可以看到您在代码中使用了 CSS appearance

        -webkit-appearance: none;
        -moz-appearance: none;
        -ms-appearance: none;
        -o-appearance: none;
        appearance: none;
    
    IE 浏览器不支持

    CSS appearance 并且 -ms 前缀无助于修复它。我认为这是问题的原因。

    参考:

    CSS Appearance

    我没有找到任何替代 CSS 外观的方法。

    您可以尝试使用开发者工具查看应用的 CSS 属性。这有助于理解问题。

    【讨论】:

      【解决方案2】:

      既不是最佳解决方案,也不是最佳实践,但这是我带来的:

      我的 JSX

      const CheckBox2: FC<ICheckBox2Props> = ({ label, type = "checkbox", className, labelClassName, ...props }) => {
          return (
              <label className={[styles.container, className ?? ""].join(" ")}>
                  <input
                      {...props}
                      type={type}
                  />
                  <div className={styles.inputContainer}>
                  </div>
                  {label && <span className={[styles.label, labelClassName ?? ""].join(" ")} >{label}</span>}
              </label>
          );
      };
      

      CSS

      .container {
          display: inline-flex;
          align-items: center;
          margin-right: 48px;
          position: relative;
      
          .inputContainer{
              position: absolute;
              display: flex;
              justify-content: center;
              align-items: center;
      
              cursor: pointer;
      
              height: 16px;
              width: 16px;
      
              box-sizing: border-box;
              border: 1px solid #ABACAD;
              border-radius: 2px;
          }
      
          >input{
              opacity: 0;
              height: 16px;
              width: 16px;
      
              &:checked + .inputContainer {
                  height: 16px;
                  width: 16px;
      
                  box-sizing: border-box;
                  border: 1px solid #2D3641;
                  border-radius: 2px;
                  background-color: #2D3641;
      
                  background-image: url(../../../../assets/images/icons/ico_check.png);
                  background-size: 10px 9px;
                  background-repeat: no-repeat;
                  background-position: center;
              } 
          }
      
      }
      
      @media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
          .container{
              .inputContainer{
                  top: 2px;
                  left: 0px
              }
          }
      }
      
      
      

      【讨论】:

      • 感谢您发布此问题的解决方案。我建议您尝试在 48 小时后标记您自己对这个问题的答案,当它可以标记时。它可以在未来帮助其他社区成员解决类似的问题。感谢您的理解。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      相关资源
      最近更新 更多