All grammarA2
Two-way prepositions
The rule
After in, an, auf, über, unter, vor, hinter, neben and zwischen, use the Dativ when you say where something is and the Akkusativ when you say where it goes.
Wo? asks where something is. The answer takes the Dativ: auf dem Tisch, an der Wand.
Wohin? asks where something goes, with a movement to a new place. The answer takes the Akkusativ: auf den Tisch, an die Wand.
Short forms: in dem is im, in das is ins, an dem is am.
Examples
Die Lampe ist über dem Tisch.
The lamp is above the table.
Das Buch ist auf dem Tisch.
The book is on the table.
Wir stellen das Sofa an die Wand.
We put the sofa against the wall.
Wir stellen das Regal ins Schlafzimmer.
We put the shelf in the bedroom.