ID与CSS中的CLASS。请用例子详细解释 [英] ID vs CLASS in CSS. Please explain in detail with example

查看:179
本文介绍了ID与CSS中的CLASS。请用例子详细解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说ID是唯一的,只能在页面中使用一次,但在页面上多次使用时,它的工作状况良好。请让我知道身份证的用途,并发现其与课程完全不同?

@HTML FILE

 < html xmlns =http: //www.w3.org/1999/xhtml\"> 
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< LINK href =special.css =stylesheettype =text / css>
< title>无标题文档< / title>
< / head>

< body>
< div class =layout>
< div class =left_box>
< div id =color>< / div>
< / div>

< div class =right_box>
< div id =color>< / div>
< / div>
< / div>
< / body>
< / html>

@CSS FILE

  @charsetutf-8; 
/ * CSS文档* /
.layout {
width:600px;
height:600px;
背景颜色:#666;
margin:0 auto;
}

.left_box {
width:300px;
height:600px;
float:left;
}

.right_box {
width:300px;
height:600px;
float:right;
}

#color {
background-color:#CCC;
height:600px;
}


解决方案


我听说ID是唯一的,只能在页面中使用一次

这是一个HTML规则,并且没有任何关系与CSS。


但它在页面上多次使用时正常工作。


浏览器执行错误恢复。不要依赖它,因为并非所有浏览器都能以相同的方式从所有错误中恢复。写有效标记。


请让我知道ID的用途,并指出它与类完全不同?


在HTML中 - 每个文档的id是唯一的并可以成为链接目标。一个类可以被重用。

在CSS中 - 一个id选择器有一个更高的特异性比类选择器。


I have heard that ID is unique and can only be used once in a page, but its working fine when used over multiple times on a page. Please let me know the purpose of ID and hows its exactly different from classes?

@HTML FILE

  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <LINK href="special.css" rel="stylesheet" type="text/css">
   <title>Untitled Document</title>
   </head>

    <body>
    <div class="layout">
    <div class="left_box">
    <div id="color"></div>
   </div>

   <div class="right_box">
    <div id="color"></div>
    </div>
     </div>
   </body>
   </html>

@CSS FILE

      @charset "utf-8";
     /* CSS Document */
     .layout {
 width:600px;
  height:600px;
  background-color:#666;
  margin:0 auto;
      }

     .left_box {
  width:300px;
  height:600px;
  float:left;
        }

    .right_box {
 width:300px;
 height:600px;
 float:right;
    }

    #color {
background-color:#CCC;
height:600px;
    }

解决方案

I have heard that ID is unique and can only be used once in a page

That is an HTML rule and has nothing to do with CSS.

but its working fine when used over multiple times on a page.

Browsers perform error recovery. Don't depend on it as not all browsers will recover from all errors in the same way. Write valid markup.

Please let me know the purpose of ID and hows its exactly different from classes?

In HTML terms — an id is unique per document and can be a link target. A class can be reused.

In CSS terms — An id selector has a higher specificity than a class selector.

这篇关于ID与CSS中的CLASS。请用例子详细解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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