Giving headlines a background color spanning across the entire typearea

This is for a section, but you can extend it easily for subsections with a similar scheme:

\documentclass[a4paper]{scrbook}
\usepackage{xcolor,lipsum}
\usepackage{titlesec}

\titleformat{name=\section}[block]
  {\sffamily\large}
  {}
  {0pt}
  {\colorsection}
\titlespacing*{\section}{0pt}{\baselineskip}{\baselineskip}

\newcommand{\colorsection}[1]{%
  \colorbox{blue!20}{\parbox{\dimexpr\textwidth-2\fboxsep}{\thesection\ #1}}}

\begin{document}

\lipsum[1]
\section{This is the title}
\lipsum[2]
\end{document}

If you want white on colored text, just modify the \colorsection command, for example as

\newcommand{\colorsection}[1]{%
  \colorbox{blue}{\parbox{\dimexpr\textwidth-2\fboxsep}{\color{white}\thesection\ #1}}}

enter image description here


Here is a suggestion redefining the new command \sectionlinesformat that is explained in the documentation of version 3.19 or newer:

\documentclass{scrartcl}[2015/09/15]
\usepackage{blindtext}% dummy text
\usepackage{xcolor}
\colorlet{sectioncolor}{orange}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \colorbox{sectioncolor}{%
    \parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
      \raggedsection\color{white}\@hangfrom{#3}{#4}%
}}}
\makeatother
%\renewcommand\raggedsection{\centering}
\begin{document}
  \section{Example section}
  \blindtext
  \section{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \addsec{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \blinddocument
\end{document}

enter image description here

It is also possible to use different colors for sections, subsections and subsubsections:

\documentclass{scrartcl}[2015/09/15]
\usepackage{blindtext}% dummy text
\usepackage{xcolor}
\colorlet{sectioncolor}{orange}
\colorlet{subsectioncolor}{purple}
\colorlet{subsubsectioncolor}{blue!50!black}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \colorbox{#1color}{%
    \parbox[t]{\dimexpr\textwidth-2\fboxsep\relax}{%
      \raggedsection\color{white}\@hangfrom{#3}{#4}%
}}}
\makeatother
%\renewcommand\raggedsection{\centering}
\begin{document}
  \section{Example section}
  \blindtext
  \section{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \addsec{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \blinddocument
\end{document}

enter image description here


The second argument (#2) of \sectionlinesformat is the indent of the section heading that you can set using \RedeclarSectionCommand. Here is an example using an negative indent to enlarge the section heading into the left margin:

\documentclass{scrartcl}[2015/09/15]
\usepackage{blindtext}% dummy text
\usepackage{xcolor}
\colorlet{sectioncolor}{orange}
\colorlet{subsectioncolor}{purple}
\colorlet{subsubsectioncolor}{blue!50!black}
\makeatletter
\renewcommand\sectionlinesformat[4]{%
  \hspace{#2}%
  \colorbox{#1color}{%
    \parbox[t]{\dimexpr\textwidth-2\fboxsep-#2\relax}{%
      \raggedsection\color{white}\@hangfrom{#3}{#4}%
}}}
\makeatother
\RedeclareSectionCommand[indent=-1em]{section}
%\renewcommand\raggedsection{\centering}
\begin{document}
  \section{Example section}
  \blindtext
  \section{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \addsec{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
  \blindtext
  \blinddocument
\end{document}

enter image description here


Here my second try. It is using the same Idea as Sharpie, but doesn't need tikz.

\documentclass{scrartcl}

\usepackage{xcolor}
\usepackage{lipsum}

\setkomafont{section}{\mysection}
\newcommand{\mysection}[1]{%
    \Large\sf\bf%
    \setlength{\fboxsep}{0cm}%already boxed
    \colorbox{orange!80}{%
        \begin{minipage}{\linewidth}%
            \vspace*{2pt}%Space before
            #1
            \vspace*{2pt}%Space after
        \end{minipage}%
    }}

    \begin{document}

        \section{Example section}
        \lipsum[1]

        \section*{A very very very very very very very very very very very very very very very very very very very very very very very very very very very very very very long section}
        \lipsum[1]
\end{document}

result is: result feel free to adopt for subsection etc ;)