TOC

This article is currently in the process of being translated into Chinese (~2% done).

HTML Basics:

Id's & Classes

Now that we have started to semantically divide our content, it is time to introduce yet another set of attributes/values. Up until now, we haven’t been working with the look of your page, but later on you will want to be able to change the look of your webpage and this is where the id’s and classes attributes comes in handy.

Id

ID属性为您的元素分配一个名称。 该名称必须是唯一的,不能在文档中别的元素上使用。 id 是identifier的缩写——当您希望能够标识特定元素时,可以使用 id 属性。

您可以这样使用 id元素,例如在网页中定义列,那样HTML标签看起来如下所示:

<div id="right-column">Content of the right column</div>
<div id="left-column"> Content of the left column </div>

I said that id-elements would have to be unique and this means you cannot have two with the same value in you markup – the following example would not render correctly in the browser:

<div id="menu">This would be your menu</div>
<div id="blog-entry">Your first blog-entry</div>
<div id="blog-entry">Your second blog-entry</div>

Classes

Classes are used to assign some general properties to your element. This means, that you can have several elements in your document with the same class-name. You do this, because you want them to behave and look the same way. Normally this is used for e.g. links or images – when you want a lot of elements to look the same. Below is an example where we assign different classes to the entire blog-entry and to a specific part of the blog-entry.

<div class="blog-entry">
	<p> Just Another Day<br />
	Written by Christina<br />
	On January 11th </p>
	<p class="content">This is my second blog entry, and I just wanted to check in on you </p>
</div>
<div class="blog-entry">
	<p> My First Blog Entry<br />
	Written by Christina<br />
	On January 10th </p>
	<p class="content">I’m so happy to write my first blog entry – yay!</p>
<div>

You can use this in combination with Cascading Style Sheets (CSS). CSS tells the browser how to display a HTML-element, this means that all formatting is (or should be) removed from the HTML-document and stored in a separate file – also referred to as the style sheet. I will not go further into CSS in this tutorial, but it is important that you know there is a thing called style sheets and the design of your webpage is defined in these documents.

What you have learned

  • Id’s and classes are attributes
  • Id’s are unique to the specific element
  • Classes can be used across several elements
  • They are normally used in combination with CSS

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!
adplus-dvertising