The **colortbl package is the machinery for putting background color** behind a table’s cells, rows, and columns, and for coloring its rules. Loading it takes one line: \usepackage[table]{xcolor}. The **table option** of xcolor pulls colortbl in automatically, so you get both halves at once — color names from xcolor, table shading from colortbl. The core is three commands — **\rowcolor (color a row), \columncolor (color a column), and \cellcolor (color a cell) — plus \rowcolors for automatic zebra striping and \arrayrulecolor for colored rules. This page covers all five, and the order of precedence among them: cell beats row beats column**.
Loading it — the table option of xcolor
colortbl can be loaded on its own, but the recommended entry point is **xcolor**. Loading xcolor **with the table option** turns on both xcolor (color names, mixing, model conversion) and colortbl (shading inside tables) at once. colortbl itself depends on two packages internally — array and color — and loading it through xcolor brings those in for you.
\usepackage[table]{xcolor}That single line enables everything described below — \rowcolor, \columncolor, \cellcolor, \rowcolors, and \arrayrulecolor. Inside beamer the spelling differs slightly: pass it as a class option, \documentclass[xcolor=table]{beamer}. Colors can be plain names like gray and red, or any xcolor expression such as **gray!20** (20% intensity) or red!30!yellow (30% red mixed into yellow). For soft backgrounds, a diluted name!20 is the usual choice.
Three commands: row, column, cell
The core is three commands. **\rowcolor{color} shades an entire row, \columncolor{color} an entire column, and \cellcolor{color}** a single cell. All three follow the same shape, \columncolor[model]{color}[left overhang][right overhang]: an optional color-model argument before the color, and optional left/right overhang arguments after it. The overhang says how far the colored panel extends past the column on each side; omitted, it defaults to \tabcolsep (or \arraycolsep in array).
Note that the three go in three different places. \rowcolor goes at the start of the row — before the content of that row’s first cell, i.e. right after the \\. \columncolor goes not in the body but inside the column specification, in the form >{\columncolor{gray!20}} (the >{…} hook from the array package injects material just before every cell of that column). Only \cellcolor is unconstrained: it may appear anywhere inside the cell it applies to.
\begin{tabular}{>{\columncolor{gray!20}}l c r}
\rowcolor{blue!30}
項目 & 数量 & 価格 \\
りんご & 3 & 380 \\
みかん & \cellcolor{yellow!40}5 & 120 \\
\end{tabular}Here column 1 is shaded light gray by >{\columncolor{gray!20}}, the header row is pale blue from \rowcolor{blue!30}, and only the quantity cell of “mikan” is highlighted yellow by \cellcolor{yellow!40}. Note that \rowcolor sits at the start of the header row, while \columncolor lives **inside the >{} of the column spec**.
Precedence — cell over row over column
When all three colors meet in one cell, which wins is well defined. **\cellcolor overrides \rowcolor, which overrides \columncolor — that is, cell over row over column**. So you can layer them naturally: shade a whole column gray (\columncolor), make just the header row blue (\rowcolor), and turn one cell yellow (\cellcolor). The narrower the scope, the higher the priority — exactly as intuition suggests.
Before \cellcolor existed, shading a single cell meant the roundabout \multicolumn{1}{>{\columncolor{…}}c}{…}. That has real drawbacks — the content can stop the coloring from triggering, and you must copy the column alignment by hand (error-prone with p{} columns) — and the package author flatly calls \multicolumn{1} “silly.” To color one cell, just reach for \cellcolor.
Automatic zebra striping — \rowcolors
To shade alternate rows — a zebra stripe — automate it with xcolor’s **\rowcolors{start}{odd-row color}{even-row color} (note the trailing s**; the s-less \rowcolor is a different command). Issued once before the table, it makes subsequent rows alternate colors as specified. The first argument is the row at which coloring begins — use 2 to skip a header row. Either color argument may be left empty, meaning “no color.”
\rowcolors{2}{gray!15}{white}
\begin{tabular}{l r r}
項目 & 数量 & 価格 \\
りんご & 3 & 380 \\
みかん & 5 & 120 \\
ぶどう & 2 & 600 \\
もも & 4 & 450 \\
\end{tabular}This starts striping at row 2, alternating gray!15 (light gray) and white. The header (row 1) is left unshaded; only the data rows alternate. \rowcolors also takes a leading optional argument: \rowcolors[\hline]{2}{…}{…} runs a command such as \hline before each row. And because an explicit **\rowcolor or \multicolumn overrides** the automatic \rowcolors shading, you can still recolor a single row in the middle of a striped run.
| Command | What it colors | Where it goes |
|---|---|---|
\columncolor{c} | A whole column | Inside >{…} in the column spec |
\rowcolor{c} | A whole row | Start of the row (before the first cell) |
\cellcolor{c} | A single cell | Inside that cell (anywhere) |
\rowcolors{n}{a}{b} | Alternating rows (zebra) | Once, before the table |
\arrayrulecolor{c} | Following rules | Outside / start of row / >{} in spec |
Coloring rules — \arrayrulecolor
The color of rules (\hline, \cline, and vertical |) is set with **\arrayrulecolor{color}. It is a global declaration** taking the same arguments as \color, and may be placed outside a table, at the start of a row, or in a >{} in the spec. The catch: given mid-table it affects only rules drawn after that point — vertical rules already declared in the preamble keep their original color. To restore the default, declare \arrayrulecolor{black} again.
The gap between double rules made by || or \hline\hline is left white by default. To color it too, use **\doublerulesepcolor{color}** together with the length \doublerulesep (the gap’s width). The example below makes the rules blue and the double-rule gap yellow.
\setlength\arrayrulewidth{1pt}\arrayrulecolor{blue}
\setlength\doublerulesep{2pt}\doublerulesepcolor{yellow}
\begin{tabular}{||l|c||}
\hline\hline
one & two \\
three & four \\
\hline\hline
\end{tabular}To color one vertical rule by hand, you can also drop !{\color{green}\vline} into the column spec in place of |, without any special command. \arrayrulewidth is the length controlling rule thickness; once colored, a hairline can be hard to see, so thicken it a little if needed.
Using it with booktabs, and caveats
colortbl **works with booktabs**, but the pairing has a quirk. booktabs puts a little space above and below \toprule / \midrule / \bottomrule (\aboverulesep and \belowrulesep), which leaves a white gap between a row’s background color and the rule. To make the fill reach the rule, the common fix is to set those spaces to 0pt and move that amount into \extrarowheight instead.
Another classic issue: **tall content or large type can touch the \hline or the top of a color panel just above it**. For this, colortbl offers the length **\minrowclearance**: setting \setlength\minrowclearance{2pt} adds a little space above tall rows. (Keep it modest, though — if the capital height plus this value exceeds the normal row height, the row spacing turns uneven.)
A word on restraint to close. Most style guides caution against overusing rules and color. Background shading reads best when reserved for a header row you want to stress, or a soft stripe that helps the eye track across. Specifying the colors themselves (names, models, mixing) is covered on the “Colors (xcolor)” page, and rule conventions on the “Rules” page.