Atoms emit and absorb light photons of certain energies which correspond to the energies between the energy levels of the atom. These energy levels are unique to each element. By analysing light from an object we can deduce the elements present in the object.
We can analyse light from stars by passing it through a spectrograph. This instrument consists of :
A collimating lens to focus the light into a parallel beam.
A diffraction or transmission grating. Light will be diffracted to an extent which depends of their wavelength.
A travelling lens which focuses the diffracted light onto a film. As the lens moves, different wavelengths are focused onto the film.
Light from a star for example passes through a slit and is focussed into a parallel beam. It hits a diffraction grating. In general the light will consist of a range of wavelengths and will spread out to form rainbow fringes after passing through the diffraction grating. When the light falls on the film and exposes it, certain wavelengths will be weak or absent, producing dark fringes corresponding to the energy differences between energy levels in certain atoms. The fringes are dark because light leaving a star has to pass through the outer layer of a star. It is absorbed by some of the atoms there and reradiated in all directions. The patterns of dark lines allows elements in stars to be identified.