History of medieval Tunisia
The medieval era of Tunisia starts with what will eventually return Ifriqiya (Tunisia, and the entire Maghrib) to local Berber rule. The Shia Islamic Fatimid Caliphate departed to their newly conquered territories in Egypt leaving the Zirid dynasty to govern in their stead. The Zirids would eventually break all ties to the Fatimids and formally embrace Sunni Islamic doctrines.During this time there arose in Maghrib two strong local successive movements dedicated to Muslim purity in its practice. The Almoravids emerged in the far western area in al-Maghrib al-Aksa (Morocco) establishing an empire to as north as modern Spain (al-Andalus) and south to Mauretania; Almoravid rule never included Ifriqiya. Later, the Berber religious leader Ibn Tumart founded the Almohad movement, supplanted the Almoravids, and would eventually bring under the movement's control al-Maghrib and al-Andalus. Almohad rule would be succeeded by the Tunis-based Hafsids. The Hafsids were a local Berber dynasty and would retain control with varying success until the arrival of the Ottomans in the western Mediterranean.