A gas has a property to greatly absorb the light of the specific wavelength each. The absorption band is unique to each gas.
You can detect gas density using this property. (NDIR method) For example, in the case of CO2, There is a big absorption band
for infrared rays of the wavelength of 4.25um.
When CO2 density of space is high, an ingredient of the neighborhood of 4.25um greatly damps in the broadband infrared rays
which goes out of the infrared radiation source.
We can detect the CO2 density by comparing this with the ingredient of the reference wavelength range not to be absorbed by CO2