The reason for the "issue" is that if you use |-| to draw bars at both ends of a line, these bars are actually arrow tips and these are designed in such a way that the line width will never extend further than the coordinate of the relevant end point of the line.
In contrast, the tick lines are centered at the respective coordinates and the line width extends to both sides of the relevant coordinate. Thus, the tick lines and the lines created as arrow tips are not aligned.
See the following example (zoom in to see exactly what is going on):
\documentclass[border=10pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[ axis lines=middle, xmin=-2, xmax=2, ymin=0, ymax=1, enlargelimits, every x tick/.style={color=black, thick}, ] \fill[red, opacity=0.5] (-1,-1) rectangle (-2,1); \draw[|-|, thick] (axis cs:-1,0.05) -- (axis cs:1,0.05); \end{axis} \end{tikzpicture} \end{document}

To solve this, you could "shorten" the line with the arrow tips by minus half of the line width:
\documentclass[border=10pt]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.18} \begin{document} \begin{tikzpicture} \begin{axis}[ axis lines=middle, xmin=-2, xmax=2, ymin=0, ymax=1, enlargelimits, every x tick/.style={color=black, thick}, ] \fill[red, opacity=0.5] (-1,-1) rectangle (-2,1); \draw[|-|, thick, shorten >=-0.5\pgflinewidth, shorten <=-0.5\pgflinewidth] (axis cs:-1,0.05) -- (axis cs:1,0.05); \end{axis} \end{tikzpicture} \end{document}

axis cs:-1,0.05andaxis cs:1,0.05don’t align precisely with the ticks. I expected them to match exactly without needing to manually adjust or shorten the arrow tips.\draw[thick,red] (axis cs:-1,0.05) -- (axis cs:1,0.05);\draw[thick,red] (axis cs:-1,0.0) -- (axis cs:1,0.0);Try it yourself.\draw[|-|,thick,red] (axis cs:-1,0.0) -- (axis cs:1,0.0);Even here, the vertical bars don’t align exactly with the ticks, which is exactly the issue I’m trying to understand.