Colored headrule - fancyhdr

Do you want to avoid the gap between the header text and the line below the header? You could patch \headrule to change the color of the line:

\documentclass[9pt]{extarticle}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{fancyhdr}
\pagestyle{fancy}

\lhead{lhead text}
\chead{}
\rhead{\fcolorbox{black}{black}{
        \footnotesize\bfseries{\makebox[4em]
        {\color{white}rhead text}}}%
}
\renewcommand{\headrulewidth}{1pt}

\usepackage{xpatch}
\xpretocmd\headrule{\color{blue}}{}{\PatchFailed}

\begin{document}
    text
\end{document}

enter image description here


You can use, \setlength{headsep} to tune the distance between the header and the text.

\documentclass[9pt]{extarticle}

\usepackage[T1]{fontenc}
\usepackage{xcolor}
\usepackage{fancyhdr}
\pagestyle{fancy}

\lhead{lhead text}
\chead{}
\rhead{\fcolorbox{black}{black}{
        \footnotesize\bfseries{\makebox[4em]
        {\color{white}rhead text}}}%
}
\renewcommand{\headrulewidth}{1pt}

\renewcommand{\headrule}{\hbox to\headwidth{\color{blue}\leaders\hrule height \headrulewidth\hfill}}

%% set the gap between header and text
\setlength{\headsep}{0.1in}

\begin{document}
    text
\end{document}

Hope it helps.

Romain