2

Say there are selectors in a stylesheet that have no style info in them, so they are effectively empty (have no style declarations):

.main-menu {}

Will the browser still search for them?

My gut feeling is that it will depend on the browser, so an 'intelligent' programmer would say 'if selector empty don't bother' but not all browsers will have this kind of enlightened implementation. Had a quick search and couldn't find anything, was wondering if anyone on here knew anything regarding this...

Best practice i'm sure is to not have selectors with empty declarations, as they are a waste of space and time, does W3 say anything about this?

Thanks!

user1360809
  • 725
  • 2
  • 13
  • 24
  • Bigger is the file, bigger will be the download time. Don't forget that your css file will be download by the user browser. You should delete this selector – Tony Feb 17 '15 at 09:46
  • 2
    W3C doesn't say anything about this mostly because it's not the spec's job to describe implementation details. As far as the spec is concerned this is just an empty rule with no style information. Also, it's a rule, not a selector - a rule that contains an empty selector, or rather, *no* selector, would be `{}` which is invalid. – BoltClock Feb 17 '15 at 09:48
  • the speed in which the css is parsed would be impacted more if your empty selectors match elements in your dom. if they don't, then the impact (other then network time) would be minimal. (Assuming you don't have thousands of empty selectors) – atmd Feb 17 '15 at 09:50
  • @BoltClock Having empty rules declaration is definitely not invalid. https://developer.mozilla.org/en-US/docs/Web/CSS/Syntax#CSS_declarations_blocks – Vishwanath Feb 17 '15 at 09:56
  • @Vishwanath: I never said an empty rule is invalid. If you want to be really precise, a style rule can contain an empty declaration block, but not an empty selector. Because an empty selector is invalid. The OP is confusing the term selector with the others. – BoltClock Feb 17 '15 at 09:58
  • @BoltClock Sorry. I misread it. Since question had a selector mentioned in it. I thought u were talking about that... – Vishwanath Feb 17 '15 at 09:59
  • @BoltClock Yeah. Empty CSS rules declaration. – Vishwanath Feb 17 '15 at 10:01
  • Yep, sorry for confusion: I read it that a rule set consists of a selector (eg .class) and declaration ( anything within {} ). There was an incorrect statement in my title - my most humble apologies! Have corrected this in the title.... – user1360809 Feb 17 '15 at 10:13

1 Answers1

4

As a matter of fact, having empty CSS rules can actually serve to work around some bugs in certain browsers, and cause bugs in others. So there is evidence to suggest that, for at least two independent implementations anyway, the parser does not outright ignore CSS rules with empty declaration blocks.

As for best practice? Leave them out as all they would normally do is take up unnecessary bytes, and if you have a very good reason to use them, it's not a bad idea to add a comment explaining their purpose.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • I like this answer...and like others have said its just wasted bytes...but I was looking to see if there would be a performance impact other than network transfer of the file itself, as it would waste time searching for something that will make no difference to the page... – user1360809 Feb 17 '15 at 10:17
  • Thanks for the links too...might come in handy but unless I am stick in the situation mentioned I would currently say best leave it out... – user1360809 Feb 17 '15 at 10:19
  • @user1360809: Well, considering that it can influence *bugs*, I'd probably just throw performance out of the equation altogether. It depends on whether you prioritize bugs or performance. – BoltClock Feb 17 '15 at 10:23
  • True, but my current policy is not to add unless absolutely necessary, let the browser do its thing and see what comes out...and bugs can only be temporary for example - removed with updates etc (a very depressing thought!)...so in short less is more for me in this instance, unless you need to fix a bug...at which point suicide or change of career can be a good option ;) – user1360809 Feb 17 '15 at 10:32