During the Middle Ages, Milan was governed by a number of archbishops, under whom the city had a certain degree of independence. The archbishops, however, gradually lost their temporal power to the lower feudal nobility, who transformed Milan into a prosperous commune in the 11th century. In 1162 Milan was razed by troops under Emperor Frederick I. The city recovered sufficiently to help secure the victory (1176) of the Lombard League over Frederick near Legnano. The victory heralded a new period of prosperity. In 1277 a noble family, the Visconti, succeeded in wresting control of the city from the ruling Della Torre family; the Visconti ruled until 1447. The reign of Gian Galeazzo Visconti, 1st Duke of Milan (1351-1402), was a particularly prosperous period and was regarded as a golden age.
In 1450 the Italian soldier Francesco Sforza seized power and founded a line that remained firmly in control of Milan until 1500, when the city was conquered by France. The Sforzas continued to rule as puppets of successive foreign invaders, including the French, the Swiss, and the Austrians. The Sforza line died out in 1535, and soon afterwards Milan came under the rule of Spain. Spain ruled until 1713, when the city was ceded to Austria by the terms of the Peace of Utrecht. Napoleon ousted the Austrians in 1796 and made Milan the capital of the Cisalpine Republic.
Restored to Austria in 1815, Milan became a centre of Italian patriotic resistance, and in 1848 it briefly expelled the Austrians. In 1859, the Italians, aided by the French, freed Milan from Austrian control. In 1861 Milan joined the kingdom of Italy and subsequently prospered. During World War II it was heavily bombed. In the post-war period Milan experienced great commercial expansion and urban renewal.