During ancient times and the Middle Ages stars were thought to be made of an ethereal element different from matter on Earth. Their actual composition did not become known until the invention of the spectroscope in the 19th century. Through the refraction of light by a prism or through its diffraction by a diffraction grating, the light from a source is spread out into its different visual wavelengths, from red to blue. This is known as its spectrum. The spectra of the Sun and stars exhibited bright and dark lines called Fraunhofer lines. These were shown to be caused by elements emitting or absorbing light at specific wavelengths. Because each element emits or absorbs light only at specific wavelengths, the chemical composition of stars can be determined.

In this way the spectroscope demonstrated that the gases in the Sun and stars are those of common elements such as hydrogen, helium, iron, and calcium, but at temperatures of several thousand degrees. It was found that the average star's atmosphere consists mostly of hydrogen (87%) and helium (10%), with all other elements making up about 3%. Helium was actually first discovered in the Sun's spectrum.

Radiation in a spectrum line originates only from the uppermost, coolest layers of a star's atmosphere, while radiation in the rest of the spectrum originates from much deeper and hotter layers. For this reason the lines appear dark against a continuous background. A star may have a vast tenuous envelope, which by itself would give a bright-line spectrum. When this spectrum is superposed on that of the star, some of these bright lines may fill in the dark lines of the stellar spectrum. This occurs in the stars called novas.

At first, visual estimates of the strengths of spectral lines were used to estimate the amounts of the elements present in the Sun and a few stars, based on an analysis of the lines produced by a laboratory light source. When photographic emulsions came into use, the spectroscope became the spectrograph, in which a photographic film or plate replaces the human eye. During the first half of the 20th century spectrographs were used on telescopes to observe thousands of stars, the intensities of the lines being measured from the blackness of the film or plate. Most recently photoelectric detectors are used to scan the spectrum in a spectrophotometer. Stellar spectra can also be measured by other techniques.

Although the ultraviolet, visual, and infrared parts of a star's spectrum can be measured in this way, other techniques must be used, above the atmosphere, to measure the shorter wavelength spectra of X-ray stars and gamma-ray stars. Instead of gratings and prisms, various combinations of filters and detectors are used to measure portions of the X-ray and gamma-ray spectra. At the other extreme - that is, very long wavelengths - radio spectra of stars and other radio sources are measured by "tuning" a radio telescope to different frequencies. A radio telescope - the largest is more than 305 m (1,000 ft) across - is like a giant optical reflector with a radio amplifier at the focus. Radio spectra are much more accurate than optical spectra. Multiple radio telescopes, placed thousands of kilometers apart, can determine the position of a radio-emitting star as accurately as an optical telescope can, to better than 0.1 second of arc.