Skip to content

Instantly share code, notes, and snippets.

@jackilyn
Created January 12, 2012 20:40
Show Gist options
  • Select an option

  • Save jackilyn/1602963 to your computer and use it in GitHub Desktop.

Select an option

Save jackilyn/1602963 to your computer and use it in GitHub Desktop.
CSS Selectors
/* Adjacent Selector
* It will select only the element that is immediately preceded by the former element.
* ------------------------------ */
/* First preceding element only */
ul + p {
color: red;
}
/* All preceding elements */
h1 ~ p {
text-indent:20px;
}
/* Attribute Selectors
* ------------------------------ */
/* <a> with attribute `­att­r` */
a[attr] {
color: red;
}
img[alt] {
border:1px dotted red;
}
/* style any link with a href attribute ending with `pdf` */
a[href$=".pdf"] {
color: red;
}
/* style any link with a href attribute starting with `http` */
a[href^="http"] {
background: url(path/to/external/icon.png) no-repeat;
padding-left: 10px;
}
/* Finds an element whose attribute value contains the supplied string */
a[href*="jackilyn"] {
color: purple;
}
/* Finds attribute on the selected element and takes a space separated list of values, each of which can be styled differently. */
a[attr~="rawr meow"] {
font-weight: bold;
}
a[attr~="rawr"] {
color: red
}
a[attr~="meow"] {
color: blue;
}
div > span {
color: red;
}
/* First child element */
:first­-child {
}
/* First line of element */
:first­-line {
}
/* First letter of element */
:first­-letter {
}
/* Element with mouse over */
:hover {
}
/* Active element */
:active {
}
/* Element with focus */
:focus {
}
/* Unvisited links */
:link {
}
/* Visited links */
:visited {
}
/* Element with language `var` */
:lang(var) {
}
/* Before element */
:before {
}
/* After element */
:after {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment