小编Spi*_*ike的帖子

CSS ID和类

我是HTML/CSS编码的新手,因为我第一次碰到ID和类选择器,我脑海里浮现出一个问题.我一遍又一遍地搜索网络,但我找不到我想要的答案.

问题是:为什么在你可以用类做同样的任务时使用ID?

我的意思是,我知道ID只是一次性使用,而且类很多,但是什么禁止我使用类选择器一次?那么,根据这个,CSS中存在ID的目的是什么?

此外,当我们在样式表中使用完全相同的属性创建一个新元素时,我们需要ID甚至类?

以下代码是我想要问的一个例子:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ID's, classes and new elements</title>
<style>
    #sidebar1 {
        display: block;
        background-color: yellow;
        width: 200px;
        height: 500px;
        margin-right: 50px;
        float: left;
    }

    .sidebar2 {
        display: block;
        background-color: yellow;
        width: 200px;
        height: 500px;
        margin-right: 50px;
        float: left;
    }

    new_element {
        display: block;
        background-color: yellow;
        width: 200px;
        height: 500px;
        margin-right: 50px;
        float: left;
    }
</style>
</head>

<body>

    <div id="sidebar1">What is the difference?</div><!--Use of ID selector-->

    <div class="sidebar2">What is the difference?</div><!--Use of class …
Run Code Online (Sandbox Code Playgroud)

html css class

5
推荐指数
1
解决办法
926
查看次数

标签 统计

class ×1

css ×1

html ×1