Succinct way of specifying two or more values for an attribute in css selector

后端 未结 2 1362
囚心锁ツ
囚心锁ツ 2021-01-23 14:31

I have a CSS selector as follows:

#foo input[type=number].form-control, #foo input[type=text].form-control
相关标签:
2条回答
  • 2021-01-23 14:43

    No, I'm afraid there isn't. While there are no proposals for enumerated values in attribute selectors like the one in your example, Selectors 4's :matches() will at least allow you to OR two attribute selectors in a single compound selector:

    #foo input:matches([type=number], [type=text]).form-control
    

    That said, you will probably have to stick with the current, verbose syntax for the foreseeable future, until browsers start implementing :matches().

    0 讨论(0)
  • 2021-01-23 15:00

    No not in plain ol' CSS, but with a pre-processor like LESS or SASS you can nest attributes.

    #foo {
        input.form-control {
            &[type=number],
            &[type=text] {
                /* your css styles here */
            }
        }
    }
    

    It's prehaps not 'succinct', in that the end result is longer than your desired outcome, but it is somewhat easier to read, and may cut down some repetition in your stylesheet.

    0 讨论(0)
提交回复
热议问题