# Centering table data under right-aligned tabularx header

I'm trying to align the following table so that the "21" is centered under its header.

\begin{table}[h]
\centering
\begin{tabularx}{1.09\textwidth}{|cll>{\raggedleft\arraybackslash}X|}
\hline
\textbf{ID}     & \textbf{Severity}     & \textbf{Vulnerability}       &
\textbf{Occurrences} \\
\hline
111111         & Lorem                 & Lorem ipsum dolor sit amet & 21 \\
\hline
\end{tabularx}
\caption{example}
\label{table:example}
\end{table}


I can use \centering instead of \raggedleft for the last column to center the numbers under the header but then the header ends up too much to the left, I want the header to be like in the above picture.

Is there anyway I can get the combination of these two? The header aligned to the right like in the first picture but the number centered under the header like in the second picture.

• Welcome to TeX SX! Have you any reason for a tabularx which flows into the margins? – Bernard Mar 18 at 10:21
• @Bernard The reason i'm using tabularx is to get the same width as my widest table – antmo Mar 18 at 10:27

I would use the X type colum for the 3rd instead of the 4th cell: \begin{tabularx}{1.09\textwidth}{|clXc|}. With \makecell[r]{\textbf{Occurrences}} you can then right align the header:

\documentclass{article}

\usepackage{tabularx}
\usepackage{makecell}

\begin{document}
\begin{table}[h]
\centering
\begin{tabularx}{1.09\textwidth}{|clXc|}
\hline
\textbf{ID}     & \textbf{Severity}     & \textbf{Vulnerability}       &
\makecell[r]{\textbf{Occurrences}} \\
\hline
111111         & Lorem                 & Lorem ipsum dolor sit amet & 21 \\
\hline
\end{tabularx}
\caption{example}
\label{table:example}
\end{table}
\end{document}

• Maybe using \thead for all column heads would simplify the code? – Bernard Mar 18 at 10:19
• Thank you, that was an easy solution that gives the right look! – antmo Mar 18 at 10:21