Tallinn's naturally commanding site, on ground overlooking the Gulf of Finland, is thought to have been settled by Finno-Ugric people around 2500 BC. There was probably a proto-Estonian trading settlement here from around the 9th century AD and a wooden stronghold was built on Toompea (tawm-pe-ah; the hill dominating Tallinn) in the 11th century. The Danes under King Waldemar II (who conquered northern Estonia in 1219) met tough resistance at Tallinn and were on the verge of retreat when it’s said that a red flag with a white cross (the 'Dannebrog') fell from the sky into their bishop’s hands. Taking this as a sign of God’s support, they went on to win the battle and gain a national flag. The Danes built their own castle on Toompea, and are thought to have given Tallinn its name: Taani linn, Estonian for ‘Danish town’.

The Livonian Brothers of the Sword, a Germanic order of crusading warrior-monks, took Tallinn from the Danes in 1227 and built the first stone fort on Toompea. German traders arrived from Visby on the Baltic island of Gotland and founded a colony of about 200 people beneath the fortress. In 1238 Tallinn returned to Danish control but in 1285 it joined the German-dominated Hanseatic League as a channel for trade between Novgorod, Pihkva (Russian: Pskov) and the West. Furs, honey, leather and seal fat moved west; salt, cloth, herring and wine went east.

By the mid-14th century, when the Danes sold northern Estonia to the Teutonic Order, Tallinn was a major Hanseatic town with a prime position on the Gulf of Finland trade channel and a population of about 4000. Conflict with the knights and bishop on the hill led the mainly German artisans and merchants in the lower town to build a fortified wall to separate themselves from Toompea. Tallinn prospered regardless and became one of northern Europe’s biggest towns. Tallinn’s German name, Reval, coexisted with the local name until 1918.

Prosperity faded in the 16th century as the Hanseatic League weakened and Russians, Swedes, Danes, Poles and Lithuanians fought over the Baltic region. Sweden held Tallinn from 1561, withstanding a 29-week siege by Russia’s Ivan the Terrible (1570 to 1571) but surrendering to Peter the Great in 1710, when the town was ravaged by plague.

In 1870 a railway was completed from St Petersburg, and Tallinn became a chief port of the Russian empire. Freed peasants converged on the city from the countryside, increasing the percentage of Estonians in its population from 52% in 1867 to 89% in 1897. By WWI Tallinn had big shipyards and a working class of over 100,000. It was the natural capital of the brief Estonian Republic of 1920-1940.

Tallinn suffered badly in WWII, with thousands of buildings destroyed during Soviet bombing in 1944. After the war, under Soviet control, large-scale industry was developed – including the USSR’s biggest grain-handling port – and the city expanded, growing to nearly 500,000 from a 1937 level of 175,000. Much of the new population came from Russia and new high-rise suburbs were built on the outskirts to house the workers.

The explosion of Soviet-style settlements in the suburbs meant a loss of cultural life in the centre. By the 1980s, Tallinn's Old Town was run-down, with most people preferring to live in the new housing developments. It began to be renovated late in the decade, with the fight for independence largely playing out on these historic streets.

The 1990s saw Tallinn transformed into a contemporary mid-sized city, with a restored Old Town and a modern business district. Central Tallinn shows a taste for all things new and pleasurable, with IT-driven businesses to the fore and a tech-savvy, wi-fi–connected populace enjoying increasingly excellent restaurants and life opportunities. However, the outskirts of the city have yet to get the facelift that the centre has received – in these untouristed parts, poverty and unemployment are more evident.