Minecraft wiki entry for Spider, associated text: In normal conditions, spiders actually float above the ground, but this is often not noticeable due to the difference between the player's height and a spider's height.
When a spider wanders inside an enclosed space, it often climbs up a wall until reaching the ceiling, after which it become stuck, unable to exit the climbing state even if it is attacked or becomes hostile to the player.[10]