Introduction
ID selectors let you style one specific element on a webpage. This helps you change the look of that element without affecting others.
Jump into concepts and practice - no test required
ID selectors let you style one specific element on a webpage. This helps you change the look of that element without affecting others.
#idName { property: value; }
#header { color: blue; }
#submitButton { background-color: green; }
#mainTitle { font-size: 2rem; font-weight: bold; }
The paragraph with id="specialText" is styled to be red, bigger, and bold. The other paragraphs stay normal.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>ID Selector Example</title> <style> #specialText { color: red; font-size: 1.5rem; font-weight: bold; } </style> </head> <body> <p>This is a normal paragraph.</p> <p id="specialText">This paragraph is styled using an ID selector.</p> <p>This is another normal paragraph.</p> </body> </html>
Remember, IDs should be unique on the page. Using the same ID on multiple elements can cause unexpected results.
ID selectors have higher priority than class selectors, so they override class styles if both apply.
ID selectors target one unique element using the # symbol.
Use IDs to style or control a single element on your page.
Make sure each ID is unique to avoid confusion.
#header { color: blue; }# symbol in CSS is used to select an element by its unique ID attribute.main-content in CSS?# symbol followed by the ID name without spaces.#main-content { } to select the element with ID "main-content".<div id="box">Hello</div>
#box { color: red; } .box { color: blue; }#box and a class selector .box.#box selector applies, setting color to red. The class selector does not apply.nav is not styled?#nav { background-color: yellow color: white };. Omitting semicolons between properties causes errors.#nav { background-color: yellow color: white } is missing a semicolon after yellow. This can cause some browsers to ignore the rule.footer to have a green background and white text. Which CSS code correctly does this and ensures no other elements are affected?#idname. Here, #footer targets the element with ID "footer".background-color: green; and color: white; style the background and text color. Using #footer ensures only that element is styled.