Usage of id attribute

hi guys, could u please tell me, id attribute for what is useful? I didn’t get it.

Your code so far

<link href="" rel="stylesheet" type="text/css">
.red-text {
  color: red;

h2 {
  font-family: Lobster, monospace;

p {
  font-size: 16px;
  font-family: monospace;

.thick-green-border {
  border-color: green;
  border-width: 10px;
  border-style: solid;
  border-radius: 50%;

.smaller-image {
  width: 100px;

.silver-background {
  background-color: silver;

<h2 class="red-text">CatPhotoApp</h2>
<p class="red-text">Click here to view more <a href="#">cat photos</a>.</p>

<a href="#"><img class="smaller-image thick-green-border" src="" alt="A cute orange cat lying on its back."></a>

<div class="silver-background">
  <p>Things cats love:</p>
    <li>cat nip</li>
    <li>laser pointers</li>
  <p>Top 3 things cats hate:</p>
    <li>flea treatment</li>
    <li>other cats</li>

<form action="" id="cat-photo-form">
  <label><input type="radio" name="indoor-outdoor" checked> Indoor</label>
  <label><input type="radio" name="indoor-outdoor"> Outdoor</label><br>
  <label><input type="checkbox" name="personality" checked> Loving</label>
  <label><input type="checkbox" name="personality"> Lazy</label>
  <label><input type="checkbox" name="personality"> Energetic</label><br>
  <input type="text" placeholder="cat photo URL" required>
  <button type="submit">Submit</button>

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_1) AppleWebKit/604.3.5 (KHTML, like Gecko) Version/11.0.1 Safari/604.3.5.

Challenge: Set the id of an Element

Link to the challenge:

Hello there~!

id attributes are used to uniquely identify an element in your HTML. While you can use id attributes to style your elements with CSS, I believe the more common practise is to style with classes (classes can be set to multiple elements, while ids cannot/should not).

The primary use case for the id attribute, IMO, is to be able to select/target that element with JavaScript and manipulate its attributes in the DOM. :slightly_smiling_face:


Hi! The global ID attribute defines a unique identifier, which should not be repeated throughout the document. Its purpose is to identify the element when linking it, whether in scripts or style sheets (with Css). :wave:t2:

1 Like

Identifying an element happens through “id” attribute. Say you’ve a form with 3 input fields collecting name, email and password. For each of them you describe an id - id=“name”, id=“email”, id=“password” respectively. You then access it through “id” ensuring whatever action you’re performing applies only to that field. Usually the actions are either -

  1. CSS styling
  2. targeting sections of a page through references
  3. DOM manipulation

In contrast, take “name” attribute. It helps identify a group of similar fields and sends “name”:“value” pairs to server. In the code you’ve shown, all check boxes have name=“personality” , all radio buttons have name=“indoor-outdoor”. You want to check collectively what the input for the group with name personality is.

However, you use id=“cat-photo-form” once. This is to ensure you can have another form, say, id=“dog-photo-form” but with same fields. This helps distinguish dog-photo-form fields from cat-photo-form ones, as you’ve unique identifiers for enclosed fields though one id attribute.

1 Like

thank u so much for help

thanks for your explanation

1 Like

:slightly_smiling_face: thank u