List Styling, Generated Content, and Counters
list-style-type Property
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| list-style-type | Specifies the markers to display for list items. |
Unordered Lists:
|
While it is possible to style list items for an ordered list using non-numerical markers (and vice versa), keeping the semantics of the markup and its visual presentation aligned is best.
Using the |
list-style-type Example
See the Pen List Styling by Jason Withrow (@jwithrow) on CodePen.
list-style-position Property
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| list-style-position | Determines text position relative to the list item marker. |
|
Not used very frequently. |
list-style-position Example
See the Pen List Style Position by Jason Withrow (@jwithrow) on CodePen.
list-style-image Property
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| list-style-image | Used to insert a graphical image as the list item marker. |
|
Browsers differ in how they place the image relative to the list item text, so it is recommended that testing occur cross-browser and cross-platform.
Empty space may need to be added to the image to improve rendering across browsers. Also be careful with the application of line-height to the list items that have the graphical marker. In some browsers this can cause rendering complications. |
list-style-image Example
See the Pen List Style Image by Jason Withrow (@jwithrow) on CodePen.
list-style Shorthand Property
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| list-style | Combines the other list-related properties into a sequence. | list-style-type list-style-position list-style-image | Usually just the value for list-style-type is specified, so this offers a slightly more streamlined way of providing that information.
Using the shorthand also means that the values not specified are assigned to defaults (overriding user and browser settings). If a |
list-style Shorthand Example
See the Pen List Style by Jason Withrow (@jwithrow) on CodePen.
List Numbering and Counters
- There is currently no simple, cross-browser way to set the starting value for a list via CSS, or the value of a specific list item via CSS.
- As a result, the deprecated
startandvalueHTML attributes are still used. - The CSS approach that we do have is counters, and it allows any markup (not just list items) to be numbered.
- It relies on CSS generating the content, and counters are just one possibility when it comes to generating content.
::before and ::after Pseudo-Elements for Generated Content
- As CSS matured, and its second major version was released (CSS2), the capabilities of the styling language evolved from just styling what was there to being able to add content to the page.
- This addition was not entirely welcomed at the time, as it allowed CSS to intrude into the territory handled by HTML (the structural layer of markup and content) and also JavaScript (the behavior layer that changes the HTML dynamically), but in the years since then it has become widely adopted. You will see it used for a variety of effects, like adding a chevron (<) to the edge of an element's border.
- The
contentproperty determines what content gets added as the page is rendered, and the::beforeand::afterpseudo-elements determine where the content appears. - As an example, let's assume you are styling a paragraph:
- Adding
contentusing::before, the browser is inserting a pseudo-element (which you can style as desired) and the indicated content immediately in front of the existing paragraph text that is in the HTML. - Adding
contentusing::afteradds a pseudo-element (which you can style as desired) and the indicated content immediately after the existing paragraph text that is in the HTML.
- Adding
- The earliest versions of these pseudo-elements used a single colon rather than a double colon. As the specification evolved,
:beforechanged to::beforeand:afterto changed::after, although browsers should accept either version.
| Pseudo-Element or Property | Usage and Effect | Properties / Values |
|---|---|---|
| :after or ::after | Inserts the indicated content after the selector's HTML content. | A wide variety of CSS properties, most importantly the content property. |
| :before or ::before | Inserts the indicated content before the selector's HTML content. | A wide variety of CSS properties, most importantly the content property. |
| content | Specifies content to insert either ::before or ::after |
The
|
Example of Generated Content Involving Images
See the Pen Generated Content: Images by Jason Withrow (@jwithrow) on CodePen.
Example of Generated Content Involving Counters and Text Output
See the Pen Generated Content: Counters by Jason Withrow (@jwithrow) on CodePen.
Read about more counter-related properties
Example of Generated Content Involving Attribute Output
See the Pen Generated Content: Attributes by Jason Withrow (@jwithrow) on CodePen.
Example of Generated Content Involving Quotes
See the Pen Generated Content: Quotes by Jason Withrow (@jwithrow) on CodePen.