Density is the ratio of a substance's mass to its volume and can be expressed mathematically as
Density results from the number of protons, neutrons, and electrons in the atoms that make up the substances and how closely they are arranged to each other either in the substance.
As the temperature of matter increases, its molecules move more rapidly and get farther apart. Solids, liquids, and gases expand as temperature increases.
Since the volume goes up, but the mass stays the same, density, therefore, must decrease as a function of increasing temperature.
When matter changes from a solid to a liquid, in most cases, the liquid will take up more space and thus be less dense. Likewise, when a liquid becomes a gas, the molecules spread out even more, and the density goes down even more.
Since mass stays the same, but volume increases, density must decrease as temperature goes up and as matter changes from solids to liquids and to gasses.