Rioters burned the old churches
The Roman emperor Constantius II built a big church next to the imperial palace in Constantinople (now Istanbul) in the 300s AD. But people burned it down in riots in 404. Pulcheria soon had the church rebuilt. But rioters burned it down again during the Nika Riots of 532. So the emperor Justinian needed to rebuild the church a third time. He chose Isidore of Miletus as the architect.
Hagia Sophia’s first dome
Isidore built this new church to be a really exciting new architectural achievement. He gave it a stupendous dome over the nave, surrounded by smaller half-domes. Like other buildings of the Early Middle Ages, the inside was covered with mosaics.
Justinian dedicated the new church in December 537, only six years after the old church burned down. He dedicated it to Holy Wisdom (Hagia Sophia in Greek). That was the third part of the Trinity, with God and Jesus.
Then there was an earthquake
Istanbul has a lot of earthquakes, and they have been a big problem for Hagia Sophia. Isidore’s dome collapsed in an earthquake only 16 years after it was built, in 553.
So they built a second dome
Isidore’s nephew, also an architect and also named Isidore, rebuilt the dome a little higher and with better engineering. This is the dome we see today. He designed a new system to transition from the round dome to the square walls of the church, using pendentives: triangular transition pieces. (The Pantheon dome had avoided this problem by being a round building.)
Isidore the Younger also added ribs to the dome, to channel the weight to the right parts of the walls. However, some sections of this dome also collapsed, and were rebuilt later. This second dome in Hagia Sophia was the biggest dome ever built except the Pantheon. It stayed the biggest dome (except the Pantheon) for almost a thousand years, until Brunelleschi built the Duomo in Florence.
Hagia Sophia becomes a mosque
In 1453, the Ottoman sultan Mehmed II captured Constantinople. He changed Hagia Sophia into a mosque and added four minarets. Hagia Sophia remained a mosque until 1931, when Ataturk and the new Republic of Turkey converted Hagia Sophia into a museum (as the French did with their churches in their Revolution).