# Selectors Master Race

## Basic Selectors

Always use least specificity you need, so all li's instead of ids. Makes future use better

* \#myID `<li id="myID">`
* .myClass `<li class="myClass">`
* li //element selector
* ul li //descendant selector
* ol > li //direct child selector
* li.myClass + li //adjacent sibling(closest/immediately after)
* li.myClass \~ li //matches all later siblings
* \* //all

## Attribute Selectors

*element\[attribute]* - select elements containing the named attribute

* img\[alt] {}
* img:not(\[alt])
* E\[attr="val"] // exact case-sensitive if attribute is case sensitive
* E\[attr\~="val"] //full word matches i.e spaces aroudn &#x20;
* E\[attr|="val"] // attr has val or begins with val-(useful for languages i.e "lang en-uk" or "lang en-us")
* E\[attr^="val"] // starts with val
* This adds link at end of link for all external links, attr(href) prints the exact url preprocessing

  ```css
  a[href^=http]:after { content: " (" attr(href) ")"; }
  ```
* E\[attr$="val"] //ends with val

  ```css
  a[href$=pdf] { background-iamge: url(pdficon.gif); }
  ```
* E\[attr\*="val"] //appears anywhere in content
* E\[attr="val" i] //i adds case insensitive

## UI pseudo-classes

Based on current state of UI, recall CSS updates immediately

```css
input[type=checkbox]:checked + label {
  color: red;
}
```

* :default
* :valid
* :invalid
* :out-of-range //with using min and max, step default to 1
* :not(:valid) { ... }

## Specificity

* {} 0-0-0 //which every has highest number in the column wins left to right
* 1-0-0: ID selector
* 0-1-0: Class selector
* 0-0-1: Element Selector
* On tie whichever comes later


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openai.gitbook.io/code-cheatsheets/css/selectors.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
