A nebula begins as a dense cloud of gas and dust that contracts due to gravity. As pressure and temperature increase, it forms a protostar. When hot enough, fusion reactions begin and a main sequence star is born. Over time, a star will expand into a red giant as fusion outweighs gravity. After swelling to many times its size, a red giant cools and its outer layers form a planetary nebula surrounding the core which shrinks into a white dwarf. For very massive stars, a supernova explosion marks the end, briefly outshining the galaxy before leaving behind a crushed core such as a neutron star or black hole.