The solution presented in How to highlight specific entries in a tocloft-formatted ToC did not work for colored background. There is a problem when using color because the colored rectangles are always drawn in front of the text.

I added also a color change from section to section. Here's a minimal script to generate the problem:

\documentclass{article}
\usepackage{tocloft}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\newcounter{seccntr}
\setcounter{seccntr}{-1}

\newcommand*{\hnode}[1]{%
\tikz[remember picture] \node[minimum size=0pt,inner sep=0pt,outer sep=4.5pt] (#1) {};}
% create a node at the beginning of the section entry
\renewcommand{\cftsecfont}{\hnode{P1} \bfseries\Large}
\renewcommand{\cftsecpagefont}{\bfseries}
% create a node at the end of the section number and draw the gray box
\renewcommand{\cftsecafterpnum}{%
\stepcounter{seccntr} %
\ifcase\value{seccntr}%
\hnode{P2} \tikz[remember picture,overlay] \draw (P1.north west)  [line width={17pt}, red,opacity=0.3] -- (P2.north east); %----- 0 --
\or  \hnode{P2}\tikz[remember picture,overlay] \draw (P1.north west)  [line width={17pt}, green,opacity=0.4] -- (P2.north east);%---- 1 --
\or  \hnode{P2}\tikz[remember picture,overlay]  \draw (P1.north west)  [line width={17pt}, yellow,opacity=0.5] -- (P2.north east);%--- 2 --
\or  \hnode{P2}\tikz[remember picture,overlay]  \draw (P1.north west)  [line width={17pt}, blue,opacity=0.6] -- (P2.north east);%---- 3 --
\or  \hnode{P2}\tikz[remember picture,overlay]  \draw (P1.north west)  [line width={17pt}, orange,opacity=0.7] -- (P2.north east);%-- default
\else  \hnode{P2}\tikz[remember picture,overlay] \draw (P1.north west)  [line width={17pt}, gray,opacity=0.8] -- (P2.north east);%-- default
\fi  %
} %

\begin{document}
\tableofcontents

\section{First Section}
\subsection{A subsubsection}
\subsection{A subsubsection}
\section{Second Section}
\subsection{A subsubsection}
\end{document}


Can anyone help to put the colored boxes in the background?

\begin{pgfonlayer}{background}\tikz .... \end{pgfonlayer}


didn't help, because it is just the background of the picture.

-
Your code looks like it works to me: i.stack.imgur.com/7gkSS.png. There seem to be no colored background on top of text. –  Martin Scharrer Nov 4 '11 at 8:35
Same here. After three runs of your code I obtain same result than Martin. –  Ignasi Nov 4 '11 at 8:44
thx for testing. but the colored box is in front of the text, should be in the background. if you try to increase the opacity to 1 you'll see the text is really behind the boxes. any possibility to bring the text in front. thx –  J. Almer Nov 4 '11 at 10:54
As far as I know, you can't draw 'behind' something. The last thing added will be the frontmost content. The problem here is that you don't know exactly what you want to draw until after the text is placed. The node P2 is placed after the page number, this can only be done after the page number itself has been placed, otherwise we don't know where it is. By the way, you have a spurious space in your first case, causing the right hand side of the rectangles to not align properly, this can be seen in the image in @MartinScharrer's comment. –  Roelof Spijker Nov 4 '11 at 12:20

What you can do is to place the rectangle before the text. However, this way we can't use the placing of the page number and thus we have to determine the end position another way. What I have done is simply start the line at the same point, only draw it \textwidth + 1ex long. This looks fairly well and causes the text to be drawn later and thus on top of the line. I don't know how well this generalizes, if you change the position of the pagenumber or indent the start further it will probably break. This is the code:

\documentclass{article}
\usepackage{tocloft}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage{tikz}
\usetikzlibrary{backgrounds}
\usetikzlibrary{calc}

\newcounter{seccntr}
\setcounter{seccntr}{-1}

\newcommand*{\hnode}[1]{%
\tikz[remember picture] \node[minimum size=0pt,inner sep=0pt,outer sep=4.5pt] (#1) {};}
% create a node at the beginning of the section entry
\renewcommand{\cftsecfont}{\hnode{P1}\bfseries\Large
\stepcounter{seccntr}%
\ifcase\value{seccntr}%
\tikz[remember picture,overlay] \draw (P1.north west)  [line width={17pt}, red,opacity=0.3] -- ++($(\textwidth,0) + (1ex,0)$);%----- 0 --
\or\tikz[remember picture,overlay] \draw (P1.north west)  [line width={17pt}, green,opacity=0.4] -- ++($(\textwidth,0) + (1ex,0)$);%---- 1 --
\or\tikz[remember picture,overlay]  \draw (P1.north west)  [line width={17pt}, yellow,opacity=1] -- ++($(\textwidth,0) + (1ex,0)$);%--- 2 --
\or\tikz[remember picture,overlay]  \draw (P1.north west)  [line width={17pt}, blue,opacity=0.6] -- ++($(\textwidth,0) + (1ex,0)$);%---- 3 --
\or\tikz[remember picture,overlay]  \draw (P1.north west)  [line width={17pt}, orange,opacity=0.7] -- ++($(\textwidth,0) + (1ex,0)$);%-- default
\else\tikz[remember picture,overlay] \draw (P1.north west)  [line width={17pt}, gray,opacity=0.8] -- ++($(\textwidth,0) + (1ex,0)$);%-- default
\fi  %
}
\renewcommand{\cftsecpagefont}{\bfseries}

\begin{document}
\tableofcontents

\section{First Section}
\subsection{A subsubsection}
\subsection{A subsubsection}
\section{Second Section}
\subsection{A subsubsection}
\lipsum[1-100]
\section{Third Section}
\end{document}


And this is the relevant output (note the opacity of 1 on the third section):

The lipsum is there to check if longer page numbers don't cause issues.

-
thx for the solution, works perfectly! –  J. Almer Nov 5 '11 at 9:07