1

Is there any difference between both of them?

Example 1:

a>b{
  display: block;
}

Example 2:

a > b{
  display: block;
}
BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
Exil
  • 424
  • 11
  • 25

4 Answers4

2

There is no such difference. See the snippet below for demo:

.a>.b{
  color: red;
}

.c > .d{
  color: red;
}
<div class="a">
  Class a
  <div class="b">
    Class b
  </div>
</div>

<div class="c">
  Class c
  <div class="d">
    Class d
  </div>
</div>
Nisarg Shah
  • 13,626
  • 5
  • 36
  • 53
2

CSS is very forgiving. The CSS selectors specification mentiones that whitespaces around combinators (like your > here) are optional:

The following selector represents a p element that is child of body:

body > p

The following example combines descendant combinators and child combinators.

div ol>li p

It represents a p element that is a descendant of an li element; the li element must be the child of an ol element; the ol element must be a descendant of a div. Notice that the optional white space around the ">" combinator has been left out.

Section 8.2 of the CSS Selectors Level 3 recommendation

To further back this up, the specification's Grammar section makes this really apparent with an implementation approach:

combinator
  /* combinators can be surrounded by whitespace */
  : S+ | S* [ '>' | '+' | '~' | COLUMN | '/' IDENT '/' ] S*
  ;

Section 10 of the CSS Selectors Level 3 recommendation

For this reason, the following are all valid as CSS parsers should simply strip the spaces out:

a>b {}
a > b {}
a> b {}
a >b {}
a     >    b {}

So to answer your question: no, there is no difference.

As for which one you should use, however: that's purely a question of personal preference. For me, I'd opt for a > b, simply because I feel it makes it easier to read, but if you want to type a>b or even a > b it's entirely up to you - although anyone who has to read your code will probably not be your number 1 fan with the latter approach!

James Donnelly
  • 122,518
  • 33
  • 200
  • 204
  • See also: https://stackoverflow.com/questions/23581885/css-vs https://stackoverflow.com/questions/39091583/whitespace-in-css-selectors (and, albeit not related to combinators, https://stackoverflow.com/questions/5930898/what-are-the-rules-around-whitespace-in-attribute-selectors). – BoltClock Nov 02 '17 at 17:56
0

First of all there is no effect space between or not, both will work.

Example will work with following DOM structure:

<a><b></b></a>
Hanif
  • 3,609
  • 1
  • 10
  • 17
0

There is no difference between the two.

DavSev
  • 1,155
  • 3
  • 19
  • 40