1

Html :

<div class="main main-280">

"main-280" is dynamically generated and may change so css for that selector alone would be:

div[class^="main-"]{-------}

But how do I target both classes as in .main.main-280 using an attribute selector? It seems like this wouldn't work:

.main.div[class^="main-"]{-------}
1
  • Hi Matt, I put up an answer which suggested using these two selector combinations: div[class~="main"][class*=" main-"], div[class~="main"][class^="main-"] It seemed to work for me but the answer has been downvoted so I've removed it. I'd be interested in you trying out those two and tell me whether it helps or not as I can't see what's wrong with it. Thanks. Commented Sep 2, 2022 at 18:32

1 Answer 1

2

Not sure I know what you exactly mean, but:

const main = document.querySelector('.main');

document.querySelector('button').addEventListener('click',  () => main.classList.toggle('main-280'));
body {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
  min-height: 100vh;
}

div[class^="main"][att="alwaysVisible"] {
  height: 150px;
  width: 150px;
  background-color: royalblue;
  border-radius: 8px;
}

.main.main-280 {
  box-shadow: 4px 8px 20px rgba(0,0,0,.5);
}
<div class="main main-280" att="alwaysVisible"></div>
<button>toggle class main-280</button>

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

1 Comment

I have upvoted it as I think that it answers the question (as it can be understood)

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.