Attribute Selectors

CSS attribute selectors allow you to style elements based on their attributes and values, providing greater flexibility in targeting elements.

SelectorExampleDescription
[attribute]input[disabled]Selects elements with a specified attribute
[attribute=value]input[type="text"]Selects elements with an exact attribute value
[attribute~=value]div[class~="box"]Selects elements with an attribute containing a word (space-separated list)
[attribute^=value]a[href^="https"]Selects elements where the attribute starts with a specific value
[attribute$=value]img[src$=".jpg"]Selects elements where the attribute ends with a specific value
[attribute*=value]a[href*="google"]Selects elements where the attribute contains a specific value

Explanation of Selectors

SelectorDescriptionExample Usage
[attribute]Targets elements that have a specific attributeinput[required] { border: red; }
[attribute=value]Matches exact valueinput[type="checkbox"] { width: 20px; }
[attribute~=value]Matches a word in a space-separated listdiv[class~="featured"] { background: yellow; }
[attribute^=value]Matches attributes that start with a valuea[href^="https"] { color: green; }
[attribute$=value]Matches attributes that end with a valueimg[src$=".png"] { border-radius: 10px; }
[attribute*=value]Matches attributes containing a valuea[href*="facebook"] { font-weight: bold; }

Example:

/* Select all input fields with type text */
input[type="text"] {
    border: 2px solid blue;
}
/* Highlight all links that start with "https" */
a[href^="https"] {
    color: green;
}
/* Add a red border to images ending with .jpg */
img[src$=".jpg"] {
    border: 3px solid red;
}

✔ Ensures specific styling for different attributes

✔ Improves precision when styling elements dynamically

Qus. 1 : Which of the following selector selects all elements of E that have the attribute attr that end with the given value?

  1. E[attr^=value]
  2. E[attr$=value]
  3. E[attr*=value]
  4. None of these
Qus. 2 : Which of the following selector selects the elements that are checked?

  1. E ~ F
  2. ::after
  3. :checked
  4. None of these
Qus. 3 : How do you style all elements with a specific attribute, regardless of its value

  1. [attr]
  2. [attr=""]
  3. [attr=*]
  4. attr[ ]
Qus. 4 : <p>Match the CSS Attribute Selector with Its Function</p><p><br></p><table class="table table-bordered"><tbody><tr><td><span style="font-size: 14px;"><b>CSS Attribute Selector</b></span></td><td><span style="font-size: 14px;"><b>Function</b></span></td></tr><tr><td><span style="font-size: 14px;">A. [type="text"]</span></td><td><span style="font-size: 14px;">1. Selects all elements that have a title attribute</span></td></tr><tr><td><span style="font-size: 14px;">B. [title]</span></td><td><span style="font-size: 14px;">2. Selects input fields with the type exactly as "text"</span></td></tr><tr><td><span style="font-size: 14px;">C. [href^="https"]</span></td><td><span style="font-size: 14px;">3. Selects all &lt;a&gt; tags where href starts with "https"</span></td></tr><tr><td><span style="font-size: 14px;">D. [class~="btn"]</span></td><td><span style="font-size: 14px;">4. Selects elements whose class attribute contains "btn" as a separate word</span></td></tr><tr><td><br></td><td><span style="font-size: 14px;">5. Selects elements where class contains "btn" anywhere in the value</span></td></tr><tr><td><br></td><td><span style="font-size: 14px;">6. Selects elements where href contains "https" anywhere in the URL</span></td></tr></tbody></table><p><br></p>

  1. A → 2, B → 5, C → 3, D → 4
  2. A → 2, B → 6, C → 3, D → 4
  3. A → 5, B → 1, C → 3, D → 4
  4. A → 2, B → 1, C → 3, D → 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