Short answer
The Prophet ﷺ taught quarantine: do not enter a land where plague has broken out, and do not leave it if you are there. He also said to keep the sick away from the healthy. "There is no contagion" means no disease spreads by itself apart from Allah's will, not that infection is unreal. Following public health measures to protect others is part of Islam.
Taking precautions against infection is permitted and, where needed to prevent harm to others, required. A person who knows they are contagious must avoid harming others. Disease spreads only by Allah's decree, and precautions do not contradict trust in Him.
The four schools agree on this.
Explanation
The Prophet ﷺ said: if you hear of plague in a land, do not enter it, and if it breaks out in a land where you are, do not leave it. Umar followed this when he turned back from Syria during the plague of Amwas. He also said a sick animal should not be brought among healthy ones, and "Flee from the leper as you flee from a lion."
The same Prophet ﷺ said, "There is no contagion and no bad omen." When a Bedouin pointed out that healthy camels catch mange from a sick one, he asked, "Then who infected the first?" Scholars explain that he was rejecting the pre-Islamic belief that disease passes on by its own power. Infection is a real means, but it works only when Allah wills, which is why not everyone exposed falls ill. So the hadith on contagion and the hadith on quarantine fit together.
In practice this means:
- stay home or isolate when you are contagious, and follow public health advice
- do not go to the masjid with an infectious illness; illness is an excuse for missing the congregation
- vaccines and masks are permitted means (see are vaccines haram)
- do not hide an infection that could harm others, since harming others is forbidden
There is comfort too. The Prophet ﷺ said that death by plague is martyrdom for every Muslim. A believer takes every precaution, follows a qualified doctor's advice and health authorities, and leaves the outcome to Allah.
Questions about closing masjids or changing the congregation during an epidemic depend on local conditions and are for scholars and the masjid's imam to decide.
The evidence
Quran, Surah At-Tawba 9:51
قُل لَّن يُصِيبَنَآ إِلَّا مَا كَتَبَ ٱللَّهُ لَنَا هُوَ مَوْلَىٰنَا ۚ وَعَلَى ٱللَّهِ فَلْيَتَوَكَّلِ ٱلْمُؤْمِنُونَ ﴿٥١﴾
Say, "Never will we be struck except by what Allah has decreed for us; He is our protector." And upon Allah let the believers rely.
Disease reaches a person only by Allah's decree, alongside the means.
Quran, Surah Al-Baqara 2:195
وَأَنفِقُوا۟ فِى سَبِيلِ ٱللَّهِ وَلَا تُلْقُوا۟ بِأَيْدِيكُمْ إِلَى ٱلتَّهْلُكَةِ ۛ وَأَحْسِنُوٓا۟ ۛ إِنَّ ٱللَّهَ يُحِبُّ ٱلْمُحْسِنِينَ ﴿١٩٥﴾
And spend in the way of Allah and do not throw [yourselves] with your [own] hands into destruction [by refraining]. And do good; indeed, Allah loves the doers of good.
Avoiding needless exposure to danger is required.
Sahih al-Bukhari 5728
Sahih... إذا سمعتم بالطاعون بأرض فلا تدخلوها ...
"If you hear of plague in a land, do not enter it, and if it breaks out where you are, do not leave it."
The basis of quarantine.
Sahih al-Bukhari 5707
Sahih... وفر من المجذوم كما تفر من الأسد ...
"Flee from the leper as you would flee from a lion."
Keeping a distance from contagious illness is taught.
Sahih al-Bukhari 5771
Sahih... لا يوردن ممرض على مصح ...
"Let the owner of sick animals not bring them to the owner of healthy ones."
Separating the sick from the healthy.
Sahih al-Bukhari 5717
Sahih... فمن أعدى الأول ...
"Then who infected the first?"
Disease spreads only by Allah's will, not by its own power.
Sahih al-Bukhari 2830
Sahih... الطاعون شهادة لكل مسلم ...
"Plague is martyrdom for every Muslim."
A believer who dies in an epidemic has a great reward.
Where the ruling is recorded
- al-Durr al-Mukhtar, Kitab al-Salah, Bab al-Imama, on excuses for missing the congregation. Illness is an excuse for missing the congregation.
- al-Ashbah wa al-Naza'ir (Ibn Nujaym), The rule that harm is to be removed. Preventing harm to others is a general principle of the Shariah.
Checked by our team. For a more accurate answer to your own situation, consult a scholar. Quran translation by Sahih International. Hadith Arabic from the standard editions of the six books and the Muwatta, with gradings as recorded there; the English is our own rendering. Spotted a mistake? Tell us.