The Mexica founded Tenochtitlan in 1325, or 1327 by some counts, on an island in the lakes that filled the Valley of Mexico. Their legend says the god Huitzilopochtli showed them the place with an eagle on a prickly pear, eating a rattlesnake. In time the island city came to rule its neighbours around Lake Texcoco, and when the Spanish landed, Aztec power reached from the Gulf of Mexico to the Pacific.
Hernán Cortés walked in along the causeway from Iztapalapa on 8 November 1519, and the ruler Moctezuma II met him with gifts. The welcome soured fast. Cortés held Moctezuma under house arrest, the city rose and drove the Spanish out in June 1520, and they came back the next May with a siege. Three months of hunger and smallpox followed before Cuauhtémoc surrendered in August 1521. The Spanish kept the street plan but built churches on the temples, which is why the ruins of the Templo Mayor now lie beside the cathedral.
As capital of New Spain, the city grew on a grid spreading out from the Zócalo, with Spaniards near the square and indigenous homes beyond. The friar Motolinia counted the rebuilding among the plagues of the age. A labour draft, the desagüe, compelled thousands of indigenous people to work on flood prevention over the colonial period. Families who made fortunes elsewhere in New Spain spent them here on churches and palaces, enough of them by the late 1700s for Alexander von Humboldt to call this the city of palaces. You can still see many of them.
Independence came in 1821, and Agustín de Iturbide was crowned emperor in the cathedral. Half a century of coups followed. In 1847 US troops stormed Chapultepec Castle, defended in part by young cadets now honoured as the Niños Héroes, and then occupied the city. In 1864 the French invaded and Maximilian of Habsburg ruled from the castle. He ordered the grand avenue to the centre that is now Paseo de la Reforma.
Porfirio Díaz took power in 1876 and gave the centre its European showpieces, the Palacio Postal among them. The Revolution that followed mostly spared the capital. The exception was the Decena Trágica of February 1913, when guns at the Ciudadela shelled the centre during the coup against President Francisco Madero, who was murdered later.
In 1900 about half a million people lived here. By 1980 it held nearly 9 million, over twice the 1960 figure, and with no housing available people built shanty towns on the edges. The 1968 Olympics left the Azteca stadium and the Palacio de los Deportes. That same year an unknown number of protesting students were massacred at Tlatelolco, under a one-party government that ruled the city from the Revolution on.
At 7:19 on the morning of 19 September 1985 an earthquake of magnitude 8.1 struck, killing somewhere between 10,000 and 40,000 people. It proved a political disaster for the one-party government. Ordinary residents ran the rescues and rebuilt much of the lost housing themselves. Since 1997 the city has elected its own mayor, and it leans far to the left of the country.