TikZ: How to position a node relatively to it's edge?
\documentclass[tikz,border=5mm]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node [draw,minimum width=3cm,minimum height=2cm] (A) {A};
\node [above=2cm of A,anchor=south west,draw,minimum width=3cm,minimum height=2cm] (B) {B};
\end{tikzpicture}
\end{document}
\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\node[draw, rectangle, minimum width=3cm] (B) {B};
\node[draw, rectangle, minimum width=2cm, anchor=east]
(A) at ($(B.center)+(0, 2cm)$) {A}; % 2cm between centers of nodes
\end{tikzpicture}
\end{document}
This is one way for doing this, place node A
at (B.north)
from anchor=south east
, then shift A
by 2cm
upward.
\documentclass[tikz,border=5pt]{standalone}
\begin{document}
\begin{tikzpicture}[minimum width=2cm]
\node(B) [draw] {B};
\node(A) at (B.north) [draw,anchor=south east,yshift=2cm] {A};
\end{tikzpicture}
\end{document}