ID Selector

The CSS ID selector is used to style a single, unique element on a webpage. IDs are unique within a page, meaning each element should have only one ID.

Syntax

#idname {
    property: value;
}

Example

#header {
    background-color: black;
    color: white;
    padding: 20px;
    text-align: center;
}
/* Styling a unique button with the ID "submit-btn" */
#submit-btn {
    background-color: green;
    color: white;
    font-size: 16px;
}

Explanation

  • #header applies styles only to the element with ID "header".
  • #submit-btn applies styles only to the element with ID "submit-btn".

Example in HTML

<h1 id="header">Welcome to My Website</h1>
<button id="submit-btn">Submit</button>
Differences: ID vs. Class

SelectorUsageExample
#idnameUnique for one element#header { color: red; }
.classnameReusable for multiple elements.button { color: blue; }
Qus. 1 : Which of the following attribute is used to provide a unique name to an element?

  1. class
  2. id
  3. type
  4. None of the above
Qus. 2 : Which attribute specifies a unique alphanumeric identifier to be associated with an element?

  1. class
  2. id
  3. article
  4. html
Qus. 3 : Which of the following selectors select any tag with an id attribute set ?

  1. E#id
  2. .class
  3. #id
  4. *

Programs

HTML code using CSS to create a job application

View Solution


Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad