A CSS tricky situation- the order of the CSS class names in the HTML tags

栏目: IT技术 · 发布时间: 4年前

内容简介:Let's say we have the following set of rules in a CSS file:And the following tags in our HTML:The question is:

Let's say we have the following set of rules in a CSS file:

.red {
    color: red;
}

.blue {
    color: blue;
}

.green {
    color: green;
}

And the following tags in our HTML:

<div> First div.</div>
<div> Second div.</div>
<div> Third div.</div>

The question is: what color will we have for these divs? Stop for a few seconds and try to answer the question.

The correct answer: they’re all green.

The order of the class names in HTML tags has no importance on the styles. The lines class="red green blue" , class="blue red green" or class="green blue red" are the same thing.

All 3 selectors have the same specificity (just a simple class selector). And given that .green comes later in the stylesheet, it will override the .red and .blue selectors.

Therefore all the divs will have the color green.Cascading wins again.

By the way, this can make a great CSS interview question

I hope you have enjoyed this article and if you would like to get more articles about React and frontend development you can always sign up for my email list.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

罗辑思维

罗辑思维

罗振宇 / 长江文艺出版社 / 2013-10-25 / 36.00

本书根据罗振宇的互联网视频知识脱口秀《罗辑思维》创作。 资深媒体人罗振宇对正在到来的互联网时代有深刻的洞察。他认为,互联网正在成为我们生活中的“基础设施”,它将彻底改变人类协作的方式,使组织逐渐瓦解、消融,而个体生命的自由价值得到充分释放。 《罗辑思维》的口号是“有种、 有趣、有料”,做大家“身边的读书人”,倡导独立、理性的思考,凝聚爱智求真、积极上进、自由阳光、人格健全的年轻人。......一起来看看 《罗辑思维》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具