我可以在输入字段上使用 :before 或 :after 伪元素吗?
我尝试在 input
字段上使用 :after
CSS 伪元素,但它不起作用。如果我将它与 span
一起使用,它就可以正常工作。
<style type="text/css">
.mystyle:after {content:url(smiley.gif);}
.mystyle {color:red;}
</style>
这有效(将笑脸放在“buu!”之后和“some more”之前)
<span class="mystyle">buuu!</span>a some more
这不起作用 - 它只将 someValue 着色为红色,但没有笑脸。
<input class="mystyle" type="text" value="someValue">
我做错了什么?我应该使用另一个伪选择器吗?
注意:我无法在 input
周围添加 span
,因为它是由第三方控件生成的。
I am trying to use the :after
CSS pseudo-element on an input
field, but it does not work. If I use it with a span
, it works OK.
<style type="text/css">
.mystyle:after {content:url(smiley.gif);}
.mystyle {color:red;}
</style>
This works (puts the smiley after "buu!" and before "some more")
<span class="mystyle">buuu!</span>a some more
This does not work - it only colors someValue in red, but there is no smiley.
<input class="mystyle" type="text" value="someValue">
What am I doing wrong? should I use another pseudo-selector?
Note: I cannot add a span
around my input
, because it is being generated by a third-party control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
:before
和:after
在容器内渲染和 不能包含其他元素。
伪元素只能在容器元素上定义(或者更好地说仅受支持)。因为它们的渲染方式是在容器本身内作为子元素呈现的。
input
不能包含其他元素,因此不支持它们。另一方面,同时也是表单元素的button
支持它们,因为它是其他子元素的容器。如果你问我,如果某些浏览器确实在非容器元素上显示这两个伪元素,那么这是一个错误并且不符合标准。规范直接讲元素内容...
W3C规范
如果我们仔细阅读规范实际上说它们被插入内部包含元素:
看? 元素的文档树内容。据我了解,这意味着在容器内。
:before
and:after
render inside a containerand <input> can not contain other elements.
Pseudo-elements can only be defined (or better said are only supported) on container elements. Because the way they are rendered is within the container itself as a child element.
input
can not contain other elements hence they're not supported. Abutton
on the other hand that's also a form element supports them, because it's a container of other sub-elements.If you ask me, if some browser does display these two pseudo-elements on non-container elements, it's a bug and a non-standard conformance. Specification directly talks about element content...
W3C specification
If we carefully read the specification it actually says that they are inserted inside a containing element:
See? an element's document tree content. As I understand it this means within a container.
Internet Explorer 7 及更低版本中的任何元素都不支持
:after
和:before
。它也不适合用于替换元素,例如表单元素(输入)和图像元素。
换句话说,使用纯 CSS 是不可能的。
但是,如果使用 jquery 您可以使用
API 文档 .after
使用 javascript 附加您的内容。这适用于所有浏览器。
:after
and:before
are not supported in Internet Explorer 7 and under, on any elements.It's also not meant to be used on replaced elements such as form elements (inputs) and image elements.
In other words it's impossible with pure CSS.
However if using jquery you can use
API docs on .after
To append your content with javascript. This will work across all browsers.
奇怪的是,它适用于某些类型的输入。
至少在 Chrome 中,
工作正常,与它相同
,只是
type=text
和其他一些不起作用。Oddly, it works with some types of input.
At least in Chrome,
works fine, same as
It's just
type=text
and some others that don't work.这是另一种方法(假设您可以控制 HTML):添加一个空的
< /code> 就在输入之后,并使用
input.mystyle + span:after
在 CSS 中定位该内容我在 AngularJS 中使用这种方法,因为它会自动将 .ng-invalid 类添加到
表单元素和表单中,但不会添加到 <代码><标签>。
Here's another approach (assuming you have control of the HTML): add an empty
<span></span>
right after the input, and target that in CSS usinginput.mystyle + span:after
I'm using this approach in AngularJS because it will add
.ng-invalid
classes automatically to<input>
form elements, and to the form, but not to the<label>
.:before
和:after
应用于容器内,这意味着您可以将其用于有结尾的元素标签。顺便说一句,自关闭的元素(例如 img/hr/input)也称为“替换元素”,因为它们被替换为各自的内容。 “外部对象”缺乏更好的术语。更好地阅读此处
:before
and:after
are applied inside a container, which means you can use it for elements with an end tag.On a side note, elements which are self-closing (such as img/hr/input) are also known as 'Replaced Elements', as they are replaced with their respective content. "External Objects" for the lack of a better term. A better read here
这里最大的误解是
before
和after
这两个词的含义。它们不引用元素本身,而是引用元素中的内容。因此element:before
位于内容之前,而element:after
位于内容之后,但两者仍在原始元素内。input
元素在 CSS 视图中没有内容,因此没有:before
或:after
伪内容。许多其他无效或被替换的元素也是如此。没有伪元素引用外部元素。
在不同的宇宙中,这些伪元素可能会被称为其他名称,以使这种区别更加清晰。有人甚至可能提出了一个真正位于元素之外的伪元素。到目前为止,这个宇宙还没有出现这种情况。
The biggest misunderstanding here is the meaning of the words
before
andafter
. They do not refer to the element itself, but to the content in the element. Soelement:before
is before the content, andelement:after
is after the content, but both are still inside the original element.The
input
element has no content in the CSS view, and so has no:before
or:after
pseudo content. This is true of many other void or replaced elements.There is no pseudo element referring to outside the element.
In a different universe, these pseudo elements might have been called something else to make this distinction clearer. And someone might even have proposed a pseudo element which is genuinely outside the element. So far, this is not the case in this universe.
我使用
background-image
为必填字段创建红点。片段演示:
或者在Codepen上查看
I used the
background-image
to create the red dot for required fields.Snippet demo:
Or View on Codepen
您不能将伪元素放入输入元素中,但可以放入影子元素,例如占位符!
要使其在其他浏览器中工作,请使用
:-moz-placeholder
、::-moz-placeholder
和:-ms-input-placeholder
在不同的选择器中。无法对选择器进行分组,因为如果浏览器无法识别选择器,则整个语句无效。更新:以上代码仅适用于 CSS 预处理器(SASS、LESS...),无需使用预处理器:
You can't put a pseudo element in an input element, but can put in shadow element, like a placeholder!
To make it work in other browsers, use
:-moz-placeholder
,::-moz-placeholder
and:-ms-input-placeholder
in different selectors. Can't group the selectors, because if a browser doesn't recognize the selector invalidates the entire statement.UPDATE: The above code works only with CSS pre-processor (SASS, LESS...), without pre-processors use:
纯 CSS 中的一个有效解决方案:
技巧是假设文本字段后面有一个 dom 元素。
A working solution in pure CSS:
The trick is to suppose there's a dom element after the text-field.
(*) Limited solution, though:
But in most cases, we know our code so this solution seems efficient and 100% CSS and 0% jQuery.
我发现这篇文章是因为我遇到了同样的问题,这是对我有用的解决方案。与替换输入的值相反,只需将其删除并绝对在其后面放置一个相同大小的跨度,该跨度可以使用您选择的图标字体应用一个
:before
伪类。I found this post as I was having the same issue, this was the solution that worked for me. As opposed to replacing the input's value just remove it and absolutely position a span behind it that is the same size, the span can have a
:before
pseudo class applied to it with the icon font of your choice.问题提到“输入字段”。虽然我相信 OP 指的是 type=text 的输入字段,但
::after
和::before
伪内容确实会呈现几种不同类型的输入字段:这是一个全面演示所有输入类型,一目了然显示哪些与(在本例中)
::before
伪元素兼容。总而言之,这是可以呈现伪内容的所有输入类型的列表:
The question mentions "input field". Although I believe the OP was referring to input field with type=text,
::after
and::before
pseudocontent does render for several different types of input fields:Here is a comprehensive demo of all input types, clearly showing which ones are compatible with (in this case) the
::before
pseudoelement.To summarize, this is a list of all of the input types that can render pseudocontent:
正如其他人所解释的,
input
是一种替换的 void 元素,因此大多数浏览器不允许您生成::before
或::after
其中的伪元素。但是,CSS 工作组正在考虑明确允许
::before
和::after
,以防input
具有appearance: none< /代码>。
来自 https://lists.w3.org/Archives/Public/ www-style/2016Mar/0190.html,
As others explained,
input
s are kinda-replaced void elements, so most browsers won't allow you to generate::before
nor::after
pseudo-elements in them.However, the CSS Working Group is considering explicitly allowing
::before
and::after
in case theinput
hasappearance: none
.From https://lists.w3.org/Archives/Public/www-style/2016Mar/0190.html,
根据 CSS 2.1 规范中的注释,该规范“不完全定义 :before 和 :after 与替换元素(例如 HTML 中的 IMG)的交互。这将在未来的规范中进行更详细的定义。”虽然
input
不再是真正的替换元素,但基本情况没有改变::before
和:after
对它的影响未指定,通常没有效果。解决方案是找到一种不同的方法来解决您试图以这种方式解决的问题。将生成的内容放入文本输入控件中会产生很大的误导:对于用户来说,它似乎是控件中初始值的一部分,但无法修改 - 因此它看起来像是在开始时强制执行的内容。控制,但它不会作为表单数据的一部分提交。
According to a note in the CSS 2.1 spec, the specification “does not fully define the interaction of :before and :after with replaced elements (such as IMG in HTML). This will be defined in more detail in a future specification.” Although
input
is not really a replaced element any more, the basic situation has not changed: the effect of:before
and:after
on it in unspecified and generally has no effect.The solution is to find a different approach to the problem you are trying to address this way. Putting generated content into a text input control would be very misleading: to the user, it would appear to be part of the initial value in the control, but it cannot be modified – so it would appear to be something forced at the start of the control, but yet it would not be submitted as part of form data.
尝试下一步:
try next:
您必须在输入周围有某种包装器才能使用 before 或 after 伪元素。这是一个小提琴,它在输入的包装 div 上有一个 before,然后将 before 放在输入内部 - 或者至少看起来像这样。显然,这是一种解决方法,但在紧要关头很有效,并且有助于做出响应。如果您需要添加一些其他内容,您可以轻松地将其设为 after。
工作小提琴
输入中的美元符号作为伪元素:http ://jsfiddle.net/kapunahele/ose4r8uj/1/
HTML:
CSS:
You have to have some kind of wrapper around the input to use a before or after pseudo-element. Here's a fiddle that has a before on the wrapper div of an input and then places the before inside the input - or at least it looks like it. Obviously, this is a work around but effective in a pinch and lends itself to being responsive. You can easily make this an after if you need to put some other content.
Working Fiddle
Dollar sign inside an input as a pseudo-element: http://jsfiddle.net/kapunahele/ose4r8uj/1/
The HTML:
The CSS:
如果您尝试使用 :before 和 :after 设置输入元素的样式,那么您很可能是在尝试模仿 CSS 堆栈中其他 span、div 甚至 a 元素的效果。
正如 Robert Koritnik 的答案指出, :before 和 :after 只能应用于容器元素,而输入元素不是容器。
然而,HTML 5 引入了 button 元素,它是一个容器,其行为类似于 input[type="submit|reset"] 元素。
If you are trying to style an input element with :before and :after, odds are you are trying to mimic the effects of other span, div, or even a elements in your CSS stack.
As Robert Koritnik's answer points out, :before and :after can only be applied to container elements and input elements are not containers.
HOWEVER, HTML 5 introduced the button element which is a container and behaves like an input[type="submit|reset"] element.
:before
和:after
仅适用于可以具有子节点的节点,因为它们插入新节点作为第一个或最后一个节点。:before
and:after
only works for nodes that can have child nodes since they insert a new node as the first or last node.虽然指出 Firefox 不允许无法显示任何内容的元素使用 ::after 和 ::before 内容的行为的解释是非常正确的,但它似乎仍然可以完美地使用此规则
:重新设置复选框或单选框的样式而不引入更多不相关的标记(例如尾随范围或标签)的唯一方法,我认为强制 Firefox 允许显示 ::before 和 ::after 内容是可以的,尽管不符合规格。
While the explanations that point out that the Firefox behavior of not allowing ::after and ::before content for elements that can't display any content are quite correct, it still seems to work perfectly fine with this rule:
As ::after is the only way to restyle a checkbox or radiobox without introducing more and unrelated markup like a trailing span or label, I think it's fine to force Firefox to allow ::before and ::after content to be displayed, despite not being to spec.
我发现你可以这样做:
您需要有一个 div 父级来接受填充和 :after。
第一个父级需要是相对的,第二个 div 应该是绝对的,这样您就可以设置后面的位置。
I found that you can do it like this:
You need to have a div parent that takes the padding and the :after.
The first parent needs to be relative and the second div should be absolute so you can set the position of the after.
摘要
它不适用于
,但可以与
< 配合使用。 /代码>。
小提琴:http://jsfiddle.net/gb2wY/50/
HTML:
CSS:
Summary
It does not work with
<input type="button">
, but it works fine with<input type="checkbox">
.Fiddle: http://jsfiddle.net/gb2wY/50/
HTML:
CSS:
带有 after 和 before 的切换器示例
只需将您的输入包装在 div 块上
Example of switcher with after and before
just wrap your input on div block