What's the difference between an id and a class?

前端 未结 17 2313
长情又很酷
长情又很酷 2020-11-22 01:55

What\'s the difference between

and
when it comes to CSS? Is it alright to use
<
相关标签:
17条回答
  • A CLASS should be used for multiple elements that you want the same styling for. An ID should be for a unique element. See this tutorial.

    You should refer to the W3C standards if you want to be a strict conformist, or if you want your pages to be validated to the standards.

    0 讨论(0)
  • 2020-11-22 02:10

    Where to use an ID versus a class

    The simple difference between the two is that while a class can be used repeatedly on a page, an ID must only be used once per page. Therefore, it is appropriate to use an ID on the div element that is marking up the main content on the page, as there will only be one main content section. In contrast, you must use a class to set up alternating row colors on a table, as they are by definition going to be used more than once.

    IDs are an incredibly powerful tool. An element with an ID can be the target of a piece of JavaScript that manipulates the element or its contents in some way. The ID attribute can be used as the target of an internal link, replacing anchor tags with name attributes. Finally, if you make your IDs clear and logical, they can serve as a sort of “self documentation” within the document. For example, you do not necessarily need to add a comment before a block stating that a block of code will contain the main content if the opening tag of the block has an ID of, say, "main", "header", "footer", etc.

    0 讨论(0)
  • 2020-11-22 02:15

    Any element can have a class or an id.

    A class is used to reference a certain type of display, for example you may have a css class for a div that represents the answer to this question. As there will be many answers, multiple divs would need the same styling and you would use a class.

    An id refers to only a single element, for example the related section at the right may have styling specific to it not reused elsewhere, it would use an id.

    Technically you can use classes for all of it, or split them up logically. You can not, however, reuse id's for multiple elements.

    0 讨论(0)
  • 2020-11-22 02:16

    ids must be unique where as class can be applied to many things. In CSS, ids look like #elementID and class elements look like .someClass

    In general, use id whenever you want to refer to a specific element and class when you have a number of things that are all alike. For instance, common id elements are things like header, footer, sidebar. Common class elements are things like highlight or external-link.

    It's a good idea to read up on the cascade and understand the precedence assigned to various selectors: http://www.w3.org/TR/CSS2/cascade.html

    The most basic precedence you should understand, however, is that id selectors take precedence over class selectors. If you had this:

    <p id="intro" class="foo">Hello!</p>
    

    and:

    #intro { color: red }
    .foo { color: blue }
    

    The text would be red because the id selector takes precedence over the class selector.

    0 讨论(0)
  • 2020-11-22 02:19

    Perhaps an analogy will help understanding the difference:

    <student id="JonathanSampson" class="Biology Calculus" />
    <student id="MarySmith" class="Biology Networking" />
    

    Student ID cards are distinct. No two students on campus will have the same student ID card. However, many students can and will share at least one Class with each other.

    It's okay to put multiple students under one Class title, such as Biology. But it's never acceptable to put multiple students under one student ID.

    When giving Rules over the school intercom system, you can give Rules to a Class:

    "Tomorrow, all students are to wear a red shirt to Biology class."

    .Biology {
      color: red;
    }
    

    Or you can give rules to a Specific Student, by calling his unique ID:

    "Jonathan Sampson is to wear a green shirt tomorrow."

    #JonathanSampson {
      color: green;
    }
    

    In this case, Jonathan Sampson is receiving two commands: one as a student in the Biology class, and another as a direct requirement. Because Jonathan was told directly, via the id attribute, to wear a green shirt, he will disregard the earlier request to wear a red shirt.

    The more specific selectors win.

    0 讨论(0)
  • CSS is object oriented. ID says instance, class says class.

    0 讨论(0)
提交回复
热议问题