Temperature affects density primarily through the expansion of materials when heated, leading to an increase in volume while mass remains constant, resulting in decreased density. In constrained environments, such as gases in a metal tank, temperature changes can alter pressure without affecting density. Notably, water behaves differently; when it freezes, it expands, causing its density to decrease, which is why ice floats on water. This phenomenon highlights the unique properties of water compared to most substances. Understanding these principles is essential in fields like physics and engineering.