In an expanding universe, the distance between galaxies can indeed change over time, while the distance between stars and planets within a galaxy remains relatively constant. This difference arises from the nature of the expansion of the universe and the gravitational interactions within galaxies.
The concept of an expanding universe is based on the observations made by astronomers, which indicate that galaxies are moving away from each other on average. This observation led to the development of the Big Bang theory, which suggests that the universe began from a highly dense and hot state and has been expanding ever since.
On large scales, the expansion of the universe can be described by the Hubble's law, which states that the recessional velocity of a galaxy is directly proportional to its distance from us. This means that the farther away a galaxy is, the faster it appears to be moving away from us. The expansion of the universe itself is not due to galaxies moving through space, but rather it is the fabric of space itself that is expanding.
On the other hand, within a galaxy, such as our Milky Way, the gravitational interactions between stars and other celestial bodies dominate. Gravity acts as a binding force within galaxies, counteracting the expansion of the universe on small scales. The gravitational attraction between stars and planets holds them together within a galaxy, resulting in relatively stable distances between them.
So, while the expansion of the universe can cause galaxies to move away from each other, the gravitational forces within galaxies maintain the relatively constant distances between stars and planets.