Handling a colon in an element ID in a CSS selecto

2019-01-02 20:38发布

This question already has an answer here:

JSF is setting the ID of an input field to search_form:expression. I need to specify some styling on that element, but that colon looks like the beginning of a pseudo-element to the browser so it gets marked invalid and ignored. Is there anyway to escape the colon or something?

input#search_form:expression {
  ///...
}

8条回答
栀子花@的思念
2楼-- · 2019-01-02 21:13

Backslash:

input#search_form\:expression {  ///...}
查看更多
千与千寻千般痛.
3楼-- · 2019-01-02 21:14

Using a backslash before the colon doesn't work in many versions of IE (particularly 6 and 7; possibly others).

A workaround is to use the hexadecimal code for the colon - which is \3A

example:

input#search_form\3A expression {  }

This works in all browsers: Including IE6+ (and possibly earlier?), Firefox, Chrome, Opera, etc. It's part of the CSS2 standard.

查看更多
登录 后发表回答