【问题标题】:HTML range element vertical position difference between Chrome and FirefoxChrome和Firefox之间的HTML范围元素垂直位置差异
【发布时间】:2016-11-03 19:44:36
【问题描述】:

我有一个 CSS 样式问题:Firefox 和 Chrome 浏览器在 HTML 元素的 CSS 样式方面存在差异。

range 元素在 Firefox 中的显示高度与在 Chrome 中不同。我添加了两个屏幕截图。

火狐:

Chrome:

Chrome 中的 range 元素被压在“内容”div 的上边缘。在 Firefox 中,范围元素和“内容”div 的上边缘之间有一个小空间 - 如红色标记所示。

如何在 Firefox 和 Chrome 中将 range 元素设置为相同的高度?换句话说:我希望 range 元素垂直位于“内容”div 的中心。

HTML:

<div id="container">
 ...
 <div id="content">
  <input type="range" id="bar" min="0" max="100" step="1" value="0"/>
 </div>
</div>  

CSS:

*{
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    margin-top: 0;
    margin-left: 0;
    margin-right: 0;
    margin-bottom: 0;
}

#container {
    width: 640px;
    height: 372px;
    background: #000;
    margin: auto;
}

#content {
    width: 625px;
    height: 25px;
    background-color: #333;
    margin: auto;
    text-align: center;
    border-radius: 4px;
}

input[type=range] {
    -webkit-appearance: none;
}

input[type=range]::-webkit-slider-runnable-track {
    height: 5px;
    background: #ddd;
    border: none;
    border-radius: 0px;
}

input[type=range]::-moz-range-track {
    height: 5px;
    background: #ddd;
    border: none;
    border-radius: 0px;
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    在所有需要做很多事情的浏览器上正确设置&lt;input type="range"&gt; 的样式

    1.你需要隐藏很多默认浏览器样式

    input[type=range] {
      -webkit-appearance: none; /* Hides the slider so that custom slider can be made */
      width: 100%; /* Specific width is required for Firefox. */
      background: transparent; /* Otherwise white in Chrome */
    }
    
    input[type=range]::-webkit-slider-thumb {
      -webkit-appearance: none;
    }
    
    input[type=range]:focus {
      outline: none; /* Removes the blue border. You should probably do some kind of focus styling for accessibility reasons though. */
    }
    
    input[type=range]::-ms-track {
      width: 100%;
      cursor: pointer;
    
      /* Hides the slider so custom styles can be added */
      background: transparent; 
      border-color: transparent;
      color: transparent;
    }
    

    2。那么你需要为轨道设置样式

    input[type=range]::-webkit-slider-runnable-track {
      width: 100%;
      height: 8.4px;
      cursor: pointer;
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
      background: #3071a9;
      border-radius: 1.3px;
      border: 0.2px solid #010101;
    }
    
    input[type=range]:focus::-webkit-slider-runnable-track {
      background: #367ebd;
    }
    
    input[type=range]::-moz-range-track {
      width: 100%;
      height: 8.4px;
      cursor: pointer;
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
      background: #3071a9;
      border-radius: 1.3px;
      border: 0.2px solid #010101;
    }
    
    input[type=range]::-ms-track {
      width: 100%;
      height: 8.4px;
      cursor: pointer;
      background: transparent;
      border-color: transparent;
      border-width: 16px 0;
      color: transparent;
    }
    input[type=range]::-ms-fill-lower {
      background: #2a6495;
      border: 0.2px solid #010101;
      border-radius: 2.6px;
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
    }
    input[type=range]:focus::-ms-fill-lower {
      background: #3071a9;
    }
    input[type=range]::-ms-fill-upper {
      background: #3071a9;
      border: 0.2px solid #010101;
      border-radius: 2.6px;
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
    }
    input[type=range]:focus::-ms-fill-upper {
      background: #367ebd;
    }
    

    3.那么你需要给拇指设置样式

        /* Special styling for WebKit/Blink */
    input[type=range]::-webkit-slider-thumb {
      -webkit-appearance: none;
      border: 1px solid #000000;
      height: 36px;
      width: 16px;
      border-radius: 3px;
      background: #ffffff;
      cursor: pointer;
      margin-top: -14px; /* You need to specify a margin in Chrome, but in Firefox and IE it is automatic */
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d; /* Add cool effects to your sliders! */
    }
    
    /* All the same stuff for Firefox */
    input[type=range]::-moz-range-thumb {
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
      border: 1px solid #000000;
      height: 36px;
      width: 16px;
      border-radius: 3px;
      background: #ffffff;
      cursor: pointer;
    }
    
    /* All the same stuff for IE */
    input[type=range]::-ms-thumb {
      box-shadow: 1px 1px 1px #000000, 0px 0px 1px #0d0d0d;
      border: 1px solid #000000;
      height: 36px;
      width: 16px;
      border-radius: 3px;
      background: #ffffff;
      cursor: pointer;
    }
    

    抱歉复制并粘贴 - 我编辑了它,现在它应该可以工作了

    【讨论】:

    • 试过了,但是Firefox和Chrome中元素的高度还是有区别的。我需要该元素处于相同的高度,即:垂直位于“内容”div的中心。
    • 不,高度仍然没有改变。我在 HTML 页面的顶部添加了一个 标记,现在 Chrome 中的范围元素显示低于 Firefox 中的范围元素。在 Firefox 中,它仍然没有垂直显示在灰色 div 的中心。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 2018-09-09
    • 2019-08-29
    • 1970-01-01
    相关资源
    最近更新 更多