Firefox 输入元素(搜索框)中的光标位置错误
我在搜索框中的光标出现问题,该光标在 Firefox 中显示得太靠左,而在 Chrome 和 Safari 中看起来很好。 CSS 使用 Modernizr 在可能的情况下显示边框半径,因此问题不涉及提供纯正方形的 IE盒子。
HTML 如下:
<form action="/search-results/" id="search" role="search">
<input type="search" placeholder="Search this site here" name="q" results=5 id="q" autocomplete="off" size="31"/>
</form>
CSS 如下:
input::-webkit-input-placeholder {
color: #999;
}
input:-moz-placeholder {
color: #999;
显示占位符并
.borderradius #search input#q {
width: 180px;
height: 20px;
font: 11px "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
margin: 0;
padding: 0;
background: #fcfcfc;
border: 1px solid #d1d1d1;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
text-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15) inset;
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15) inset;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15) inset;
-webkit-transition: all 0.7s ease 0s;
-moz-transition: all 0.7s ease 0s;
-o-transition: all 0.7s ease 0s;
transition: all 0.7s ease 0s;
}
.no-borderradius #search input#q {
border: 1px solid #ccc;
background: #eee;
font: inherit;
width: 170px;
height: 20px;
padding: 0 0 0 8px
}
设置框的样式。
我还添加了这个:(
/* Remove default input type="search styling */
input[type="search"] {
-webkit-appearance: textfield; /* You could also use none */
}
我在 这篇文章),它具有删除 Webkit 搜索框的“强制”样式的效果,这意味着可以使其一致地呈现在 Mozilla 中具有正确的样式。
在输入标记中添加 results=5
属性(尽管当前尚未验证)会在 Webkit 中显示放大镜。
上述代码也可以在我的网站上在线查看。
搜索框显示如下:
Safari
Chrome
Firefox
Firefox 没有显示放大镜,但没关系。另一方面,虽然调整框上的边距和/或填充可以纠正错误的(太左)Firefox 光标显示,但这样做的代价必然是在 Webkit 浏览器中将其推得太右(不可接受的)成本。在这种情况下,我还没有找到只针对 Mozilla 的方法。有任何建议欢迎...
I'm having trouble with the cursor in a searchbox that displays too far left in Firefox, whereas it looks fine in Chrome and Safari. The CSS uses Modernizr to display a borderradius where possible, so the issue doesn't concern IE which is served a plain square box.
Here's the HTML:
<form action="/search-results/" id="search" role="search">
<input type="search" placeholder="Search this site here" name="q" results=5 id="q" autocomplete="off" size="31"/>
</form>
The CSS is as follows:
input::-webkit-input-placeholder {
color: #999;
}
input:-moz-placeholder {
color: #999;
to display the placeholder and
.borderradius #search input#q {
width: 180px;
height: 20px;
font: 11px "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
margin: 0;
padding: 0;
background: #fcfcfc;
border: 1px solid #d1d1d1;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px;
text-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15) inset;
-moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15) inset;
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15) inset;
-webkit-transition: all 0.7s ease 0s;
-moz-transition: all 0.7s ease 0s;
-o-transition: all 0.7s ease 0s;
transition: all 0.7s ease 0s;
}
.no-borderradius #search input#q {
border: 1px solid #ccc;
background: #eee;
font: inherit;
width: 170px;
height: 20px;
padding: 0 0 0 8px
}
to style the box.
I also added this:
/* Remove default input type="search styling */
input[type="search"] {
-webkit-appearance: textfield; /* You could also use none */
}
(which I saw in this article) which has the effect of removing the 'enforced' styling of the Webkit search box—meaning it can then be made to render consistently in Mozilla with the right styling.
Adding the results=5
attribute in the input tag, although it doesn't currently validate, displays a magnifying glass in Webkit.
The above code can also be checked online on my site.
The search box displays as follows:
Safari
Chrome
Firefox
Firefox doesn't display the magnifying glass, but that's fine. On the other hand, while adjusting the margin and/or padding on the box can correct the wrong (too far left) Firefox cursor display, it necessarily does so at the (unacceptable) cost of pushing it too far right in Webkit browsers. And I haven't found a way of targeting just Mozilla in this instance. Any suggestions welcome...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
公平地说,Firefox 并没有显示错误的光标位置。如果删除无效的
results
属性,您将看到光标位置位于 webkit 中的相同位置。我不确定您尝试过什么填充/边距,但下面的填充在两种浏览器中看起来都很好。使用
box-sizing:
属性,以便输入在所有浏览器中的行为方式相同,并且不会因额外的填充而使框变大。To be fair, Firefox isn't displaying the wrong cursor position. If you remove the invalid
results
attribute, you will see the cursor position is in the same place in webkit.I am not sure what padding/margins you have tried, but the padding below looked fine in both browsers. The
box-sizing:
properties are used so that the input acts the same way in all browsers and doesn't make the box bigger with the extra padding.这很复杂 - 在我看来,在新表单元素的本机样式在跨浏览器上正常工作之前,我们还有很长的路要走。我已经整理了a fiddle,它显示了在 webkit 浏览器中正确显示搜索输入所需的代码,并且火狐。
Trent Walton 的这篇文章介绍了影响外观的各种属性搜索输入,并且总是出色的 CSS 技巧 也有一些有用的信息。
基本上,我使用此代码推翻了浏览器的本机样式:
然后我使用了 -moz-padding-start 属性(谁知道?)仅为 Firefox 添加一些左侧填充。
因此,虽然这可行,但要跨越这些障碍还远非理想。关于 @tw16 的答案的注释:
-moz-box-sizing
目前甚至被最新版本的 Firefox 使用,但将来可能会被无前缀版本取代,这将有可能打破这个解决方案。公平地说,-moz-padding-start
可能会在未来的某个时候被类似地替换,尽管 CSS 编写模块 似乎更晦涩(至少对我来说)。This is complex - it seems to me that we've some way to go before native styling of the new form elements works well cross-browser. I have put together a fiddle which shows the code necessary to display your search input properly in both webkit browsers and Firefox.
This article by Trent Walton describes the various properties that affect the appearance of search inputs, and the always brilliant CSS Tricks also has some useful information.
Basically, I've over-ruled the browser's native styling using this code:
I've then used the -moz-padding-start property (who knew?) to add some left padding only for Firefox.
So, although that works, it is far from ideal having to jump through those hoops. A note on @tw16's answer:
-moz-box-sizing
is currently used by even the most recent version of Firefox, but is likely to be replaced by the non-prefixed version in future, which will potentially break that solution. To be fair,-moz-padding-start
will presumably be similarly replaced at some point in the future, though the CSS Writing Module seems more obscure (to me at least).