Can I target all tags with a single selector?

后端 未结 10 1681
予麋鹿
予麋鹿 2020-12-04 08:44

I\'d like to target all h tags on a page. I know you can do it this way...

h1,
h2,
h3,
h4,
h5,
h6 {
  font: 32px/42px trajan-pro-1,trajan-pro-2;
}

10条回答
  •  暖寄归人
    2020-12-04 09:14

    It's not basic css, but if you're using LESS (http://lesscss.org), you can do this using recursion:

    .hClass (@index) when (@index > 0) {
        h@{index} {
            font: 32px/42px trajan-pro-1,trajan-pro-2;
        }
        .hClass(@index - 1);
    }
    .hClass(6);
    

    Sass (http://sass-lang.com) will allow you to manage this, but won't allow recursion; they have @for syntax for these instances:

    @for $index from 1 through 6 {
      h#{$index}{
        font: 32px/42px trajan-pro-1,trajan-pro-2;
      }
    }
    

    If you're not using a dynamic language that compiles to CSS like LESS or Sass, you should definitely check out one of these options. They can really simplify and make more dynamic your CSS development.

提交回复
热议问题