Home > database >  Is there a way to specify multiple HTML elements inside a CSS class clause?
Is there a way to specify multiple HTML elements inside a CSS class clause?

Time:11-22

I want to alter multiple HTML elements that live inside a singular CSS class.

For example:

<div >
    <h1>Hello</h1>
    <p>Good Morning</p>
</div>

I know it is possible to do this:

.main h1 { ... }
.main p {...}

Is it possible to create a single CSS clause that does this in one line? (Like this:)

.main h1,p {...}

Thanks!

CodePudding user response:

You may use a scoped universal selector, as a shortcut for every element inside .main, e.g.

.main * { ... }

or list all the specific elements inside .main using :is or :where (the difference is in their different specificity), e.g.

.main :where(h1, p) { ... }

Reference (MDN):
https://developer.mozilla.org/en-US/docs/Web/CSS/:is
https://developer.mozilla.org/en-US/docs/Web/CSS/:where

CodePudding user response:

Try using

.main h1,
.main p {...}
  • Related