Why do Jupiter and Saturn contain mostly hydrogen and helium?
Jupiter and Saturn contain mostly hydrogen and helium because they formed in the outer regions of the solar nebula, far enough from the Sun that temperatures were low enough for water to condense into ice.1
In these cooler regions, accreting bodies could gather significantly more solid material than those forming closer to the Sun. Once a forming planet reached approximately 10 times the mass of Earth, its gravitational field became strong enough to capture and retain large amounts of hydrogen and helium directly from the surrounding nebular gas. Because hydrogen and helium are the most abundant elements in the universe, these planets were able to grow into massive, gas-rich bodies.12
While the inner planets remained small and rocky due to higher temperatures that prevented the condensation of volatile substances, Jupiter and Saturn successfully underwent this gravitational collapse, accumulating their vast gaseous envelopes during the early stages of the solar system's formation.123