Shaving cream is mainly cleaning soap and water. It’s put right into a can together with compressed butane fuel. With out the fuel, all you’ve got is soapy liquid.
When the valve is pressed, a few of the fuel mixes with the cleaning soap and water, escapes, and expands to make foam.
The filling course of is rather like that used to fill air within the tube of a tire, or perhaps a balloon.
So long as the strain is maintained in a closed system and the fuel from a pressurised supply just isn’t allowed to flee, it takes up a really small quantity of area.
The truth that any pressurised fuel expands when the strain is launched implies that a small amount of cleaning soap and water could make an entire lot of foamy bubbles.