0

I want to style all the input and select from inside a certain div in a certain manner. I know I can do (1)

#divNew select {
    display:table-cell;
    margin-left:1em;
}
#divNew input {
    display:table-cell;
    margin-left:1em;
}

and I can also do (2)

#divNew select,#divNew input {
    display:table-cell;
    margin-left:1em;
}

and I know I can't do (3)

#divNew select,input {
    display:table-cell;
    margin-left:1em;
}

because (3) applies this style to all input, inside and outside divNew.

My question is: is this all that is to it? I prefer the shorter option (2), but I feel that the redundant #divNew should be eliminated. Can it become even shorter than (2)?

9
  • 1
    why 2 is ugly? if you are looking just from the point of readability, why not use css preprocessors? Commented Sep 5, 2019 at 0:30
  • Option 2 is more common over option 1, though typically with each element on its own line (i.e. a hard return after each comma - though this is convention rather than a standard). Though if you want to find more elegant and maintainable stylesheets, maybe try looking into sass Commented Sep 5, 2019 at 0:30
  • Please share html as well Commented Sep 5, 2019 at 0:30
  • @karthick Because it adds a redundant #divNew. And I'm not into so big projects as to need another layer of complexity. Commented Sep 5, 2019 at 2:19
  • 2
    @NagaSaiA That's just a simple question about a trivial css structure (that I couldn't find, though, for the lack of the right keywords). Can't you just abstract the html? Even because the question has already been answered. Commented Sep 5, 2019 at 2:23

1 Answer 1

2

Your option #2 is the proper way to share declarations amongst a group of selectors. Container nesting like your #3 is not supported, but something similar has been proposed to the W3C.

You might find CSS preprocessing tools like sass or less helpful to reduce the boilerplate, at the cost of adding a step to your build pipeline. Though most frontend frameworks nowadays already include a CSS preprocessor like these natively.

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

3 Comments

the proposal is accepted and there is already a first draft: drafts.csswg.org/css-nesting-1
But it's nothing like #3 though. What's closer to #3 is the :is() pseudo. That also appeared in its original form years ago. And it's still not available cross-browser yet.
@BoltClock I forgot about :is(), which is a great answer and should probably be accepted.

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.