Using Styles in Cascading Style Sheets

Learn how to use CSS for the styling of your D3 elements.

Declaring styles

Declaring styles on an element by element basis is an OK way to apply styles. But when our visualizations become more complex, this can be an inefficient use of code.

A smarter way to provide a common set of styles to elements is to declare them in the <style> section of our HTML document using CSS. These will then be automatically applied to our elements.

Drawing lines with styles

We start with an example script that draws our three lines that have different styles of linecaps. Our previous example looked like the following:

Get hands-on with 1200+ tech skills courses.