Tx · LaTeX & TikZ

How to Set the Background Color of a Multilayer TikZ Node

Published By the 9to5Science editorial team

At a glance

  1. 1. Choose the node type

    Use fill for a regular node, or rectangle split part fill for separate parts of a multipart node.

  2. 2. Load the library

    For a rectangle split node, load shapes.multipart with the tikzpicture’s TikZ libraries.

  3. 3. Set each part’s color

    Give rectangle split part fill a color list in the same order as the node’s parts.

  4. 4. Check the layering

    If the node is made from separate overlaid nodes, set fill on each one that needs a background.

In TikZ, a node’s background is controlled by its fill color. A regular node gets one fill for its entire shape. If by “multilayer node” you mean a node divided into several parts, use the rectangle split shape and specify a fill color for each part. If the layers are actually separate nodes stacked on top of one another, set the fill on each node independently.

Set one background color

Add fill=<color> to the node’s options. For example, fill=yellow!20 gives the node a pale yellow background. The draw option adds an outline; it is independent of the fill.

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \node[draw, fill=blue!15, rounded corners, inner sep=6pt]
    {A node with a light blue background};
\end{tikzpicture}
\end{document}

The fill option colors the node’s shape behind its text. It does not change the text color; use text=<color> for that. The inner sep option controls the space between the text and the node boundary.

Give parts of a split node different fills

For a node divided into sections, load the shapes.multipart library and use the rectangle split shape. Set rectangle split parts to the number of sections, then give rectangle split part fill a color for each part, in order.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.multipart}

\begin{document}
\begin{tikzpicture}
  \node[
    draw,
    rectangle split,
    rectangle split parts=3,
    rectangle split part fill={blue!15,white,orange!20},
    minimum width=4cm
  ] {
    Header
    \nodepart{two}Details go here
    \nodepart{three}Footer
  };
\end{tikzpicture}
\end{document}

The first color fills the first part, the second fills the part named two, and the third fills three. In this example, the node outline is drawn around the full shape, with the split separators between parts. You can change the color names or use mixtures such as green!20 and red!10.

If you leave out rectangle split part fill, the parts use the node’s ordinary fill settings. The part-fill list should correspond to the number of parts: add a color for every section you want to color explicitly.

When the layers are separate nodes

TikZ does not treat a collection of overlapping nodes as one multipart node. Each node is a separate shape. Give each one its own fill, and draw the intended background first so later nodes do not cover it.

\begin{tikzpicture}
  \node[draw, fill=gray!15, minimum width=3cm, minimum height=1.5cm] at (0,0) {};
  \node[draw, fill=white, inner sep=4pt] at (0,0) {Text on top};
\end{tikzpicture}

Here, the gray node is drawn first and the white node second. The second node therefore covers the center of the gray one. If you want the upper node’s text without an opaque background, omit its fill option; nodes without an explicit fill are normally transparent, so the lower node shows through.

For more complex diagrams, TikZ’s backgrounds library can place drawing commands on a background layer. That controls the order in which paths are drawn; it does not automatically assign different background colors to the parts of a single node.

Common mistakes

  • Using background color instead of fill: For a TikZ node’s shape, fill=<color> is the standard option.
  • Expecting fill to color each split part differently: A single fill gives the shape one color. Use rectangle split part fill for per-part colors.
  • Forgetting the library: rectangle split requires \usetikzlibrary{shapes.multipart}.
  • Confusing text color and background color: text=<color> changes the lettering; fill=<color> changes the node’s shape.
  • Drawing an opaque node over another: A later node can hide an earlier fill. Check the drawing order, or make the upper node transparent if you want the lower color to remain visible.

References

  1. The TikZ and PGF Packages: Manual

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.