Both coal and petroleum were formed from biomass (the bodies of living things) acted on by biological and geological processes over a very long time.
(a) Coal: coal is the remains of trees, ferns and other plants that lived millions of years ago. These plants were crushed into the earth, perhaps by earthquakes or volcanic eruptions, and pressed down by layers of earth and rock. Under these conditions they slowly decayed into coal.
(b) Petroleum: oil and gas are the remains of millions of tiny plants and animals that lived in the sea. When they died, their bodies sank to the sea bed and were covered by silt. Bacteria attacked the dead remains and, under the high pressure there, turned them into oil and gas. The silt was slowly compressed into rock, and the oil and gas seeped into its porous parts and were trapped there, like water in a sponge.
(c) They are called fossil fuels because, like fossils, they are formed from the remains of living things that died millions of years ago.
Their formation took millions of years under special conditions of burial, heat and pressure. We are burning them far faster than this, within years, so the stock used up cannot be replaced on any human timescale; the supply is limited and will one day run out.
