我试着做一个看起来不错的搜索栏。我所做的是,我做了一个搜索栏的图像,我把图像添加到输入的背景中,我正在编辑字体出现的位置和大小。唯一我找不到编辑的方法的是当我使用输入类型搜索时出现的小“x”按钮。我想把它稍微向左移动一点,这样它就可以修复我的搜索栏图像。
下面是我的HTML:
<input id="search" name="Search" type="search" value="Search" />
下面是我的CSS:
#search{
width: 480px;
height: 49px;
border: 3px solid black;
padding: 1px 0 0 48px;
font-size: 22px;
color: blue;
background-image: url('images/search.jpg');
background-repeat: no-repeat;
background-position: center;
outline: 0;
}
8条答案
按热度按时间ddarikpa1#
对于那些在这里(像我一样)思考“如何检查此元素以应用自定义样式?"的人,您需要 * 启用用户代理shadow DOM* 以使这些供应商元素可访问。
对于 WebKit(Safari)和 Blink(Chrome、Edge、Opera、Brave)浏览器,请按照以下步骤操作:
1.打开开发工具(Ctrl+Shift+I)
1.找到右上角的齿轮图标,然后单击打开下拉菜单
1.在打开的上下文菜单中,在“* 首选项 "下,找到底部的“ 元素 *”并启用“显示用户代理阴影DOM”
ecr0jaav2#
在Webkit浏览器中设计“x”取消搜索按钮的样式
假设你正在谈论“取消搜索”[X]图标,它只出现在Webkit浏览器(Chrome,Safari,Opera)中,你可以使用
-webkit-search-cancel-button
伪元素。例如:演示:http://jsfiddle.net/5XKrc/1/
截图:
使用此方法,您甚至可以创建自己的取消按钮,例如以下样式:
代替[X]将创建一个红色圆圈。
演示http://jsfiddle.net/5XKrc/3/
截图:
对于IE10及更高版本,您可以使用以下命令移动按钮:
哦,请使用
placeholder="Search"
而不是value="Search"
--当输入为空时,它将显示“搜索”一词--当用户键入内容时,它将自动删除。zrfyljdw3#
2022跨浏览器一致的方法
这是一个跨浏览器的Clear Search“x”按钮的实现,它使用FontAwesome的实心时间圈SVG作为图标,并且适用于深色和浅色背景。它还标准化了Safari,使其采用Chrome实现,仅当表单字段有焦点时才显示图标。
第一个
注1.此S.O.问题明确 * 与clear button伪元素有关,* 该元素仅在基于Webkit的浏览器中支持(Edge、Safari和Chrome)。**目前(2022)Firefox仅支持功能标志后面的搜索清除按钮。**在Firefox公开发布此功能之前,支持Firefox的唯一真正的跨浏览器方法是通过一种变通方法,即利用HTML+CSS和绝对定位的
<input type="reset">
在单击时清除整个表单。请参阅stackoverflow.com/a/37846330请注意,如果您的搜索表单包含多个搜索字段,此解决方法将清除所有单选/复选框选择和其他字段。NB 2.在Edge中,你的里程可能会有所不同,Edge也是基于Webkit的。在我的测试中(通过BrowserStack),Edge的一些版本不支持在
::-webkit-search-cancel-button
伪类中设置background-image: url()
。wbrvyc0a4#
我想移动[小'x'图标]一点点左,这样它就会修复我的搜索栏图像。
用户希望UI中的内容不会移动太多。如果您决定移动“x”图标,请考虑使用伪类并移动搜索图标:
第一次
如果搜索图标嵌入您的背景图像,请将其移动到具有
role="presentation"
属性的第二个图像中,并将其放置在标记中您的input
之后:将其放置在用户期望的位置:
然后使用
:placeholder-shown
伪类隐藏和显示它:如果你愿意的话,你可以设计“x”图标的样式。但是你可能不想再这样做了。
nfs0ujit5#
使用单一CSS规则区块,以
dark
或light
背景执行简单的“X”。执行程式码片段以查看范例。第一个
参考:https://stackoverflow.com/a/52141879/8762323
30byixjq6#
lx0bsm1f7#
Just to highlight better how to figure out such kinds of things by ourselves. As shown and mentioned in @UncaughtTypeError answer above https://stackoverflow.com/a/58484957/7668448
Also in the last section I do go to show how to do different things including changing the color. And with examples.
I loved the answer because it was teaching us how to fish rather than here is the fish
I want to clarify that further for others. Who may didn't notice.
By enabling the
show user agent shadow dom
inelements section
ofpreferences
indevtools
.Now you'll be able to access the
shadow dom
that is created by theagent
(browser engine or browser shortly). In dev tools.What is shadow dom?
From Mozilla doc Using_shadow_DOM
An important aspect of web components is encapsulation — being able to keep the markup structure, style, and behavior hidden and separate from other code on the page so that different parts do not clash, and the code can be kept nice and clean. The Shadow DOM API is a key part of this, providing a way to attach a hidden separated DOM to an element. This article covers the basics of using the Shadow DOM.
You can learn more about it. On the link above.
How to figure out how you would refer to those hidden elements
After enabling
showing the agent shadow dom
. Now you can see those hidden dom elements.Select the element. And check the Styles corresponding selector. As shown by the red box in the illustration above.
And that's it.
Can test an example below:
https://codepen.io/mohamedlamineallal/pen/JjZmdPv
See before enabling and after enabling the
agent dom shadow
.And for demonstration purposes. You can see, I changed the color using
filter
, resize the button withpadding
, and repositioned it withmargin-right
.Elements around manipulating the clear button
A great deal with this method is that now you can use the Dev-tool to experiment faster. That includes figuring out what doesn't work at a better speed. Example
mask-image
withbackground-color
. Or pseudo-element.before
.Things we can figure out:
margin-right
opacity
appearance
can allow us to hide the default behavior fully. [If we want to disable the default button. We can useappearance: none;
(default:appearance: auto;
)]... that at a fast glimpse.
filter
with (invert, sepia, saturate, hue, brightness, contrast) as in( code pen )
You can use the calculator here: 1 , 2
You can see the details of that method here (SO answer)
read the answer (link).
Reimplement all
And surely if the desired output is more complex. Simply disabling the default behavior and re-implement it fully would be more clean and easy and faster.
Using
appearance: none;
will hide and disable the default behavior.You can use
position: absolute;
on aspan
element to keep the input behavior asoutline
on focus (can usepadding-right
for not letting text overflow below the button) and you can also use CSS URL forbackground-image
(SVG icons, you can have utf8 inline encoded SVG where you can change the color, including dynamically if needed) ... [take keywords, if they make sense check them]Absolutely: don't use pseudo-element
:after
. You can't add a js event listener to it. Using a span is cleaner and faster.Here are some examples:
:after
pseudo-element and using the event-pointer workaround. (event bubbling make it possible) (you can read the comment in the playground)The
:after
example demonstrate. Using a flex-box system. Hidding input outline and border. And re-implementing them. Could have used that in the span example. useoutline: none;
to disable the default outline .I advise always to use the dom el (span) way.
bjg7j2ky8#
我不确定这是否是您要查找的内容,但您可以将搜索栏的样式设置为
fiddle
HTML格式
CSS格式