To understand this, you first need to understand in more detail what transparency is. Even in the air there are particles of moisture and dust that refract the light, but their effect is so insignificant that we simply do not notice it.
Foam can be called transparent and opaque at the same time. In essence, foam is a collection of bubbles. Each bubble can be considered conditionally transparent. No matter what color the liquid the foam is made of, the diameter of the bubble wall is so thin that there are practically no coloring particles in it. Each individual bubble can also be considered as a small lens, and if you look closely, you can see rainbow spots on its walls. This is the interference of light.
The smaller the bubbles, the whiter the foam.
Foam consists of many thousands of bubbles. Each of them passes light through itself, but distorts the beam, acting like a magnifying glass. In addition, the bubble "works" and a mirror, reflecting all the rays falling into it. As a result, millions of light rays passing through the foam lose their original direction so much that the overall transparency is lost. In addition, due to the above-described interference effect, the light not only refracts, but also during its travel through the numerous bubbles of the foamy abyss many times decomposes into the entire spectrum of visible colors (from red to purple).
Why is the foam white?
It remains to understand why only the light wave of only the white spectrum reaches the human eye.
Everything is simple here. The human eye does not see the object itself, but those light rays that are reflected from it. Since light rays directed at the foam randomly scatter, the majority of the same rays that fall into the foam are reflected. Since sunlight or an electric light replacing it is most often white, the observer sees a foam of the same color.
This can be confirmed with a simple experiment. If you take any foam (it is most convenient to use ready-made shaving foam), bring it into a dark room and shine with a red light, the foam will be predictably red. When lit with a green light, the foam will turn green.
Foam will always reflect the color of those rays that fall on it.