Within the Muslim world, sentiment towards LGBTQ people varies and has varied between societies and individual Muslims. While colloquial and de facto official acceptance of at least some homosexual and gender variant behaviors were commonplace in pre-modern periods, later developments, starting from the 19th century, have created a predominantly hostile environment for LGBTQ people.

There are differences in how the Quran and later hadith traditions (orally transmitted collections of Muhammad's teachings) treat homosexuality, with the latter being far more explicitly negative. This has caused rifts in legalistic opinion, as while all major schools of jurisprudence broadly agreed that liwat—anal sex between men—was haram, opinions varied in terms of the legality and severity of punishment, as well as the legal situation of women's same-sex relations. Furthermore, these formulations largely remained theoretical, as historical evidence from the pre-modern period shows de facto tolerance of homosexual relationships. Historical records also suggest that in the event that laws against homosexuality were invoked in the pre-modern period, they were done so mainly in cases of rape or other "exceptionally blatant infringement on public morals". These factors allowed themes of homoeroticism and pederasty to be cultivated in Islamic poetry and other Islamic literary genres from the 8th century CE into the modern era.

In the modern era, Muslim public attitudes towards homosexuality underwent a marked change beginning in the 19th century, largely due to the global spread of Islamic fundamentalist movements, namely Salafism and Wahhabism whose adherents believe in rigid and literalist version of Shariah. The Muslim world was also influenced by the sexual notions and norms that were prevalent in the Christian world at the time, particularly with regard to anti-LGBTQ legislation prevalent throughout European societies; a number of Muslim-majority countries that were once colonies of European empires retain colonial criminal penalties against homosexuality.

As Western culture eventually enabled a platform for the flourishing of many LGBTQ movements, many Muslim fundamentalists came to associate the Western world with "ravaging moral decay" and rampant homosexuality. In contemporary society, prejudice, anti-LGBTQ discrimination and anti-LGBTQ violence—including violence which is practiced within legal systems—persist in much of the Muslim world, exacerbated by socially conservative attitudes and the rise of Islamist ideologies in some countries. There are laws in place against homosexual activities in many Muslim-majority countries, with a number of them prescribing the death penalty for convicted offenders. In surveys of public opinion, the vast majority of Muslims across various countries reject the notion that homosexuality should be acceptable in society. Most Muslim-majority countries have opposed moves to advance LGBTQ rights and recognition at the United Nations (UN), including within the UN General Assembly and the UN Human Rights Council.

Despite these developments, contemporary Islamic jurisprudence generally accepts the possibility for transgender people (mukhannith/mutarajjilah) to change their gender status, but only after surgery, linking one's gender to biological markers. Trans people are nonetheless confronted with stigma, discrimination, intimidation, and harassment in many ways in Muslim-majority societies. Transgender identities are often considered under the gender binary, although some pre-modern scholars had recognized effeminate men (mukhannath) as a form of third gender, as long as their behaviour was natural and not a performance.

History Muslim attitudes to LGBTQ practices has varied throughout Islamic history; legal scholars condemned and often formulated punishments for homosexual acts, yet lenient (or often non-existent) enforcement allowed for toleration, and sometimes celebration of such acts.

Homoeroticism was idealized in the form of poetry or artistic declarations of love, often from an older man to a younger man or adolescent boy. This was accepted in courtly settings as a system of pederasty, though egalitarian same-sex relations also occurred (with the threat of the social censure of the passive partner, if revealed). Accordingly, the Arabic language had an appreciable vocabulary of homoerotic terms, with multiple words to describe types of male prostitutes, including those pre-dating Islam. Schmitt identifies some twenty words in Arabic, Persian, and Turkish to identify those who are penetrated. Other related Arabic words includes mukhannathun (effeminate men), ma'bûn, halaqī, and baghghā.

Pre-modern era There is little evidence for homosexual practice in Islamic societies for the first century and a half of the Islamic era. Homoerotic poetry appears suddenly at the end of the 8th century CE, particularly in Baghdad in the work of Abu Nuwas (756–814), who became a master of all the contemporary genres of Arabic poetry. The famous author Jahiz tried to explain the abrupt change in attitudes toward homosexuality after the Abbasid Revolution by the arrival of the Abbasid army from Khorasan, who are said to have consoled themselves with male pages when they were forbidden to take their wives with them. According to numerous contemporaneous authors, the increased prosperity following the early conquests was accompanied by a supposed "corruption of morals" in the two holy cities of Mecca and Medina; Bosworth and others infer that homosexual practice became more widespread during this time as a result of acculturation to foreign customs, such as the music and dance practiced by mukhannathun, who were mostly foreign in origin, while other authors claim this process was related to interfaith dialogue, especially in and around Transoxiana.

The following centuries witnessed a continuation of this trend. The Abbasid caliphate Al-Amin (r. 809–813) was said to have required slave women to be dressed in masculine clothing so he could be persuaded to have sex with them, and a broader fashion for ghulamiyyat (boy-like girls) is reflected in literature of the period. Religious scholars were also influenced by this environment, but to a lesser extent; Chief Judge of the Abbasid Caliphate Yahya ibn Aktham permitted homosexual acts, despite being harsh on other sexual acts such as fornication. This had proved controversial with a writer, Abi Salma, who wrote "we had hoped to see justice apparent, but our implorations ended in despair, for, can the world and its people come to any good when the Grand Judge of Muslims sodomizes (yaluṯu)?" Later on, in an eleventh-century discussion among the scholars of Baghdad, some scholars who showed traits of bisexuality argued that it is natural for a man to desire anal intercourse with a fellow man, but this would only be allowed in the afterlife. Similarly, El-Rouayheb suggests that as Islamic orthodoxy stabilized and religious scholars considered sodomy as an abhorrent sin more or less without exception, most of them did not believe that this prohibition covered falling in love or writing poetry.

The conceptions of homosexuality found in classical Islamic texts resembled the traditions of classical Greece and those of ancient Rome, rather than the modern understanding of sexual orientation. In earlier texts, it was expected that many mature men would be sexually attracted to both women and adolescent boys (with a supposed 'ideal' beginning at the age of fourteen and lasting until the growth of a full beard), and such men were expected to wish to play only an active role in homosexual intercourse once they reached adulthood. These norms were not static, however; while the growth of a beard was considered to be the conventional age when an adolescent lost his homoerotic appeal in earlier periods, the age of the stereotypical beloved became less restricted during later periods. This prototype was often represented in Persian poetry by Turkic slave-soldiers in most cases considerably older than the supposed 'ideal age', and was also illustrated by the story of Mahmud of Ghazni (971–1030), the ruler of the Ghaznavid Empire, and his cupbearer Malik Ayaz. While classical texts and poetry covered homoeroticism in a restricted fashion, many authors argue that in many periods, the permissiveness and egalitarianism in the discourse of same-sex relations varied from one context to another. Murray argues that despite the normative nature of a restrictive pederastic discourse in courtly settings, egalitarian same-sex relations did happen; and furthermore, Rowson points out that male authors writing about sex seem to have been relatively ambivalent about female same-sex relations. Speaking for the lands of the Ottoman Empire specifically, Ze'evi claims that the stigma attached to adult passive partners common in the Golden Age seems to be suspiciously absent from Ottoman medical treatises; and further argues that, with the exception of a far more male-centered, patriarchal voice, colloquial Ottoman sexual discourse would "to us (...) be more akin to latter-day television series such as Sex and the City or Will and Grace than to most nineteenth-and twentieth-century sexual discourse."

In turn, more open discussions of sexuality during these times can be found in numerous contexts. The compendiums of both folk and classical poetry and song compiled by Ali Ufkî, contains numerous songs and poems related to male and female same-sex love, at least in one case in an egalitarian context. The works of Enderunlu Fazıl similarly covers both male and female same-sex love in a much less age-segragated fashion. Nedîm, the famed Ottoman poet, seems to have freely talked about love with older men, as he made frequent mention of beards in his Turkish and Persian poetry. Similarly, shadow play often represented female same-sex love as well as one of the main characters, Karagöz, switching between active and passive roles. As for Central Asian examples, Babur, Timurid prince and founder of the Mughal Empire, has written in his memoirs about a young male lover named Baburi. It is most likely that they were in similar ages, as Babur was in his teenage years, given the time of the account.

Despite these exceptions in certain settings and periods however, homosexual practices present in courtly and 'respectable' settings in the Islamic world remained largely age-segregated. In the medical tradition, seeking to play a passive role was considered both unnatural and shameful for a mature man, and following Greek precedents, the Islamic world regarded this as pathological. Therefore, the medical term ubnah was used for the desire of a man to exclusively be on the receiving end of anal intercourse. Physicians that theorized on ubnah includes Rhazes, who thought that it was correlated with small genitals and that a treatment was possible provided that the subject was deemed to be not too effeminate and the behavior not "prolonged".. Dawud al-Antaki, in contrast, advanced that it could have been caused by an acidic substance embedded in the veins of the anus, causing itchiness and thus the need to seek relief.

Modern era The 18th and 19th centuries saw the rise of Islamic fundamentalism such as Wahhabism, which came to call for stricter adherence to the Hadith. In 1744, Muhammad bin Saud, the tribal ruler of the town of Diriyah, endorsed ibn Abd al-Wahhab's mission and the two swore an oath to establish a state together run according to what they considered to be true Islamic principles. For the next seventy years, until the dismantlement of the first state in 1818, the Wahhabis dominated from Damascus to Baghdad. Homosexuality, which had been largely tolerated in the Ottoman Empire, became criminalized under the Wahhabis, and those found guilty were thrown to their deaths from the top of the minarets.