InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS selectors

    Simple Selectors

    • type selectors
    • universal selector (*)
    • attribute presence and value selectors
    • attribute substring selectors
    • class selectors
    • two classes (.c1.c2)
    • id selectors

    Pseudo-classes

    • :link
    • :visited
    • :hover
    • :active
    • :focus
    • :target
    • :lang
    • :enabled and :disabled
    • :checked
    • :root
    • :nth-child
    • :nth-of-type
    • :nth-child vs :nth-of-type
    • :nth-last-child
    • :nth-last-of-type
    • :first-child
    • :first-of-type
    • :last-child
    • :last-of-type
    • :only-child
    • :only-of-type
    • :empty
    • :not

    Pseudo-elements

    • ::first-line
    • ::first-letter
    • ::before and ::after
    • ::placeholder

    Combinators

    • child vs descendant selector
    • adjacent sibling selector (E1 + E2)
    • general sibling selector (E1 ~ E2)

    Specificity

    • Selector specificity
     
    • Home
    • > Tutorials
    • > CSS
    • > CSS selectors

    CSS attribute substring selectors

    on Apr 6, 2016

    CSS attribute selector can be used to select DOM elements based on matching substring of attribute values. We can use attribute substring selectors in multiple ways.

    [att^=val] attribute value begins with val. If val is empty, then it does not do anything.
    [att$=val] attribute value ends with val. If val is empty, then it does not do anything.
    [att*=val] attribute contains val. If val is empty, then it does not do anything.

    Note that in substring matching there is no requirement of match having space or any other boundary character before or after.

    Example – attribute value prefix match selector

    a tag with href beginning with “#” will have different color

    <style type="text/css">
    a[href^="#"] {
      color:red;
    }
    </style>
    
    <a href="#link1">link to #link1</a><br/>
    <a href="#link2">link to #link2</a><br/>
    <a href="/">link to /</a><br/>
    refresh done
    try it online

    Example – attribute value suffix match selector

    a tag with href ending at “.html” will have different color

    <style type="text/css">
    a[href$=".html"] {
      color:red;
    }
    </style>
    
    <a href="/demo2/hello.html">link to hello.html</a><br/>
    <a href="/">link to /</a><br/>
    refresh done
    try it online

    Example – attribute value contains match selector

    a tag with title containing “hello” will have different color

    <style type="text/css">
    a[title*="hello"] {
      color:red;
    }
    </style>
    
    <a href="/" title="ahello world">link having hello in title</a><br/>
    <a href="/">link to /</a><br/>
    refresh done
    try it online

    Related

    • CSS type selectors - define style by element name
    • CSS universal selector
    • CSS attribute presence and value selectors
    • CSS class selectors - define style by class name
    • CSS - style for element having two classes (both)
    • CSS id selectors - define style by element id

    Specification

    • W3C CSS 3 attribute selectors specification

    Suggested posts:

    1. How to trigger click using jQuery or Javascript
    2. CSS class selectors – define style by class name
    3. CSS placeholder style
    4. CSS pseudo class :visited – style for visited links
    5. CSS color values
    6. CSS word-spacing – additional space between words
    7. Javascript – string ends with check
    8. CSS outline – create outline around an element
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged CSS, CSS selectors, Tutorials, Web Development
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress