Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select ids which contain special characters?

Tags:

html

css

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?

like image 443
jcrs Avatar asked Oct 17 '16 22:10

jcrs


People also ask

How do you select an element with a specific ID?

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.

Which is the correct selector for an ID?

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.

Are ID selector highly specific?

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.

Which is more specific ID or class?

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.


2 Answers

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>
like image 174
Turnip Avatar answered Sep 28 '22 01:09

Turnip


use this:

div[id="a>span.tex"] {
    style here;
}

this will select div with id equal to your id

like image 38
Abood Avatar answered Sep 28 '22 01:09

Abood