css - 输入类型=范围在 Firefox 中不会拉伸(stretch)

标签 css firefox flexbox

这是一个例子。我想让 slider 拉伸(stretch)到父容器的整个宽度。下面的代码在 Chrome 中看起来像我想要的,但在 Firefox 中 slider 没有拉伸(stretch)。但是,我在他们的跟踪器上找不到针对该问题的任何错误,也许我错过了一些东西?

#block {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: green;
  align-items: stretch;
}

input {
  background-color: yellow;
}
<div id=block>
  <input type=range />
</div>

最佳答案

具有方向的 flex 容器,并且由于align-items的默认值为stretch,将拉伸(stretch) flex 元素以填充它的容器沿着其横轴,与其宽度相同。

所以在你的情况下它应该按原样工作,尽管在 Firefox、Edge 和 IE 中情况并非如此。它们都需要 width: 100% 来让 range 类型的 input 填充容器。

作为比较,text 类型的 inputcheckbox 以及 textarea 可以跨浏览器拉伸(stretch),但同样,checkbox 类型的 input 在 Firefox 中并不存在。

我还不能确定 Firefox(和 Edge/IE)是否正确或者是否存在错误。我已经搜索遍了,但找不到直接答案,但一旦找到,我就会更新这个答案。

因此,从今天开始,我建议将宽度设置为 100%,如果 Chrome 正确,则不会造成任何损害,并且可以稍后将其删除。

#block {
  /* width: 100%;              not needed since it is the default */
  height: 100%;             /* as its parent doesn't have a height, 
                               this doesn't do anything */
  display: flex;
  flex-direction: column;
  background-color: green;
  /* align-items: stretch;     this is the defalt and can be omitted */
}

input, textarea, span {
  background-color: yellow;

  width: 100%;              /* make FF/Edge/IE stretch input range */
  
  margin: 0;                /* remove any default margin an element might have */
  padding: 0;               /* remove any default padding an element might have */

  box-sizing: border-box;   /* include border an element might have in the set width */
}
<div id=block>
  <input type=range />
  
  <!-- added these for comparison -->
  <input type=checkbox />
  <input type=text value="Input text"/>
  <textarea>Textarea</textarea>
  <span>Span</span>
  
</div>

关于css - 输入类型=范围在 Firefox 中不会拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44520709/

相关文章:

css - IE8 & CSS 单项无序列表

javascript - KineticJS:Firefox 与 Chrome 中的动画性能

ios - 地理位置 html5 在网络浏览器上工作,但在 iphone 浏览器上不工作

html - 根据高度水平换行?

html - img 缩放到 div 内的视口(viewport)

html - 无法使 flexbox 垂直和水平居中元素

html - 一个 960px 宽的中心 div,我希望左右两侧的 div 能够平均填充两侧的剩余空间

html - 使div的文本部分透明

jquery - 将动画复选框添加到 Bootstrap 表单检查

firefox - 为什么我只能在 Firefox 中获得证书 "error"?