Short answer
Islam holds that a person's sex is part of how Allah created them, so medical or social transition to the other sex is not permitted in the Hanafi school or the other three. Feeling a mismatch with your body is not a sin, and anyone who struggles with it deserves compassion, support and good care. People born intersex are a separate case with their own rulings.
Changing one's sex through surgery or hormones, and dressing or living as the opposite sex, are haram because they change Allah's creation and involve imitating the other sex. Inner feelings one does not choose carry no sin. Medical treatment for an intersex (khuntha) condition to establish the dominant sex is permitted.
The four schools agree on this.
Explanation
The Quran says Allah creates the two mates, the male and female (53:45), and records Satan's vow to make people change the creation of Allah (4:119). The Prophet ﷺ cursed men who imitate women and women who imitate men. On this basis the four schools hold that sex is not a matter of choice, and that surgery, hormones or a public change of gender to live as the other sex are not allowed.
That ruling is about actions, not feelings. Someone who experiences gender dysphoria has not sinned by feeling it. The Prophet ﷺ said Allah overlooks what passes through a person's mind as long as they do not act on it or speak it. Struggling with such feelings while keeping to Allah's limits is a real test, and patience in a test is rewarded.
A person who has already transitioned remains a Muslim, and the door of tawbah is open. Rulings such as prayer rows, awrah and inheritance follow the sex they were born with, and a mufti can guide on the details of each case.
Intersex is different. Classical fiqh has a full chapter on the khuntha, a person born with ambiguous sex characteristics. Doctors may examine and treat them to establish the dominant sex. That is not changing Allah's creation but treating a condition.
Families and communities should respond with mercy, not mockery: shaming drives people away from Allah and harms their mental health. If you or someone you know is struggling, find a Muslim counsellor or a trusted scholar, and if there is any risk of self-harm, contact emergency services or a crisis line in your country straight away. See also mental health in Islam.
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 An-Najm 53:45-46
وَأَنَّهُۥ خَلَقَ ٱلزَّوْجَيْنِ ٱلذَّكَرَ وَٱلْأُنثَىٰ ﴿٤٥﴾ مِن نُّطْفَةٍ إِذَا تُمْنَىٰ ﴿٤٦﴾
And that He creates the two mates - the male and female - From a sperm-drop when it is emitted
Male and female are Allah's creation, not a choice.
Quran, Surah An-Nisaa 4:119
وَلَأُضِلَّنَّهُمْ وَلَأُمَنِّيَنَّهُمْ وَلَءَامُرَنَّهُمْ فَلَيُبَتِّكُنَّ ءَاذَانَ ٱلْأَنْعَٰمِ وَلَءَامُرَنَّهُمْ فَلَيُغَيِّرُنَّ خَلْقَ ٱللَّهِ ۚ وَمَن يَتَّخِذِ ٱلشَّيْطَٰنَ وَلِيًّۭا مِّن دُونِ ٱللَّهِ فَقَدْ خَسِرَ خُسْرَانًۭا مُّبِينًۭا ﴿١١٩﴾
And I will mislead them, and I will arouse in them [sinful] desires, and I will command them so they will slit the ears of cattle, and I will command them so they will change the creation of Allah." And whoever takes Satan as an ally instead of Allah has certainly sustained a clear loss.
Changing Allah's creation is Satan's call.
Sahih al-Bukhari 5885
Sahih... لعن رسول الله صلى الله عليه وسلم المتشبهين من الرجال بالنساء ...
"The Messenger of Allah cursed men who imitate women, and women who imitate men."
Living as the opposite sex is forbidden.
Sahih al-Bukhari 5269
Sahih... إن الله تجاوز عن أمتي ما حدثت به أنفسها ما لم تعمل أو تتكلم ...
"Allah has overlooked for my ummah what their souls whisper, as long as they do not act on it or speak it."
Unchosen feelings and thoughts are not sins.
Where the ruling is recorded
- al-Hidaya, Kitab al-Khuntha. The rulings for a person of ambiguous sex, decided by the dominant signs.
- Radd al-Muhtar (Ibn Abidin), Kitab al-Hazr wa al-Ibaha, on men imitating women and women imitating men. Imitating the opposite sex in dress and manner is forbidden.
- Resolutions of the Islamic Fiqh Council (Muslim World League), Resolution on sex-change operations. Forbids changing a person's sex, and allows surgery to treat an intersex condition.
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.