[Tex/LaTex] Why does ref link to section instead of label?

cross-referencinggraphics

I want to refer to a figure within a section (say 1.2), using \ref{fig:xyz}

Section:

\section{XYZ}

Figure:

\begin{figure}
    \includegraphics[width=1\linewidth]{xyz.jpg}
    \label{fig:xyz}
    \caption[XYZ]
\end{figure}

When I add \ref{label:001} to my document, it does not link to the figure, but to the section (1.2).

What can I get it right?

Best Answer

You have to put \label after \caption - and everything will work.

Thus, your figure will now be:
\begin{figure}
    \includegraphics[width=1\linewidth]{xyz.jpg}
    \caption[XYZ]
    \label{fig:xyz}
\end{figure}