1. Muscle strain
Any activity that places your neck in an awkward position for an extended amount of time could cause neck muscles to become fatigued and spasm. For example, holding your phone against your shoulder while you talk, sleeping with your neck at an awkward angle, carrying a heavy bag on one shoulder, or having to look too far downward or upward to view your computer screen can all cause neck stiffness.
2. Cervical spine disorders
The cervical spine encompasses all of the discs, bones, joints, muscles, and nerves in your neck. The spinal cord also runs through the centre of the vertebrae (bones) in your cervical spine.
A stiff neck caused by an infection is rare compared to the other causes above, but it is a serious medical condition. For example, meningitis can cause a stiff neck by infecting and inflaming the meninges, which are the protective membranes surrounding the spinal cord and brain. https://www.nhs.uk/conditions/neck-pain-and-stiff-neck/