0

I have the following CSS selector:

#AllContextMenus :not(.menu-iconic-left):not(.menu-accel):not(.menu-accel-left):not(.menu-accel-container):not(.menu-accel-container-left):not(.menu-iconic-accel):not(.menu-right)::before

For readability purposes, I like to keep all code lines under 100 characters.

Is there any way to simplify, optimize, or write this CSS selector without changing what it matches and without reducing performance?

For example, is there any type of "and" operator that can be used within :not()?

4
  • Can you apply a style to #AllContextMenus and then override it for the specific menu types? Commented Jun 10, 2020 at 3:10
  • 1
    Need more context. Commented Jun 10, 2020 at 3:13
  • Context is irrelevant (I can post the thousands of lines of relevant code related to this selector, but no one will actually read it, and others will undoubtedly complain). I'm only asking about refining this selector without changing what it matches... nothing else. Commented Jun 10, 2020 at 9:15
  • @NiettheDarkAbsol Thanks for your suggestion. Your idea is basically what I've done. #AllContextMenus has a style, but specific sub-elements all need the same modifications to the style. Commented Jun 10, 2020 at 9:25

1 Answer 1

2

You generally can't simplify a selector without changing the semantics of what it matches.

But you can break a selector up into multiple lines at many points to meet maximum line length requirements. Just use a comment and put the line break inside the comment. Like this:

#AllContextMenus :not(.menu-iconic-left)/*
*/:not(.menu-accel)/*
*/:not(.menu-accel-left)/*
*/:not(.menu-accel-container)/*
*/:not(.menu-accel-container-left)/*
*/:not(.menu-iconic-accel)/*
*/:not(.menu-right)::before

#AllContextMenus :not(.menu-iconic-left)/*
*/:not(.menu-accel)/*
*/:not(.menu-accel-left)/*
*/:not(.menu-accel-container)/*
*/:not(.menu-accel-container-left)/*
*/:not(.menu-iconic-accel)/*
*/:not(.menu-right)::before {
  color:red;
  content:'TEST '
}
  <section id="AllContextMenus">
    <div class="a">A</div>
    <div class="menu-iconic-accel">menu-iconic-accel</div>
  </section> 

Sign up to request clarification or add additional context in comments.

4 Comments

That's a clever trick using line breaks inside comments. My thanks and upvote!
Using your technique, besides possibly adding spaces before each */ is there any way to make the code more readable?
@RockPaperLizard based on your HTML maybe you can have this; jsfiddle.net/erf10cqo if you want to target all the menu-* classes
@TemaniAfif Thank you for your suggestion. I like your idea. I need to go through many thousands of lines of code (most of which I didn't write) to see if something like that is a possibility. I'm going to spend a little time thinking of a good way to automate that process, because doing it manually would be very time-intensive.

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.