【问题标题】:input type=range is not stretching in Firefox输入类型=范围在 Firefox 中没有拉伸
【发布时间】:2017-11-15 04:27:04
【问题描述】:

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

#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>

【问题讨论】:

  • 请查看并评论我的回答,如果有不清楚或遗漏的地方,请告诉我。如果不是,那么如果您能接受对您帮助最大的答案,那就太好了。

标签: css firefox flexbox


【解决方案1】:

你必须将&lt;input&gt;标签的宽度设置为100%才能得到你想要的!

#block {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: green;
  padding: 0;
}

input {
  background-color: yellow;
  width: 100%;
  margin: 0;
}
<div id=block>
  <input type=range />
</div>

【讨论】:

  • 您还添加了margin: 0,因为没有它输入会溢出容器。最好使中间 div 具有非零边距。但是它没有回答问题,是 Firefox 中的错误还是我错误地使用了 flexbox 模型?
【解决方案2】:

具有列方向的伸缩容器,并且align-items的默认值为stretch,将拉伸伸缩项目以沿其横轴填充其容器,这与它的宽度。

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

作为比较,text 和 checkbox 类型的 input 和 textarea 可以跨浏览器进行拉伸,但同样input 类型为 checkbox 的 checkbox 不在 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>

【讨论】:

  • 在第一个变体中,您忘记添加margin: 0,就像@Tobi 在另一个答案中所做的那样。第二个变体不适合我的需要——我需要垂直弯曲。然而,同样,它并没有回答这个问题——它是 Firefox 中的错误还是什么?
  • @Grief 更新了我的答案
猜你喜欢
  • 2013-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-23
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多