0

I am working on a project where i am using an internal UI Library. In one of the CSS file, i saw something which looks strange. To simplify things, I am reciprocating with basic html elements and corresponding css:

CSS:

div{
  border:1px solid black;
  width:100px;
  height:100px;
}
.parent
//some comment exists here
.child{
  background-color: red;
}
.anotherdiv{
  background-color: blue;  
}

and HTML :

<div class='parent same'>Parent
    <div class='child'>Child</div>
</div>
<div class='anotherdiv'></div>

When i tried to check above thing on firefox, i got below warning under CSS console.

Dangling combinator: Ruleset ignored due to bad selector

I tried to approach the problem backwards, i.e. for the given CSS:

.parent
//some comment exists here
.child{
    background-color:red;
}

I thought above will resolve either to

.parent .child{
  background-color:red;          //applied on inside level element
}

or,

.parent.child{
  background-color:red;         //applied on same element
}

But either of them is not applied.

And, ruleset defined for div with class anotherdiv is working perfectly fine. I want to know how browser reads CSS, and upon reaching some crooked line, how it resolves and how following rulesets in the CSS gets effected.

UPDATE

I cross checked the type of file and it came out as .SCSS and below is what i found strange

.accordion-toggle 
 // Inner needs the styles because you can't animate properly with any styles on the element
 .accordion-inner {
      padding: 9px 15px;
      border-top: 1px solid #e5e5e5;
  }

Sorry for the previous misunderstanding!

Rakesh_Kumar
  • 1,442
  • 1
  • 14
  • 30
  • 6
    `//` is not a comment in CSS so you might as well have written `.parent .child` – PeeHaa Jun 18 '15 at 10:06
  • That is invalid in CSS, but it will work (with a little modification) in some CSS preprocessor like SASS or LESS. – Vucko Jun 18 '15 at 10:07
  • @PeeHaa Along with `/*....*/`, I probably have seen `//` to be used as specifying comments. If not, then what are its possible usage? – Rakesh_Kumar Jun 18 '15 at 10:12
  • 1
    There is no usage in CSS for that. If you think you have ever seen in it was in error (most likely because some languages do use it) – PeeHaa Jun 18 '15 at 10:12
  • Yes, it will work in many languages, but not in CSS. Personally I never use it, because some files are automatically compressed on server, than the code won't work (imagine what happens if you remove all line-breaks). – skobaljic Jun 18 '15 at 10:13
  • Make sure all hidden characters are removed from the css file: http://stackoverflow.com/questions/17942359/only-first-media-query-working – Mathijs Rutgers Jun 18 '15 at 10:16
  • 1
    @Rakesh_Kumar [SASS](http://sass-lang.com/documentation/file.SASS_REFERENCE.html#comments) and [LESS](http://lesscss.org/features/#features-overview-feature-comments) use those comments. – Vucko Jun 18 '15 at 10:21
  • @PeeHaa Ok..so as you said there is no usage of `//` in CSS, thus there is some problem with internal library. But would you like to elaborate on second part of my question i.e. how browser handles those situation when some garbled character appears, and how following CSS ruleset evaluation done. – Rakesh_Kumar Jun 18 '15 at 10:23
  • In your example there is no next ruleset. So I really don't get what you mean / are asking. If you have some other ruleset after your code it would just be interpreted as normal – PeeHaa Jun 18 '15 at 10:31
  • @PeeHaa This is my followed next ruleset.. `.anotherdiv{ background-color: blue; }` which works perfectly fine. – Rakesh_Kumar Jun 18 '15 at 10:36

2 Answers2

2

Assuming the "comment" you speak of literally begins with // in your source file, in which case it is not a proper CSS comment but simply garbage (as the forward slash is not part of a CSS identifier or any valid CSS selector syntax), this causes a parse error.

The following stream of characters:

.parent
//some comment exists here
.child{
    background-color:red;
}

Is treated as .parent, followed by garbage, followed by a declaration block denoted by curly braces. Everything up to and including the } is discarded and the parser resumes from that point, continuing as though this stream of characters it just ignored was never there. From section 4.1.7 of CSS2.1:

The selector (see also the section on selectors) consists of everything up to (but not including) the first left curly brace ({). A selector always goes together with a declaration block. When a user agent cannot parse the selector (i.e., it is not valid CSS 2.1), it must ignore the selector and the following declaration block (if any) as well.

Now, when the parser sees the following rule:

.anotherdiv{
  background-color: blue;  
}

It is able to read and apply this rule because as far as the parser is concerned, the previous rule was your div rule at the very beginning of your snippet.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Nicely explained! Thanks a lot.. :) – Rakesh_Kumar Jun 18 '15 at 10:43
  • Big mistake from my side. I just cross checked again for file type and its defined type is SCSS. How this would changes the aspects..? – Rakesh_Kumar Jun 18 '15 at 11:01
  • @Rakesh_Kumar: Completely. SCSS is not meant to be served directly to the browser - it is meant to be compiled into a CSS file that is served to the browser. – BoltClock Jun 18 '15 at 11:02
  • I am not aware but after compilation of `.accordion-toggle // Inner needs the styles because you can't animate properly with any styles on the element .accordion-inner { padding: 9px 15px; border-top: 1px solid #e5e5e5; }`, what it finally becomes? Is it simply `.accordion-toggle.accordion-inner{...}` – Rakesh_Kumar Jun 18 '15 at 11:06
  • 1
    @Rakesh_Kumar: That is a separate question entirely. You can very easily test this by going to http://sassmeister.com however. – BoltClock Jun 18 '15 at 11:14
1

The CSS rule set or rule in your case is:

.parent
//some comment exists here
.child{
    background-color: red;
}

It consists of selector and declaration block. The selector points to the HTML element you want to style:

The selector consists of everything up to (but not including) the first left curly bracket*

Since for CSS comments we know following:

The /* */ comment syntax is used for both single and multi line comments. There is no other way to specify comments in external stylesheets*.

Than your selector is parsed as:

.parent //some comment exists here .child

Which is an invalid selector and it outputs the error.

As the selector is bad, than browser cannot evaluate a selector against element nodes and whole rule is ignored. Browser will never try to fix the CSS rule, because:

  • it may make more damage to layout.
  • the rule may be correct, but current browser does not recognize it

Note: Some bad selectors can break the CSS structure, so all the rules that come after will be ignored.

read more...

Community
  • 1
  • 1
skobaljic
  • 9,379
  • 1
  • 25
  • 51