Broken rainbows

In 1801 William Wollaston observed a rainbow in close detail and noticed tiny dark lines in the visible spectrum. Here is an experimental setup for the prismatic observation of the solar spectrum by Wollaston:
Reproduced from Philosophical Transactions of the Royal Society of London, vol. 92 (1802), p. 380
In 1817 Fraunhofer took an even closer look at the Sun’s spectrum (IR, UV and visible) by expanding the spectrum onto a large wall. As a result, he found thousands of slices that were missing.
Image Credit: NASA
These are known as absorption lines or ‘Fraunhofer lines’. But why do they occur?

Colorful gasses

A major advance was made in 1859 by Gustav Kirchhoff and Robert Bunsen that explained why the dark lines occur. When certain chemicals were heated in Bunsen's burner, characteristic bright lines appeared.
Image Credit: Jan Fijalkowski
In some cases these were at exactly the same points in the spectrum as Fraunhofer's dark lines. For example, these are the spectrum lines visible when we heat hydrogen gas. This is known as the Balmer series :
Image Credit: Jan Homann
Light passing through cooler atmospheric hydrogen will show absorption lines in the exact same position. For example imagine sunlight passing through a tube of hydrogen gas and then split using a prism:
Hydrogen absorption and emission lines in the visible spectrum
Emission lines refer to the fact that glowing hot gas emits lines of light, whereas absorption lines refer to the tendency of cool atmospheric gas to absorb the same lines of light. When light passes through gas in the atmosphere some of the light at particular wavelengths is scattered resulting in darker bands. These lines came to be known as ‘spectral lines’ and were cataloged by heating common elements until they produced light and measuring the wavelengths emitted.
Below is an interactive illustration. Click on different gasses to see how they absorb different wavelengths of light:
This provides a fingerprint that confirmed the presence of chemical elements at a distance. Astronomers use this fingerprint (which spans the entire spectrum) to determine the composition of distant stars (which are essentially balls of burning gas) as well as the atmosphere of stars and planets (that absorb light passing through their atmospheres).