如果HTML中的ID应该是唯一的,为什么有时我在Css看到像div#nav-blue? [英] If IDs in HTML should be unique, why sometimes I see in Css see something like div#nav-blue?

查看:155
本文介绍了如果HTML中的ID应该是唯一的,为什么有时我在Css看到像div#nav-blue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于ID在HTML中应该是唯一的,为什么有时候我看到的格式化为(div#nav-blue)的Css选择器,因为很明显没有其他元素具有这个ID的exccept,所以aint写入#nav-blue更有意义?

解决方案

它没有改变或有点。



您可以出于某种原因执行此操作:在维护代码时具有更高的可见性。更容易找到,并记住每种元素的风格。



第二个原因是选择器的优先级。



有一些不同的优先顺序:

 !important> #id> .class>元素

您可以考虑

  element = 1 
.class = 10
#id = 100
!important = 1000

然后 div#id = 101> #id = 100



  div#myid {color:red;}#myid {color :blue;}。myclass {color:yellow;} div {color:green;}  

 < div class =myclassid =myid>一些文本< / div>   

Since an ID should be unique in HTML, why sometimes i see in Css selectors formatted like (div#nav-blue), since it's obvious that there will be no other element having this ID exccept for that div, so aint writting #nav-blue makes more sense?

解决方案

It does no change or a little.

You can do this for some reason : more visibility when you maintain your code. Easier to find, and remember for each kind of element is the style.

The second reason is the priority of selector.

There is some different order of priority :

!important > #id > .class > element

you can consider that

 element = 1
 .class = 10
 #id = 100
 !important= 1000

And then div#id = 101 > #id = 100

div#myid{
  color:red;
}

#myid{
  color:blue;
}

.myclass{
  color:yellow;
}

div{
  color:green;
}

<div class="myclass" id="myid">
Some text
</div>

这篇关于如果HTML中的ID应该是唯一的,为什么有时我在Css看到像div#nav-blue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆