I'm working with an HTML piece that I cannot modify. One of the ids in the document is:
<div id="a>span.tex"> ... </div>
This is perfectly valid HTML5 syntax for ids, however, it is not possible to select this id in CSS without having troubles with >
and .
characters.
How can I select this id in CSS?
The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.
The selector must start with a pound sign ( # ) and then the value of the id attribute. The browser will then look for a tag in the page that has an id attribute equal to that id.
IDs have more specificity then classes, but inline styles have more than IDs. Inline styles have more specificity than external style sheets. By using classes you standardize how styles are applied to your elements making things more predictable.
ID's are more specific than classes and take precedence over them. This is because ID's have to be UNIQUE on every page...so they are by nature very specific. Classes can appear multiple times. Learning how this works is fundamental to coding CSS.
You can escape special characters with a backslash:
#a\>span\.tex {
color: red;
}
<div id="a>span.tex"> Some funky ID </div>
You can even use backslashes within your ID as long as you escape them with another backslash:
#a\\span\\tex {
color: red;
}
<div id="a\span\tex"> Some funky ID </div>
In fact, lots of crazy strings are valid IDs in HTML5
#\¯\\\_\(\ツ\)\_\/\¯ {
color: red;
}
<div id="¯\_(ツ)_/¯"> ¯\_(ツ)_/¯ - but why would you? </div>
use this:
div[id="a>span.tex"] {
style here;
}
this will select div with id equal to your id
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With