4

I have the following string and I want to ignore just strong tag from the string using css selector:

<p><strong>Local:</strong><br>
-Brasília/DF </p>

I tried the following syntax but it doesn't work.

p:not(strong)

Where am I wrong?

Rob
  • 14,746
  • 28
  • 47
  • 65
Mayur Koshti
  • 1,794
  • 15
  • 20

3 Answers3

5

A pseudo-class, attached to an element p:not(strong), selects from those elements to which it is 'attached' (here the p); and a <p> element is always not a <strong> element; therefore this selector will always match every <p> element.

You seem to be trying to style the parent <p> element based on its child <strong> element which cannot work, as CSS has no parent-selector (see: "Is there a CSS parent selector?")

You could, instead, add a class (or other attribute) to the <p> element, and use that in the selector:

<p class="hasStrongDescendant"><strong><strong>Local:</strong><br>
-Brasília/DF </p>

And style with:

p:not(.hasStrongDescendant) {
    /* CSS here */
}

p:not(.hasStrongDescendant) {
  color: orange;
}
<p>A paragraph with no child elements</p>

<p class="hasStrongDescendant"><strong>Local:</strong>
  <br>-Brasília/DF</p>

Or, using a data-* attribute:

<p data-hasChild="strong"><strong>Local:</strong><br>
-Brasília/DF </p>

And style with:

p:not([data-hasChild="strong"]) {
    /* CSS here */
}

p:not([data-hasChild="strong"]) {
  color: #f90;
}
<p>A paragraph with no child elements</p>

<p data-hasChild="strong"><strong>Local:</strong>
  <br>-Brasília/DF</p>

Also, if you wrapped the contents of the <p>, following the <strong>, inside their own element you could style the descendants of the paragraph using the negation selector:

<p>A paragraph with no child elements</p>

<p><strong>Local:</strong>
  <br><span>-Brasília/DF</span>
</p>

Styling with:

p :not(strong) {
  /* note the space between the
     'p' and the ':not()' */
  color: #f90;
}

p :not(strong) {
  color: #f90;
}
<p>A paragraph with no child elements</p>

<p><strong>Local:</strong>
  <br><span>-Brasília/DF</span>
</p>

Two further approaches, assuming that you want to style the text of the child outside of the <strong> element is (the simplest):

/* define a colour for the <p>
   elements: */
p {
  color: #f90;
}

/* define a colour for the <strong>
   elements within <p> elements: */    
p strong {
  color: #000;
}

p {
  color: #f90;
}
p strong {
  color: #000;
}
<p>A paragraph with no child elements</p>

<p><strong>Local:</strong>
  <br>-Brasília/DF</p>

And a slightly more complex version, using CSS generated content:

p {
  color: #f90;
}
p[data-label]::before {
  content: attr(data-label) ': ';
  color: #000;
  display: block;
  font-weight: bold;
}
<p data-label="Local">-Brasília/DF</p>

References:

Community
  • 1
  • 1
David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • No problem at all :) – David Thomas Jun 21 '16 at 10:26
  • I haven't seen a more contrived workaround than that data-attribute. It's one of those "if it works, it works" things... – BoltClock Jun 21 '16 at 10:30
  • @BoltClock: yeah; it's *really* not pretty but the point was to try and show an alternative form of selector being used with `:not()`, rather than looking nice as such; the only way I'd personally attempt the above is the use of `p :not(...)` though it does require additional elements. Having said that, I'd also be tempted to style the whole `

    ` and move the 'label' ('Local') into a data-attribute and then use pseudo-elements instead.

    – David Thomas Jun 21 '16 at 10:36
3

That is not how :not() pseudo-class works. It matches an element that is not represented by the argument.

So in this case to select text of p but not the strong tag you can wrap other part of text in span and then select all children of p but not strong like this

p :not(strong) {
  color: blue;
}
<p><strong>Local:</strong><br>
<span>-Brasília/DF </span></p>

Other option is to simply select text of p and then overwrite strong tag with default style.

p {
  color: blue;
}
p strong {
  color: black;
}
<p><strong>Local:</strong>
  <br>-Brasília/DF</p>
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

Anything but the strong tag will use CSS. Like:

.just_this :not(strong){
  color:blue;
  }
<p class="just_this">
  <strong>Local:</strong>
  <br /><span>-Brasília/DF</span>
</p>

Alternative(just use the child-combinator '>'):

.just_this{
  color:blue;
}

.just_this>strong{
  color:black;  
}
<p class="just_this">
  <strong>Local:</strong>
  <br />-Brasília/DF
</p>
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • 2
    "Something like this" What is this? The "just_this" class has no relation to the strong whatsoever. Why would you have a negation with the class on the strong in the first place? – BoltClock Jun 21 '16 at 10:31