The head or neck might settle at an awkward angle causes stiff neck. For an extended period of time while sleeping, which can stretch and stress muscles, ligaments, and joints beyond their normal limits.
Perhaps from rolling over or reacting to a dream. Sudden neck movements might occur while sleeping that can strain or sprain the neck.
Some injuries that happen while awake, such as whiplash. May take many hours before pain and stiffness develop later while sleeping.
Most commonly, the root cause of a stiff neck is a neck strain, which could be due to a muscle strain
Applying ice shortly after a neck strain may help limit the swelling. Ice applications tend to be best for 10 to 20 minutes at a time.
Walking and moving around are still encouraged, because full bed rest may cause the stiff neck and pain to last longer.