Tx · LaTeX & TikZ

How to Position a tcolorbox Inside an Itemize List

Published By the 9to5Science editorial team

At a glance

  1. 1. Start the list item

    Put the tcolorbox inside the item’s content, after \item.

  2. 2. Use the local width

    Set width=\linewidth so the box fits the space available inside the list.

  3. 3. Adjust the spacing

    Use before skip and after skip for vertical gaps; left and right change padding inside the frame.

  4. 4. Check nested lists

    Each list level has a narrower \linewidth, so the same width setting adapts automatically.

A tcolorbox inside an itemize list is positioned relative to the list’s available text area. In most cases, the simplest fix is to put the box in the item’s body and set width=\linewidth. This makes the box fit the current list level instead of using the full page width.

The box’s position, its width, and the padding inside its frame are separate settings. Change the one that matches the problem rather than adding manual horizontal spaces.

A minimal working example

\documentclass{article}
\usepackage{tcolorbox}

\begin{document}

\begin{itemize}
  \item A regular list item.
  \item
    \begin{tcolorbox}[
      width=\linewidth,
      colback=blue!5,
      colframe=blue!60!black,
      before skip=0pt,
      after skip=0pt
    ]
      This box occupies the available width of this list item.
    \end{tcolorbox}
  \item Another regular list item.
\end{itemize}

\end{document}

The example needs only the tcolorbox package. The width=\linewidth option is the key: inside a list, \linewidth is the line width available at that point in the document. It is narrower than the page text width because the list reserves space for its indentation and label.

The before skip and after skip options control vertical space before and after the box. Set them to 0pt when you want to remove the box’s own extra vertical gap. The colback and colframe options are visual styling, not positioning controls.

Aligning the box and its contents

A list item’s text starts after the item label. A box placed in the item body normally aligns with that text area, not with the page margin. Its internal text is inset from the frame by the box padding. To change that inset, use left and right:

\begin{tcolorbox}[
  width=\linewidth,
  left=2mm,
  right=2mm
]
  Text with smaller horizontal padding inside the frame.
\end{tcolorbox}

These options move the text inside the box; they do not move the box’s outside edge. If the box seems too far right, first check whether you want it aligned with the item text or with the list’s outer edge. Those are different alignments. Avoid compensating with \hspace until you have checked the list width and the intended alignment.

For a box that is itself the list item and should have no bullet, use an empty label:

\begin{itemize}
  \item[]
    \begin{tcolorbox}[width=\linewidth]
      A box with no item label.
    \end{tcolorbox}
\end{itemize}

Nested lists

The same \linewidth setting works in nested lists. Each level has less available width, and \linewidth changes accordingly. For example:

\begin{itemize}
  \item Outer item
    \begin{itemize}
      \item
        \begin{tcolorbox}[width=\linewidth]
          This box fits the inner list, not the full page width.
        \end{tcolorbox}
    \end{itemize}
\end{itemize}

Using \textwidth instead would request the main text-block width, which is usually too wide inside a list. That can make the box extend past the list’s right edge.

Common positioning mistakes

  • Using \textwidth for a list-width box: Use \linewidth when the box should fit its current surrounding area.
  • Changing left to fix the outside alignment: left changes the padding between the frame and its contents. It does not shift the frame left.
  • Confusing vertical gaps with horizontal alignment: Use before skip and after skip for vertical spacing. They do not affect the box’s left or right edge.
  • Adding manual spaces before the box: A horizontal space may be discarded at a paragraph break or behave differently in a nested list. Fix the width or list margins instead.

If the box must extend beyond the item text area to line up with the list’s outer edge, that requires deliberate adjustment to the box’s horizontal position and width. The exact adjustment depends on the list margins and whether the item label should remain visible; do not simply substitute \textwidth in a nested list.

References

  1. tcolorbox — CTAN package page
  2. LaTeX2e unofficial reference manual

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