liveBook · Manning

Fill aesthetic

As explained in D3.js in Action, Third Edition

The SVG fill attribute specifies the paint applied to the interior of a shape or path.

The SVG `fill` attribute sets the color inside a shape. Most SVG shapes appear black until you set a fill. Give a rectangle a CSS color value, such as `fill="#6ba5d7"`, to change its interior; use `stroke` separately for its border. section 1.2.2

Color a rectangle with D3

Append a rectangle, set its position and size, then chain `.attr("fill", "turquoise")`. The color is passed as a string; `x`, `y`, `width`, and `height` determine where the colored shape appears. section 2.5

Fill can also be set with D3’s `.style("fill", "plum")` or a CSS rule such as `rect { fill: orange; }`. If the same rectangle has an attribute fill of turquoise, an inline-style fill of plum, and a stylesheet fill of orange, the inline style wins. Use one approach consistently when possible so the result is easier to manage. section 2.6

Choose a fill to match the visible result you want:

  • Use `fill="none"` for a stroked path or outline without a filled interior. section 1.2.2
  • Use `fill-opacity` to make a filled shape semitransparent. section 1.2.2

How D3.js in Action, Third Edition covers it

How deep it goes
Works through fill attributes, D3 styling methods, and a three-way cascade example; does not develop a broader styling system.
Examples
Uses colored and outlined SVG shapes, a turquoise D3 rectangle, and competing turquoise, plum, and orange fills.
What it assumes
Builds from an HTML SVG container and basic D3 selections to setting a rectangle’s attributes.
What you can do after
Apply it via D3’s `attr()` or `style()` methods, or CSS, and identify why an inline fill overrides other demonstrated fills.
How it connects
Connects SVG shapes and paths to stroke, opacity, CSS, and the style cascade.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.