The Hafsid dynasty () was a Sunni Muslim dynasty of Berber descent that ruled Ifriqiya (modern day Tunisia, western Libya, and eastern Algeria) from 1229 to 1574. The dynasty was founded by Abu Zakariya Yahya, who was initially appointed governor of the region by the Almohad caliph before declaring his independence.

Under the reigns of Abu Zakariya and his successor, al-Mustansir (), the Hafsids consolidated and expanded their power, with Tunis as their capital. After al-Mustansir's death, internal conflicts resulted in a division between an eastern branch of the dynasty ruling from Tunis and a western branch ruling from Béjaïa and Constantine. The ancestor of the dynasty (from whom their name is derived), was Abu Hafs Umar ibn Yahya al-Hintati, a Berber from the Hintata tribal confederation, which belonged to the greater Masmuda confederation in present-day Morocco. He was a member of the Council of Ten, one of the highest Almohad political bodies, and a close companion of Ibn Tumart, the Almohad movement's founder. He was established in Tunis, which the Almohads had chosen as the province's administrative capital. His appointment came in the wake of the defeat of Yahya Ibn Ghaniya, who had launched a serious attack against Almohad authority in the region. Abu Muhammad Abd al-Wahid was ultimately quite effective in keeping order. The caliph had granted him a significant degree of autonomy in governing, partly to help persuade him to accept this difficult position in the first place. This laid the groundwork for a future Hafsid state. That same year, Sijilmasa and Ceuta (Sabta) also recognized his authority,

thumb|[[Louis IX of France lands at Tunis in 1270; French soldiers attempting to enter the city are killed by Tunisian soldiers. (Grandes Chroniques de France)]]

It was during his reign that the failed Eighth Crusade took place, led by Louis IX of France. After landing at Carthage, Louis died of dysentery in the middle of his army decimated by disease in 1270.

thumb|[[Philip III of France concludes the Treaty of Tunis with Hafsid sultan Muhammad I al-Mustansir. (1270)]]

After al-Mustansir's death in 1277, the Hafsids were riven by internal conflict, aggravated by interference from Aragon. After the initial split, the first successful reunification took place under Abu Yahya Abu Bakr II (), the ruler of the western branch who managed to take control of Tunis.

Apogee

thumb|Double page from the Qur'an manuscript endowed to the [[Kasbah Mosque, Tunis|Kasbah Mosque by sultan Abu Faris Abd al-Aziz II in March 1405. Bibliothèque nationale de France.]]

After the Marinid threat ended, attempts to reunify the Hafsids failed until Abu al-Abbas Ahmad II (), the emir of Béjaïa and Constantine, conquered Tunis in 1370. In 1424, he defeated the Zayyanid sultan, Abu Malik Abd al-Wahid, and placed another Zayyanid, Abu Abdallah Muhammad IV,<!-- As the cited sources are not in English and their spelling is difficult to read for English readers, the transliteration of the names here generally follows Bosworth's 1996 "The New Islamic Dynasties" --> on the throne of Tlemcen as his vassal. Around the same time (probably in 1426), Abu Faris Abd al-Aziz also helped to install Abd al-Haqq II on the Marinid throne in Fez – under the regency of Abu Zakariya Yahya al-Wattasi – and thus obtained from him a recognition of Hafsid suzerainty.

In 1429, the Hafsids attacked the island of Malta and took 3000 slaves, although they did not conquer the island. Kaid Ridavan was the military leader during the attack. The profits were used for a great building programme and to support art and culture. However, piracy also provoked retaliation from the Christians, which several times launched attacks and crusades against Hafsid coastal cities such as the Barbary crusade (1390), the Bona crusade (1399) and the capture of Djerba in 1423.

Abu Faris Abd al-Aziz II died in 1434 during another expedition against Tlemcen. He led two expeditions to Tlemcen in 1462 and 1466 and made the Zayyanids his vassals, while the Wattasid state in Morocco also formally accepted his authority. The entire Maghreb was thus briefly under Hafsid suzerainty. Due to the Ottoman threat, the Hafsids were vassals of Spain after 1535. The Ottomans again conquered Tunis in 1569 and held it for four years. Don Juan of Austria recaptured it in 1573. The Ottomans reconquered Tunis in 1574, and Muhammad VI, the last Caliph of the Hafsids, was brought to Constantinople and was subsequently executed due to his collaboration with Spain and the desire of the Ottoman Sultan to take the title of Caliph as he now controlled Mecca and Medina.

Society

Demographic developments

thumb|View of Tunis ([[Agostino Veneziano)]]

The demographics of Ifriqiya changed considerably during the Hafsid period and in the period leading up to it. The large-scale influx of Arab Bedouin tribes into the countryside during the preceding centuries promoted the Arabization of Ifriqiya. Aside from some relatively isolated region such as Djerba and the Nafusa Mountains, where Berber languages continued to be spoken, nearly all of Ifriqiya became fully Arabized during this era. however piracy against Christian shipping grew as well, particularly during the rule of Abd al-Aziz II (1394–1434). The Hafsids also had a large stake in trans-Saharan trade through the caravan routes from Tunis to Timbuktu and from Tripoli to sub-Saharan Africa. Of great impact on culture were immigrants from al-Andalus, whom Abu Zakariya encouraged to come to his realm in the 13th century. Among the most important figures was the historian and intellectual, Ibn Khaldun, born in Tunis and of Andalusi descent. While Kairouan remained an important religious center, Tunis was the capital and progressively replaced it as the main city of the region and the main center of architectural patronage. Unlike the architecture further west, Hafsid architecture was built primarily in stone (rather than brick or mudbrick) and appears to have featured much less decoration. and is mentioned in historical records for the first time during the reign of Abu Faris.). This was followed by many others (almost all of them in Tunis) such as the Madrasa al-Hawa founded in the 1250s, the Madrasa al-Ma'ridiya (1282), and the Madrasa al-Unqiya (1341). The Madrasa al-Muntasiriya, completed in 1437, is among the best preserved madrasas of the Hafsid period. he states : "Among the Hafsid standards carried in the parades stood out, apart, closer to the sultan and held by a man on horseback, a white standard, the "victorious standard" (al-alam al-mansûr). It is with good reason that some wanted to find in this white standard that of the Almohads, of the same color, reproducing in turn that which the Fatimids had adopted." Historian Charles-André Julien also speaks of Hafsid sovereigns doing parades with their court while hoisting their own white standard, overshadowing multicolored flags of embroidered silk.

The Book of Knowledge of All Kingdoms, written by a Franciscan friar in the 14th century, describes the flag of Tunis as being white with a black moon at its center. Other cities within modern Tunisia and eastern Algeria were also reported having white flags with a moon.<gallery class="center" caption="Flags of Hafsids on portolans and from other sources">

File:Flag of Tunis during 14th century.svg|Early red flag with white or yellow crescent of the 14th century, reported by Marino Sanudo (ca. 1321), Pietro Vesconte (1325), Angelino Dulcerta (1339) and the Catalan Atlas (1385)

File:Flag of Hafsid Tunisia (1550).svg|White with blue crescent according to Jacobo Russo, 1550 (last period of the kingdom)