Problems with css :not() attribute

Asked

Viewed 93 times

1

I’m having a problem I’m editing a h2with css and within that h2 has a span but wanted to apply the style in h2but I don’t want style to apply to my span I’m using the selector :not(span) but it doesn’t work follow my code:

html

<div class="text-center box-product band-product">
                        <h2>Quero<br>anunciar meu<br><span>produto</span></h2>
                        <a href="#" title="Saiba mais">Saiba mais <i class="fas fa-arrow-right"></i></a>
                    </div>

SCSS:

.box-product{
        background-color: $light-gray;
        h2:not(span){
            line-height: 30px;
            @include gradient-orange(#f97c16, #feb226);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            span{
                color: $gray;
                font-weight: normal;
                font-family: "good_vibes", sans-serif;
                font-size: 2em;
                text-transform: capitalize;
            }
        }
    }

notice that the color I’m applying to h2 is applying in my span tbm do not want it as I can resolve follow the image inserir a descrição da imagem aqui

1 answer

1

Dude I think you just ended up complicating things with the not() needlessly.

See in the example below how you can fix the problem just by coming back with the color of the text on span thus -webkit-text-fill-color: gray;

.box-product{
    background-color: #000;
}
.box-product h2 {
    line-height: 30px;
    background-image: linear-gradient(#f97c16, #feb226);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}
.box-product h2 > span {
    color:gray;
    font-weight: normal;
    font-family: "good_vibes", sans-serif;
    font-size: 2em;
    text-transform: capitalize;
    -webkit-text-fill-color: gray;
    background-image: linear-gradient(gray, gray);
    -webkit-background-clip: text;
}
<div class="text-center box-product band-product">
    <h2>Quero<br>anunciar meu<br><span>produto</span></h2>
    <a href="#" title="Saiba mais">Saiba mais <i class="fas fa-arrow-right"></i></a>
</div>


I’ll leave an example here in case you want to better understand the selector :not().

In the example below I have a tag <h1> and inside it I have two more tags a <span> and a <label>, ai I used not dial to color everything inside the h1, except the span.

Notice that what’s inside the h1 is the son of h1, then use h1:not() means that "all the h1 not ( css rule)", Ex: if you do this h1:not(.tit) {color: red} all H1 turn red, except for the class .tit

Take the example:

body {color: green;}
h1 {color: blue;}
h1 > *:not(label) { color: red; }

/* todos P ficaram azul, menos o com a classe .tit*/
p:not(.tit) {
    color: aqua;
}
<h1>text H1 
    <span class="classico">tag span</span>
    <label>tag label</label>
</h1>
<p>Mais um texto<p>
<p class="tit">Mais um texto<p>
<p>Mais um texto<p>

  • then the problem of doing this is that besides repeating the code realize that in span has an orange borada of H2 well subted more to see so I believe that :not() would help me 100% understand that I am using wrong the attribute with scss has some way to do-that it works ?

  • 1

    @Kirito I’ll take a test and tell you

  • beauty thank you so much :)

  • @Kirito did some tests here I can even disregard the color in span, but when you use the background there is no way not to make it go behind the child too, unless the child has its own background. It’s like when you put opacity on the div and everything inside it also gets opacity... At the moment besides repeating the classes, including putting a background-image: linear-gradient(Gray, Gray); in span to remove the orange outline I can’t find another solution

  • Understood friend thank you very much if you can update the answer with the test you did by removing the orange borada thank you

  • @Kirito already edits, but actually only repeated the classes in span but changing the color to gray maybe it’s not the perfect solution, but I couldn’t think of anything better, mainly because the span is inside H1 and is "inheriting" some styles and bg.

  • @Kirito at the end of my reply I made another Edit, and includes a practical example and a few words on how to use not(), I think it can give you a light when it comes to nesting with SCSS

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.