简体   繁体   English

CSS重复,有什么更有效的方法?

[英]css repetitive, what is more efficient?

How can I write this code more effectively? 如何更有效地编写此代码? I don´t want to write .btn:hover every time. 我不想每次都写.btn:hover

 .btn:hover .glyphicon-minus, .btn:hover .glyphicon-ok, .btn:hover .glyphicon-remove, .btn:hover .glyphicon-trash, .btn:hover .glyphicon-arrow-left, .btn:hover .glyphicon-arrow-right, .btn:hover .glyphicon-eye-open, .btn:hover .glyphicon-floppy-disk, .btn:hover .glyphicon-print .btn:hover .glyphicon-pencil{ color: black !important; } 

.btn:hover [class*='glyphicon-'] {...}

... matches all elements children of .btn:hover that contain glyphicon- inside their class attribute. ...匹配.btn:hover所有子元素,它们的class属性中包含glyphicon- If they are always immediate children, you should use the direct descendant operator: 如果它们始终是直系子代,则应使用直接后代运算符:

.btn:hover > [class*='glyphicon-'] {...}

... so it doesn't apply when .glyphicon-* is not immediate child of .btn . ...因此,当.glyphicon-*不是.btn直接子代时, .btn


Note: (@Paulie_D) In principle and for the general case, this is safer than using [class^="glyphicon-"] , because when the matched children have more than one class and the one being matched is not the first, the selector won't match. 注意: (@Paulie_D)原则上,对于一般情况,这比使用[class^="glyphicon-"]更安全,因为当匹配的子项有多个类且被匹配的子项不是第一个时,则选择器不匹配。 For example: 例如:

<a class="btn"><i class="glyphicon glyphicon-floppy-disk"></i>Old devices</a>

Note: (@GeomanYabes, on the suggestion to use SASS). 注意: (@ GeomanYabes,关于使用SASS的建议)。 Using SASS (or not) usually has to do with the stack of the team/company you work in, with personal choice as a developer or with project/client requirements rather than with writing less code in a particular case in a particular project (like the one above). 使用(或不使用)SASS通常与您工作的团队/公司的堆栈有关,与开发人员的个人选择或项目/客户的要求有关,而不是在特定项目的特定情况下(例如,上面的一个)。 I mean you don't choose to use SASS if you have a case like the above and drop it if you don't . 我的意思是如果你有像这样的情况,你不会选择使用SASS,如果没有,那就放弃它 The decision is based on larger considerations and taken regardless. 该决定基于更大的考虑,并且无论如何都要做出。 As a rule of thumb I tell everyone : if you write CSS, do use SASS . 根据经验,我告诉所有人 :如果您编写CSS, 请使用SASS Getting back to your suggestion and the question, please note SASS produces CSS and that CSS is not necessarily more efficient , which is what OP seems to ask for, intentional or not. 回到您的建议和问题,请注意,SASS生成了CSS,而CSS不一定更高效 ,这是OP似乎有意要求的,无论是否有意。
In terms of efficiency, such as rendering speed and browser performance, I must say I'm really not sure which code is more efficient between the two (specifying each case or a pattern). 在效率方面,例如渲染速度和浏览器性能,我必须说我真的不确定两者之间哪个代码更有效(指定每种情况或一个模式)。 I'm assuming a pattern should be a little heavier on the parser and the extra weight might not justify the characters saved by the shorter syntax. 我假设解析器上的模式应该更重一些,而且额外的权重可能无法证明较短语法所保存的字符是合理的。
In any case, the differences are so tiny they don't really matter in like 99,99% of cases. 无论如何,差异是如此之小,以至于在99.99%的情况下,它们都不重要。 Regardless, if you decide to pursue this issue, I'm genuinely interested in any results/tests. 无论如何,如果您决定追求这个问题,那么我对任何结果/测试都非常感兴趣。 Let's just say I choose to regard CSS more like a hobby than a job, which is the opposite of how I feel about JavaScript these days. 可以说,我选择将CSS看作是一种业余爱好,而不是一份工作,这与我最近对JavaScript的看法相反。
If efficiency is measured in characters typed, I guess the pattern wins. 如果以键入的字符来衡量效率,我猜该模式会胜出。 If it's measured in time spent to code, it varies from person to person and has more to do with knowledge than syntax. 如果以花费在编码上的时间来衡量,它会因人而异,并且与知识有关的多于语法。

Try: 尝试:

.btn:hover [class^="glyphicon-"]

This targets all classes that start with glyphicon- 定位以glyphicon- 开头的所有类

The carat symbol. 克拉符号。 It's most commonly used in regular expressions to designate the beginning of a string. 在正则表达式中最常使用它来指定字符串的开头。

The 30 CSS Selectors You Must Memorize 您必须记住的30个CSS选择器

You can use LESS or SASS for that. 您可以为此使用LESS或SASS。 It allows you to nest classes/tags/IDs and other selectors inside each other. 它允许您将类/标签/ ID和其他选择器相互嵌套。 With that you could write 这样你可以写

.btn:hover {
  .glyphicon-minus,
  .glyphicon-ok,
  .glyphicon-remove,
  .glyphicon-trash,
  .glyphicon-arrow-left,
  .glyphicon-arrow-right,
  .glyphicon-eye-open,
  .glyphicon-floppy-disk,
  .glyphicon-print,
  .glyphicon-pencil {
    color: black !important;
  }
}

The answers recommending CSS Regex will definitely solve your problem if you don't need to be selective about which glyphicons are being impacted. 如果您不需要选择要影响哪些字形,建议CSS正则表达式的答案肯定会解决您的问题。

In the event you need it to be specific though, unfortunately CSS' limitation stops there and you should consider looking into a preprocessor like Sass or a postprocessor like PostCSS to help with inefficient coding time. 如果您需要具体说明,不幸的是CSS的局限性就此停止了,您应该考虑研究像Sass这样的预处理器或像PostCSS这样的后处理程序,以帮助缩短编码时间。

.btn:hover {
     .glyphicon {
        &-minus,
        &-ok,
        &-remove,
        &-trash,
        &-arrow-left,
        &-arrow-right,
        &-eye-open,
        &-floppy-disk,
        &-print
        &-pencil {
            color: black !important;
        }
    }
}

Would give you the same output if you were using Sass. 如果您使用的是Sass,则会提供相同的输出。

尝试.btn:hover:matches(.glyphicon-minus, .glyphicon-ok, .glyphicon-remove, .glyphicon-trash, .glyphicon-arrow-left, .glyphicon-arrow-right, .glyphicon-eye-open, .glyphicon-floppy-disk, .glyphicon-print .glyphicon-pencil) {/*your css*/}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM