Short answer
Yes. Finding out the baby's sex by ultrasound or a blood test is allowed. The verse that Allah knows what is in the wombs refers to complete knowledge of the unborn child, its life, provision and fate, which no machine reaches. What is not allowed is disliking the result, or ending a pregnancy because of the baby's sex.
Learning the sex of the baby through medical tests is permissible (mubah). It does not contradict the ayah about the unseen. Treating a daughter as bad news, or terminating a pregnancy because of the baby's sex, is haram.
The four schools agree on this.
Explanation
The Quran says that Allah has knowledge of the Hour, sends down the rain and knows what is in the wombs. Some people worry that an ultrasound claims this knowledge. Scholars explain that the knowledge Allah keeps to Himself is complete and from before creation: whether the child will live, its lifespan, its provision, its deeds and whether it will be happy or wretched. A hadith describes the angel asking, "My Lord, male or female? Wretched or happy? What is his provision and lifespan?" and Allah alone decrees it.
A scan shows what Allah has already created, after a certain stage, and only by outward signs. It can also be wrong. It is like a doctor estimating the due date. Nothing in it claims knowledge of the unseen.
A few things to keep in mind:
- Receive either with thanks. The Quran condemns the man whose face darkens when told of a daughter. Sons and daughters are both a gift.
- Never end a pregnancy for the sex. That is haram at every stage. See does Islam allow abortion.
- Choosing the sex through IVF is a separate question. See IVF gender selection.
- Parties. For gender reveal parties, see baby showers and gender reveals.
Scans are medical care, so follow a qualified doctor on when and how often to have them, and ask for a female sonographer where you can.
The evidence
Quran, Surah Luqman 31:34
إِنَّ ٱللَّهَ عِندَهُۥ عِلْمُ ٱلسَّاعَةِ وَيُنَزِّلُ ٱلْغَيْثَ وَيَعْلَمُ مَا فِى ٱلْأَرْحَامِ ۖ وَمَا تَدْرِى نَفْسٌۭ مَّاذَا تَكْسِبُ غَدًۭا ۖ وَمَا تَدْرِى نَفْسٌۢ بِأَىِّ أَرْضٍۢ تَمُوتُ ۚ إِنَّ ٱللَّهَ عَلِيمٌ خَبِيرٌۢ ﴿٣٤﴾
Indeed, Allah [alone] has knowledge of the Hour and sends down the rain and knows what is in the wombs. And no soul perceives what it will earn tomorrow, and no soul perceives in what land it will die. Indeed, Allah is Knowing and Acquainted.
The ayah refers to Allah's complete knowledge of the unborn, not to outward medical observation.
Quran, Surah An-Nahl 16:58-59
وَإِذَا بُشِّرَ أَحَدُهُم بِٱلْأُنثَىٰ ظَلَّ وَجْهُهُۥ مُسْوَدًّۭا وَهُوَ كَظِيمٌۭ ﴿٥٨﴾ يَتَوَٰرَىٰ مِنَ ٱلْقَوْمِ مِن سُوٓءِ مَا بُشِّرَ بِهِۦٓ ۚ أَيُمْسِكُهُۥ عَلَىٰ هُونٍ أَمْ يَدُسُّهُۥ فِى ٱلتُّرَابِ ۗ أَلَا سَآءَ مَا يَحْكُمُونَ ﴿٥٩﴾
And when one of them is informed of [the birth of] a female, his face becomes dark, and he suppresses grief. He hides himself from the people because of the ill of which he has been informed. Should he keep it in humiliation or bury it in the ground? Unquestionably, evil is what they decide.
Disliking a daughter is condemned, whatever a scan shows.
Sahih al-Bukhari 318
Sahih... يا رب أذكر أم أنثى ...
"My Lord, male or female?"
The angel asks, and Allah decrees, the sex, provision, lifespan and fate together: the complete knowledge that belongs to Him.
Sahih al-Bukhari 4697
Sahih... ولا يعلم ما تغيض الأرحام إلا الله ...
"And no one knows what the wombs fall short of except Allah."
The keys of the unseen include the full state of what is in the womb, which no test captures.
Where the ruling is recorded
- Fatawa Mahmudiyya, Chapter on beliefs, knowledge of the unseen. Knowledge gained by outward means and instruments is not knowledge of the unseen.
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.