A spiral track acts like an optical ruler
A CD stores data along one long spiral, packed into closely spaced tracks. Those tiny grooves also form a diffraction grating: a surface pattern that redirects light differently depending on its wavelength. Wavelength is the distance between successive peaks of a light wave; different wavelengths correspond to different colors.
When white light reflects from the grooved surface, waves from neighboring parts of the pattern overlap. In some directions, waves of a particular wavelength reinforce one another, while other colors reinforce in different directions. The result is not simply white light tinted by the disc: its colors have been sorted by where they travel.
Why the rainbow moves when the disc tilts
The direction in which a color is reinforced depends on the angle at which light strikes the disc and the angle at which it reaches your eye. Tilting the CD changes that geometry, so a different set of wavelengths is directed toward you. The rainbow appears to slide across the shiny surface even though the grooves themselves have not changed.
This is the same basic principle used by diffraction gratings in scientific instruments to separate light into its component colors. A prism separates colors mainly because its material bends each wavelength by a different amount; a CD separates them through interference from its repeating surface pattern. A disc made to hold music or files therefore doubles as a compact demonstration of how the structure of a surface can sort light.





