What do you mean by “can’t be gravity alone”? That’s the only real attractive force on cosmic scales.
And you’re getting the timing wrong. The gas bunched up first, then formed stars. It’s believed that they form early in a galaxy’s life, when there’s lots of free gas, which is spread too thinly to completely collapse, but still close enough to become gravitationally bound together.
Latest Answers