0

HTML:

<h2 class="title-main">
     <span class="title-inside">A</span>
     <span class="title-inside">B</span>
     <span class="title-inside">C</span>
     <span class="title-inside">D</span>
     <span class="title-inside">E</span>
     <span class="title-outside">F</span>
     <span class="title-outside">G</span>
</h2>

CSS:

:host/deep/.title-main {
    display: flex;
    margin-bottom: 0;

    .title-inside {
        &:after {
            content : '-';
        }
    }
}

I am getting the output like this

A-B-C-D-E-F G

I don't want the - for the last child. I want the output like this

A-B-C-D-E F G

i tried many options. For eg. one of them which i tried

title-inside:last-child ::after {
           content: ''
    }

But this is not working. Could anyone please help me

1
  • I tried like this also. That is not working Commented Apr 24, 2017 at 10:58

2 Answers 2

2

use :not with :nth-last-of-type()

.title-main {
  display: flex;
  margin-bottom: 0;
}

.title-inside:not(:nth-last-of-type(3))::after {
  content: '-';
}
<h2 class="title-main">
  <span class="title-inside">A</span>
  <span class="title-inside">B</span>
  <span class="title-inside">C</span>
  <span class="title-inside">D</span>
  <span class="title-inside">E</span>
  <span class="title-outside">F</span>
  <span class="title-outside">G</span>
</h2>

Here is the SASS version

:host/deep/.title-main {
    display: flex;
    margin-bottom: 0;

    .title-inside {
        &:not(:nth-last-of-type(3)){
           &::after{
            content : '-';
           }
        }
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I tried ur approach. But no hypen is coming for other child also
I've just updated the SASS version, I've forgot the &::after take a look again
Thanks for your answer. But i dont know, in my case this is not working
I have updated my html. Could you please see that. I want that style to be applied to the last child of title-inside. @dippas
Ya it worked. I used :nth-child(5). I tried with urs also it is working fine. Thanks a lot
2

Since :nth-* and last-child selectors use the actual element type and not class name, I think it is better to use a class selector.

By using the ::before pseudo instead, combined with the sibling selector +, it does not matter how many items each class has, it will always count right.

.title-main {
  display: flex;
  margin-bottom: 0;
}

.title-main  .title-inside + .title-inside::before {
  content: '-';
}
<h2 class="title-main">
     <span class="title-inside">A</span>
     <span class="title-inside">B</span>
     <span class="title-inside">C</span>
     <span class="title-inside">D</span>
     <span class="title-inside">E</span>
     <span class="title-outside">F</span>
     <span class="title-outside">G</span>
</h2>


<h2 class="title-main">
     <span class="title-inside">A</span>
     <span class="title-inside">B</span>
     <span class="title-inside">C</span>
     <span class="title-outside">D</span>
     <span class="title-outside">E</span>
     <span class="title-outside">F</span>
     <span class="title-outside">G</span>
</h2>


SASS

:host/deep/.title-main {
    display: flex;
    margin-bottom: 0;

    .title-inside {
      &+.title-inside {
        &::before{
          content : '-';
        }
      }
    }
}

2 Comments

I dont want the hypen after E
@Priya Updated my answer with an explanation that might help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.