Why did the matter in the early universe not stick together due to gravity?

Atoms themselves didn't form until well after the Big Bang. The Big Bang Nucleosynthesis (BBN) is when most nuclei formed and that happened somewhere between 10 seconds and 20 minutes after the Big Bang (that's a long time relative to how quickly everything was happening back then). That would be when ions formed, neutral atoms didn't make an appearance until a few hundred thousand years later. Matter and other things with mass didn't form until after inflation ended. By that time, the universe had expanded so much that only the smallest regions of it were able to see and interact with each other. Because it expanded so rapidly, the range over which gravity could act was extremely small. When BBN started, at around 10 seconds, gravity could only influence things that were about as far away as 10 times the distance to the Moon. If Earth was around back then, it wouldn't even know of the Sun's existence, much less feel a gravitational pull towards it. By the time BBN ended, gravity's range would be enough to allow all the planets out to Mars to orbit the Sun. At this point, the universe was still expanding very fast as well.

So the reason why matter was able to avoid being all in one enormous clump is that it simply didn't feel the gravitational pull of other matter until it was too late to form a clump.

To be fair, even if all matter felt the pull of all other matter right from the beginning, the matter in the universe was distributed fairly homogeneously. This means there wouldn't be a net pull in any direction. There were inhomogeneities; perturbations in the density of matter. These would naturally tend to fall together with the smaller scale over-densities clumping first.

In reality, because of how fast the universe was expanding and how long it took for gravity to extend its influence over neighbouring regions, what we see is a combination of what I described above. The limited range of gravity and the rapid expansion prevented matter from any extensive falling together in very early times. As expansion slowed and gravity's reach extended outwards, we observe that matter formed structures; first on the smallest scales and later on larger scales. These structures were seeded by the over-densities.

Tags:

Cosmology