Density is defined as the mass per unit volume.
Volume will vary with temperature and so this will vary the density.
As you add thermal energy into an object it will tend to expand as the energy makes the molecules more active. Pushing against each other. This is easier to observe in fluids and gasses. As the same mass now takes up more volume, then density decreases
It is related to why pressure increases, in closed systems, as temperature increases. As the system is closed the material can't expand in volume and so the increased movement/activity of the molecules increases the pressure.
If the temperature is decreased then density increases.
Regards