如何删除所有默认的 Webkit 搜索字段样式?

发布于 2025-01-08 04:50:01 字数 349 浏览 4 评论 0 原文

默认情况下, 呈现为 Mac OS X 上的“本机”搜索字段(圆角、清除按钮等)。我想完全删除此自定义样式,以便输入看起来与等效文本输入相同 (),但同时将输入类型设置为 搜索

我尝试过 -webkit-appearance: none; ,它非常接近......但是边距/填充发生了一些有趣的事情,我似乎无法覆盖,这导致了宽度搜索字段的渲染比文本输入短约 20px。

是否还有另一个我不知道的 -webkit- 特定属性可以完全禁用样式?

By default, <input type="search" /> renders like a "native" search field on Mac OS X (rounded corners, clear button, etc.). I want to completely remove this custom styling so that the input looks identical to an equivalent text input (<input type="text" />), but while keeping the input type set to search.

I've tried -webkit-appearance: none;, which gets it very close...but there's something funny going on with margins/padding that I can't seem to override, which causes the width of the search field to render ~20px shorter than a text input.

Is there another -webkit- specific property I'm not aware of to totally disable the styling?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫瑟鸿黎 2025-01-15 04:50:01

尝试这些样式:

input[type="search"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-results-button,
input[type="search"]::-webkit-search-results-decoration {
  -webkit-appearance:none;
}

来源:http://css-tricks.com/ webkit-html5-search-inputs/#comment-82432

Try these stylings:

input[type="search"]::-webkit-search-decoration,
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-results-button,
input[type="search"]::-webkit-search-results-decoration {
  -webkit-appearance:none;
}

Source: http://css-tricks.com/webkit-html5-search-inputs/#comment-82432

第几種人 2025-01-15 04:50:01

对于那些仍然需要支持 IE 的用户,值得一提的是,您需要一套完全不同的供应商样式来从 Internet Explorer 中删除“×”

。 blog.maximerouiller.com/post/remove-the-x-from-internet-explorer-and-chrome-input-type-search/" rel="noreferrer">从 Internet Explorer 和 Chrome 输入类型搜索中删除 X

/* clears the 'X' from Internet Explorer */
input.hide-clear[type=search]::-ms-clear,
input.hide-clear[type=search]::-ms-reveal {
  display: none;
  width: 0;
  height: 0; 
}

/* clears the 'X' from Chrome */
input.hide-clear[type="search"]::-webkit-search-decoration,
input.hide-clear[type="search"]::-webkit-search-cancel-button,
input.hide-clear[type="search"]::-webkit-search-results-button,
input.hide-clear[type="search"]::-webkit-search-results-decoration {
  display: none; 
}

演示在堆栈片段和jsFiddle

label, input {display: block; margin-bottom: 1rem;}

/* clears the 'X' from Internet Explorer */
input.hide-clear[type=search]::-ms-clear,
input.hide-clear[type=search]::-ms-reveal {
  display: none;
  width: 0;
  height: 0; 
}

/* clears the 'X' from Chrome */
input.hide-clear[type="search"]::-webkit-search-decoration,
input.hide-clear[type="search"]::-webkit-search-cancel-button,
input.hide-clear[type="search"]::-webkit-search-results-button,
input.hide-clear[type="search"]::-webkit-search-results-decoration {
  display: none; 
}
<label >
  default
  <input type="search" value="query">  
</label>


<label >
  without x
  <input type="search" value="query" class="hide-clear" >  
</label>

For those that still need to support IE, it's worth mentioning that you need an entirely different set of vendor styles to remove the '×' from Internet Explorer

Per the article Remove the X from Internet Explorer and Chrome input type search:

/* clears the 'X' from Internet Explorer */
input.hide-clear[type=search]::-ms-clear,
input.hide-clear[type=search]::-ms-reveal {
  display: none;
  width: 0;
  height: 0; 
}

/* clears the 'X' from Chrome */
input.hide-clear[type="search"]::-webkit-search-decoration,
input.hide-clear[type="search"]::-webkit-search-cancel-button,
input.hide-clear[type="search"]::-webkit-search-results-button,
input.hide-clear[type="search"]::-webkit-search-results-decoration {
  display: none; 
}

Demo in Stack Snippets & jsFiddle

label, input {display: block; margin-bottom: 1rem;}

/* clears the 'X' from Internet Explorer */
input.hide-clear[type=search]::-ms-clear,
input.hide-clear[type=search]::-ms-reveal {
  display: none;
  width: 0;
  height: 0; 
}

/* clears the 'X' from Chrome */
input.hide-clear[type="search"]::-webkit-search-decoration,
input.hide-clear[type="search"]::-webkit-search-cancel-button,
input.hide-clear[type="search"]::-webkit-search-results-button,
input.hide-clear[type="search"]::-webkit-search-results-decoration {
  display: none; 
}
<label >
  default
  <input type="search" value="query">  
</label>


<label >
  without x
  <input type="search" value="query" class="hide-clear" >  
</label>

南风起 2025-01-15 04:50:01

到 2023 年,这可以得到简化。

Firefox 搜索字段显示为文本字段。
只有 Chrome 和 Safari 会更改搜索字段的外观。

  • 删除取消按钮(Chrome 和 Safari):
[type="search"]::-webkit-search-cancel-button {
  appearance: none;
}
  • 删除 macOS 下的 Safari 左侧内边距或 iOS 下左侧的放大镜:
[type="search"]::-webkit-search-decoration {
  appearance: none;
}
  • 两者:
[type="search"]::-webkit-search-decoration,
[type="search"]::-webkit-search-cancel-button {
  appearance: none;
}
  • 使搜索字段在 Safari 下显示为文本字段:(
[type="search"] {
  appearance: textfield;
  outline-offset: -2px; /* Fix outline style in Safari */
}

来源:https://github.com/necolas/normalize.css/blob/8.0.1/normalize.css#L285-L293

-2px“打破" Chrome :-/

  • 一些解释:

-webkit-search-results-button-webkit-search-results-decoration 不需要,因为 Chrome >= 53(发布于2016) 不再支持 results 属性。

Chrome 117 影子 DOM 仅使用一个伪元素 -webkit-search-cancel-button 特定于 input type="search"

input type=

另外 Safari 17.0 Shadow DOM 使用 -webkit-search-decoration< /code> 在 macOS 13.6 下添加左侧内边距,在 iOS 17.0 下在左侧添加放大镜:

input type=

演示:

[type="search"].no-cancel-button::-webkit-search-cancel-button {
  appearance: none;
}

[type="search"].no-decoration::-webkit-search-decoration {
  appearance: none;
}

[type="search"].no-decoration-no-cancel-button::-webkit-search-decoration,
[type="search"].no-decoration-no-cancel-button::-webkit-search-cancel-button {
  appearance: none;
}

[type="search"].as-textfield {
  appearance: textfield;
  outline-offset: -2px;
}
<input type="search" placeholder="input type='search'">

<br><br>

<input type="search" class="no-cancel-button" placeholder="no cancel button">

<br><br>

<input type="search" class="no-decoration" placeholder="no decoration">

<br><br>

<input type="search" class="no-decoration-no-cancel-button" placeholder="no decoration no cancel button">

<br><br>

<input type="search" class="as-textfield" placeholder="as textfield">

<br><br>

<input type="text" placeholder="input type='text'">

As of 2023, this can be simplified.

Firefox search field appears as a text field.
Only Chrome and Safari change the search field appearance.

  • Remove cancel button (Chrome & Safari):
[type="search"]::-webkit-search-cancel-button {
  appearance: none;
}
  • Remove Safari left inner padding under macOS or the magnifying glass on the left under iOS:
[type="search"]::-webkit-search-decoration {
  appearance: none;
}
  • Both:
[type="search"]::-webkit-search-decoration,
[type="search"]::-webkit-search-cancel-button {
  appearance: none;
}
  • Make the search field to be displayed like a text field under Safari:
[type="search"] {
  appearance: textfield;
  outline-offset: -2px; /* Fix outline style in Safari */
}

(source: https://github.com/necolas/normalize.css/blob/8.0.1/normalize.css#L285-L293)

The -2px "breaks" Chrome :-/

  • Some explanations:

-webkit-search-results-button and -webkit-search-results-decoration are not needed because Chrome >= 53 (released in 2016) does not support the results attribute anymore.

Chrome 117 shadow DOM uses only one pseudo-element -webkit-search-cancel-button which is specific to input type="search":

input type="search" Chrome 117 shadow DOM

In addition Safari 17.0 shadow DOM uses -webkit-search-decoration which adds a left inner padding under macOS 13.6 or a magnifying glass on the left under iOS 17.0:

input type="search" Safari 17.0 shadow DOM

Demo:

[type="search"].no-cancel-button::-webkit-search-cancel-button {
  appearance: none;
}

[type="search"].no-decoration::-webkit-search-decoration {
  appearance: none;
}

[type="search"].no-decoration-no-cancel-button::-webkit-search-decoration,
[type="search"].no-decoration-no-cancel-button::-webkit-search-cancel-button {
  appearance: none;
}

[type="search"].as-textfield {
  appearance: textfield;
  outline-offset: -2px;
}
<input type="search" placeholder="input type='search'">

<br><br>

<input type="search" class="no-cancel-button" placeholder="no cancel button">

<br><br>

<input type="search" class="no-decoration" placeholder="no decoration">

<br><br>

<input type="search" class="no-decoration-no-cancel-button" placeholder="no decoration no cancel button">

<br><br>

<input type="search" class="as-textfield" placeholder="as textfield">

<br><br>

<input type="text" placeholder="input type='text'">

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文