The Hafsid dynasty (Arabic: الحفصيون, romanized: al-Ḥafṣiyūn) was a culturally Arabized 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 (r. 1249–1277), 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 Consantine. A reunification took place under Abu Yahya Abu Bakr II (r. 1318–1346), but his death was followed by another crisis during which the Marinids, based in present-day Morocco, invaded briefly. Eventually, unity was re-established by Abu al-Abbas Ahmad II (r. 1370–1394), who inaugurated the apogee of Hafsid power and influence across the region, which continued under Abu Faris Abd al-Aziz II (r. 1394–1434) and Abu 'Amr 'Uthman (r. 1435–1488). After this, their power gradually declined. During the 16th century, as the Ottoman Empire encroached on the region, the Hafsids were propped up by Spain until the final Ottoman conquest of Tunis in 1574 put an end to their reign.