TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Don’t use IDs in CSS selectors? (2011)

131 pointsby outdooriconover 10 years ago

7 comments

_RPMover 10 years ago
[Slightly off topic] One of the reasons I wouldn't use ID's is because the DOM element with that ID is automatically introduced to the global scope. Open up the console and type "up_8630368" and you will see it yields a DOM Element.
评论 #8631084 未加载
评论 #8631104 未加载
评论 #8630975 未加载
评论 #8631606 未加载
评论 #8630990 未加载
评论 #8631490 未加载
dreamweaponover 10 years ago
This is rather like famous &quot;Go To Statement Considered Harmful&quot; dictum of lore, and its variants in other contexts: the key takeaway shouldn&#x27;t be that we should <i>never</i> use these constructs -- rather than we should <i>be circumspect</i> about using them, as they tend to have hidden side effects.
评论 #8631278 未加载
Joe8Bitover 10 years ago
I have to admit to not feeling too strongly either way about this, but the argument I would have against the &#x27;never use IDs in CSS selectors&#x27; is purely a semantic one.<p>An ID, by design, semantically references a single thing. This has important meaning when making HTML (amd CSS) readable and parseable. Now, I&#x27;m not necessarily saying this is a &#x27;winning&#x27; argument, but throwing away a useful tool for declaring semantic meaning for hand wave&#x27;y arguments about &#x27;britality&#x27; feels like overkill.
评论 #8631282 未加载
评论 #8631063 未加载
moron4hireover 10 years ago
I don&#x27;t think you can really make a blanket statement like this. When you make blanket statements, you&#x27;re ultimately basing a decision on superstition, rather than need or evidence. It&#x27;s why I don&#x27;t really care for anything that claims to be &quot;best practices&quot;. The term &quot;best practices&quot; really just means &quot;this worked for us in our situation&quot;. That&#x27;s great, and one should be aware of such things, but that doesn&#x27;t make it a standard or a law.
评论 #8631190 未加载
评论 #8631144 未加载
onion2kover 10 years ago
<i>the performance difference between classes and IDs is irrelevant</i><p>Instead of a test with a list of 1000 elements, try a test with a <i>nested</i> list of 1000 elements. The performance problem with classes is that the browser has to traverse the DOM to find them, starting from the deepest element and working up. It&#x27;s pretty quick, but not <i>that</i> quick when you have a deep tree. Whereas finding an element by ID is literally just a single lookup in an index regardless of where the element actually appears.<p>That said, DOM traversal was a very slow procedure for a long time, so browser developers spent a lot of time and energy optimising it. It&#x27;s way better than it was. It could well be the case that classes don&#x27;t have the performance problems they once did. Unfortunately, the test in this article won&#x27;t show whether that&#x27;s true or not.<p>To clarify, if you have a selector like &quot;div.class a&quot;, and HTML like;<p>&lt;div class=&quot;class&quot;&gt;&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;div&gt;&lt;a&gt;Woo!&lt;&#x2F;a&gt;&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;&lt;&#x2F;div&gt;<p>..then the browser goes to every anchor in the DOM and then looks up the tree asking &#x27;Does this div have a class of &#x27;class&#x27;?&quot;. That&#x27;s what&#x27;s slow. If you use &quot;div#id a&quot; then the browser can look for the id more quickly. That&#x27;s why IDs are faster.
评论 #8631266 未加载
评论 #8630807 未加载
评论 #8630961 未加载
评论 #8630531 未加载
评论 #8630981 未加载
spionover 10 years ago
Or simply don&#x27;t use CSS at all:<p><a href="https://speakerdeck.com/vjeux/react-css-in-js" rel="nofollow">https:&#x2F;&#x2F;speakerdeck.com&#x2F;vjeux&#x2F;react-css-in-js</a><p>That presentation goes into more depth describing problems with CSS. Given a virtual DOM in JS, the solution becomes simple and obvious: plain objects.<p>Its interesting how React completely reinvents &quot;best practices&quot;.
评论 #8633919 未加载
评论 #8631362 未加载
ad_hominemover 10 years ago
HN title should be &quot;Don&#x27;t use IDs in CSS selectors? [2011]&quot;<p>(missing question mark from original title and the year written)