How does fog work?

Where I live, sometimes in the morning there's heavy fog just as the sun is coming up but after the sun somes up it all goes away. Why does it get foggy right before the sun comes up and why does the fog disappear shortly after?
Thanks.

Water vapur is held in the air. Water vapour is invisible but when it condenses into water droplets like clouds or 'steam' from a kettle you can easily see it.
The amount of water vapour that air can hold depends on the temperature.
At night as the air is cool, all the water vapour cannot be held n the air and it condenses out into fog/clouds. As the air heats up these droplets evaporate back into vapur and are absorbed by the air - as vapour they are invisible.

The same amount off water (roughly) is present it's just that as widely spread individual water vapour molecules as a gas you can't see it, condensed into liquid water droplets in a fog it reflects and scatters light so you can.

It's generally cooler at night than in the morning, so the liquid water settles to the ground as dew. With the sun up, evaporation starts. A lot of the water changes back and forth between the liquid and vapour states as it ascends, which is why you see mist rising from the ground in the morning.

The only difference is in height above the ground. Both are composed of water droplets, water vapour, and sometimes ice crystals suspended in air. A lot of clouds are far more dynamic than fog simply because of the air currents at higher altitudes. Clouds will generally be colder as well, also due to altitude.

edit: I don't know where you guys live. What's really cool here is to see a couple of mountains in the Rockies engulfed in clouds, then visit one of the towns up there and find it foggy from their perspective.