Palaces were also built and decorated with the help of Andalusian craftsmen from Cordoba and Seville, who brought the Umayyad style characterized by chiseled domes and poly-lobed arches. This Andalusian influence merged with Saharan and even West African elements and was synthesized into an original architecture totally adapted to the specific environment of Marrakech. The city became the capital of the Almoravid Emirate which extended from the shores of Senegal to the center of Spain and from the Atlantic coast to Algiers.
The city was then fortified by the son of Youssef Ibn Tachfin, Ali Ben Youssef, who had the ramparts built around 1122-1123, which are still visible.
In 1147 the Almohads, supporters of an orthodox Islam and from the Masmoudas tribes of the High Atlas, took over the city. The last Almoravids were exterminated except for those who went into exile in the Balearic Islands (Beni Ghania family). As a result, almost all the monuments were destroyed. The Almohads built many palaces and religious buildings, such as the famous Koutoubia mosque built on the ruins of an Almoravid palace, and twin sister of the Giralda in Seville and the Hassan Tower (unfinished) in Rabat.
The Kasbah housed the caliphal residence (since the reign of Abd al-Mu'min the Almohad ruler bore the title of caliph, thus competing with the distant eastern caliphate of the Abbasids), embellished with a hospital that attracted the Andalusian physician Ibn Toufayl. Of the majestic complex of the Mansourian Kasbah, named after the Caliph Abu Yousef Yaqoub al-Mansour, the superb gate of Bab Agnaw still remains. Marrakech was thus worthy of housing the capital of the major power of the Mediterranean Muslim West at the time, the Almohad Empire which encompassed the entire region between Cordoba and Tripoli, from Spain to Libya.At the end of the seventeenth century, the Alawite dynasty succeeded the Saadians. The throne was successively transferred to Fez and then to Meknes, the new imperial city. Sultan Mohammed III (1757-1790) chose the city as his main residence because of its proximity to the port of Mogador (now Essaouira), which he had built according to the plans of the French architect Théodore Cornut. It was also in Marrakech that the first treaty of friendship between Morocco and the newly independent United States was signed in 1787. In 1792, Marrakech became the capital of a son of Mohammed III, Hicham, who was recognized as sultan by this part of the country while his brother Sulayman was recognized as the legitimate sultan in Fez by the ulama and by the provinces north of the Oum Errabiaa River. There followed a war between the two rival sultans, which ended with the defeat of Hicham in 1796, despite the Spanish support he enjoyed. Marrakech was reconquered by Sulayman in 1797 and the city returned to the territory of the official makhzen of Fez.