Short answer
Only for a medical reason, according to most contemporary scholars. Choosing an embryo's sex to avoid a serious genetic disease that affects one sex is allowed by many, including the Islamic Fiqh Council of the Muslim World League. Choosing it for preference or 'family balancing' through IVF and embryo testing is not allowed by most Hanafi muftis. Dua for a son or a daughter is always fine.
Sex selection through IVF and embryo testing for non-medical reasons is not permitted on the relied-upon view of contemporary Hanafi muftis, because it uncovers the awrah and discards embryos without need. To prevent a serious sex-linked disease, on specialist advice, it is permitted.
The four schools agree on this.
Explanation
Allah gives to whom He wills female children, and He gives to whom He wills males, or both, and makes barren whom He wills (42:49-50). Wanting a son or a daughter is natural, and asking Allah for one is allowed: Zakariyya asked for a son. The question is the method.
Sex selection in IVF usually works by creating several embryos, testing them (PGT), transferring only those of the wanted sex and discarding or freezing the rest. Scholars raise three concerns: IVF is permitted only as treatment for a real need, because it involves uncovering the awrah, and choosing a sex is not a need; embryos are discarded for a preference; and it can feed the old dislike of daughters that the Quran condemns (16:58-59).
The Islamic Fiqh Council of the Muslim World League ruled that no medical intervention to choose a baby's sex is allowed except in medical necessity, such as a hereditary disease that affects only males or only females. Many Hanafi muftis hold the same. Some scholars allow non-invasive methods, such as timing, where no embryos are created or discarded. The four classical schools have no ruling on IVF, so the difference is among contemporary scholars.
If you are having IVF for infertility, ask a mufti before agreeing to any sex selection, and keep to the conditions in is IVF haram. A child of either sex is a gift: the Prophet ﷺ said that whoever raises two girls until they come of age will be with him on the Day of Judgement.
Ask a mufti about your own case. This answer explains the general ruling. When the details of your situation matter, as they do here, ask a qualified mufti and give them the full facts. Where to ask a scholar.
The evidence
Quran, Surah Ash-Shura 42:49-50
لِّلَّهِ مُلْكُ ٱلسَّمَٰوَٰتِ وَٱلْأَرْضِ ۚ يَخْلُقُ مَا يَشَآءُ ۚ يَهَبُ لِمَن يَشَآءُ إِنَٰثًۭا وَيَهَبُ لِمَن يَشَآءُ ٱلذُّكُورَ ﴿٤٩﴾ أَوْ يُزَوِّجُهُمْ ذُكْرَانًۭا وَإِنَٰثًۭا ۖ وَيَجْعَلُ مَن يَشَآءُ عَقِيمًا ۚ إِنَّهُۥ عَلِيمٌۭ قَدِيرٌۭ ﴿٥٠﴾
To Allah belongs the dominion of the heavens and the earth; He creates what he wills. He gives to whom He wills female [children], and He gives to whom He wills males. Or He makes them [both] males and females, and He renders whom He wills barren. Indeed, He is Knowing and Competent.
The sex of a child is Allah's gift and decision.
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.
The Quran condemns disliking daughters.
Sahih Muslim 2645
Sahih... يا رب أذكر أو أنثى فيجعله الله ذكرا أو أنثى ...
"The angel says: My Lord, male or female? And Allah makes it male or female."
The child's sex is part of Allah's decree.
Sahih Muslim 2631
Sahih... من عال جاريتين حتى تبلغا جاء يوم القيامة أنا وهو ...
"Whoever raises two girls until they reach maturity, he and I will come on the Day of Resurrection like this."
Daughters are a source of great reward, not a lesser gift.
Where the ruling is recorded
- Resolutions of the Islamic Fiqh Council (Muslim World League), Resolution on choosing the sex of the fetus. Allows intervention only for medical necessity, such as sex-linked hereditary disease.
- Jadid Fiqhi Masa'il (Khalid Saifullah Rahmani), Chapter on test-tube babies. A Hanafi treatment of IVF and its limits.
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.