Styles the first letter of a text block (like drop caps).
This paragraph has a large first letter. It looks like a drop cap
from old books.
/* ::first-letter - drop cap effect */
p::first-letter {
font-size: 48px;
font-weight: bold;
color: #e94560;
}
7. ::first-line
Styles the first line of a text block.
This is the first line of this paragraph. The first line will be
bold and blue. The rest of the text will be normal. Resize your
browser to see the first line change.
/* ::first-line - styles first line only */
p::first-line {
font-weight: bold;
color: #4a90e2;
}
8. ::selection
Styles text when highlighted by the user.
Select (highlight) this text. The background will turn pink and text
white!
/* ::selection - when user highlights text */
p::selection {
background-color: #e94560;
color: white;
}
9. ::placeholder
Styles placeholder text in inputs.
/* ::placeholder - styles hint text in inputs */
input::placeholder {
color: #888;
font-style: italic;
}
/* =================================== PSEUDO-ELEMENTS (:: two colons)
=================================== */ ::before /* Insert content
BEFORE element */
::after /* Insert content AFTER element */
::first-letter /* First letter of text block */
::first-line /* First line of text block */
::selection /* Highlighted text */
::placeholder /* Input placeholder text */
::marker /* List bullet points/numbers */
::backdrop /* Behind modal/dialog */
::file-selector-button /* Style file upload button */
/* =================================== IMPORTANT RULES
=================================== */ /* 1. content property is
REQUIRED for ::before and ::after */
.element::before { content: ""; } /* Must have content */
/* 2. Two colons :: is modern syntax (CSS3) */
/* One colon : is older syntax (CSS2) - still works */
/* 3. ::before and ::after work on most elements, but not all */
/* Does NOT work on: img, input, textarea, select, br, hr */
/* =================================== COMMON USE CASES
=================================== */ /* Icons before list items
*/
li::before { content: "✓ "; }
/* Badges on elements */
.element::before { content: "NEW"; position: absolute; }
/* Drop caps (old book style) */
p::first-letter { font-size: 48px; font-weight: bold; }
/* =================================== PSEUDO-CLASSES (one colon :)
Target a STATE of an element =================================== */
:hover /* Mouse over */
:focus /* Focused */
:first-child /* Position in parent */
:nth-child() /* Position in parent */
/* =================================== PSEUDO-ELEMENTS (two colons ::)
Target a PART of an element =================================== */
::before /* Inserted content before */
::after /* Inserted content after */
::first-letter /* First letter */
::selection /* Highlighted text */
/* =================================== QUICK EXAMPLE
=================================== */ /* Pseudo-class - targets STATE
*/
button:hover { background: red; }
/* Question 1: What pseudo-element inserts content BEFORE an element?
*/ /* Answer: ::before */ /* Question 2: What property is REQUIRED for
::before and ::after? */ /* Answer: content */ /* Question 3: What
pseudo-element styles highlighted text? */ /* Answer: ::selection */
/* Question 4: What is the difference between :first-child and
::first-letter? */ /* Answer: :first-child is a pseudo-class
(state/position). ::first-letter is a pseudo-element (part of
element). */ /* Question 5: Can ::before work on an tag? */ /*
Answer: No. Does not work on self-closing tags like img, input, br, hr
*/