Unit 6 ยท Cascading Style Sheets (CSS)

6.5Pseudo-classes and Pseudo-elements

Pseudo-classes (one colon) style an element in a particular state.

CSS
a:link    { color: blue; }        /* unvisited link */
a:visited { color: purple; }      /* visited link */
a:hover   { color: red; text-decoration: underline; }  /* mouse over */
a:active  { color: orange; }      /* while being clicked */

input:focus    { outline: 2px solid dodgerblue; }  /* keyboard/typing focus */
input:disabled { background: #eee; }
input:checked + label { font-weight: bold; }
input:valid    { border-color: green; }
input:invalid  { border-color: red; }

li:first-child  { font-weight: bold; }
li:last-child   { border-bottom: none; }
li:nth-child(odd)  { background: #f2f2f2; }   /* zebra stripes */
li:nth-child(3n)   { color: gray; }
p:not(.note)    { color: #333; }

Remember the order for links: LoVe HAte (link, visited, hover, active).

Pseudo-elements (two colons) style a part of an element or insert generated content.

CSS
p::first-line   { font-weight: bold; }
p::first-letter { font-size: 2em; color: crimson; }  /* drop-cap */
::selection     { background: yellow; }               /* highlighted text */
input::placeholder { color: #999; }

.required::after { content: ' *'; color: red; }       /* adds text after */
.quote::before   { content: '\201C'; }                /* opening quote mark */

::before and ::after need a content property (can be an empty string) to appear.

Try it: Create a navigation menu from a <ul>. Remove bullets with list-style: none, show links side by side with display: inline-block, add padding, and change the background on a:hover. Add zebra stripes to a table using tr:nth-child(even).