Search⌘ K
AI Features

Cascading Style Sheets (CSS)

Explore how to use CSS in D3.js to style simple graphs, focusing on selectors, properties, and values to change line width, color, and fill. Understand how CSS rules influence the appearance of graph elements and practice modifying styles for interactive visuals.

Let’s take a look at the CSS part of the simple graph code to understand why our graph looks the way it does.

The CSS portion of the code

The CSS code is as follows:

NAME_
CSS
.line {
fill: none;
stroke: steelblue;
stroke-width: 2px;
}

Understanding the CSS portion

CSS gives us control over the look, feel, and presentation of web content. The idea is to define a set of properties to objects on the web page.

They are made up of “rules.” Each rule has a “selector” and one or more “declarations.” Each declaration has a property and a value (or a group of properties and values).

For instance, in the CSS code above, we have the following:

  • line is the selector. The period (.) in front of line indicates that the selector is a “class.” This tells us that on the web page, any particular element line (and we are going to apply this rule to the line of our graph), which we decorate with the “class,” will have the various declarations applied to it.

  • There are three declarations as part of the rule. These are contained within curly braces and separated by semicolons.

  • One of the declarations is for the width of the graph line (stroke-width: 2px;). The property is stroke-width:, and the value is 2px (2 pixels). This tells the web page that any element in the web page that has the class line will have lines drawn that are (amongst other things) 2 pixels wide.

Sure enough, if we look at the line of the graph:

Graph line with stroke-width of 2 pixels
Graph line with stroke-width of 2 pixels

That looks as if the line might actually be 2 pixels wide!

Let’s try a test. We can change that particular declaration (line 4) to the following:

stroke-width: 20px; 

And the result is:

Graph line with stroke-width of 20 pixels
Graph line with stroke-width of 20 pixels

Ahh, 20 pixels of goodness!

Because we’re getting the hang of things now, let’s change the color declaration to:

stroke: red; 

And we get:

Graph line with stroke color changed to red
Graph line with stroke color changed to red

Awesome! I think we can safely say that this has had the desired effect. So what else is there?

Since there’s only one declaration left, it seems like a shame not to try something different with it:

fill: blue; 

We’ll get:

So the fill property (line 2) looks like it will change the color of the area that would be closed by the line. Nice.

One thing to take away from this small exercise is that there is a good deal of flexibility in adjusting the properties of elements on the web page via CSS.


You can test these changes by editing the CSS portion (highlighted) below:

date,close
1-May-12,58.13
30-Apr-12,53.98
27-Apr-12,67.00
26-Apr-12,89.70
25-Apr-12,99.00
24-Apr-12,130.28
23-Apr-12,166.70
20-Apr-12,234.98
19-Apr-12,345.44
18-Apr-12,443.34
17-Apr-12,543.70
16-Apr-12,580.13
13-Apr-12,605.23
12-Apr-12,622.77
11-Apr-12,626.20
10-Apr-12,628.44
9-Apr-12,636.23
5-Apr-12,633.68
4-Apr-12,624.31
3-Apr-12,629.32
2-Apr-12,618.63
30-Mar-12,599.55
29-Mar-12,609.86
28-Mar-12,617.62
27-Mar-12,614.48
26-Mar-12,606.98
Simple graph code