Kranj was attested in written sources in the 5th century and c. 670 as Carnium (and as via Chreinariorum in 973, actum Kreine in 1050–65, in loco Chreina in 1065–77, and Chrainburch in 1291). The Slovene name is derived from Slavic *Korn’ь, borrowed from Romance Carnium in late antiquity. Like the Latin regional name Carnia, it is derived from the northern Italic (Celtic) tribe known as the Carnī (Greek: Κάρνοι). The name of the tribe is probably derived from the Celtic root *karno- 'peak, hill, pile of stones'.[4] The German name of the town was Krainburg.[5] The name of the historical region of Carniola is a Latin diminutive form of Carnia.[4]

Archaeological finds show that Kranj was settled in prehistoric times. Discoveries include a bronze ax found in Drulovka, Hallstatt-era graves in the northern part of the town above the bank of the Kokra River, testifying to Illyrian settlement, and a burial site in the southern part of the town above the left bank of the Sava River, indicating a Celtic settlement. The Romans founded the settlement of Carnium at the confluence of the Sava and Kokra. In the 6th century, a major Germanic settlement stood at the same site, and an Ostrogothic cemetery was discovered nearby. The Gothic settlement was continued by the Lombards and existed until c. AD 580, when it was destroyed by invading Slavs.[6]

Traces of the old Slavic settlement (a Slavic burial site) date from the 9th and 10th centuries. As the seat of the margraves of Carniola in the 11th century, it was the most important settlement in the territory. The town itself is believed to have developed in the early 13th century; citizens of the town of Kranj appear in a document from 1221, and Kranj was officially referred to as a town in 1256. It was the seat of a court whose jurisdiction extended between that of Radovljica and Kamnik. In 1414 a decision was issued relieving the citizens of the town from paying tolls. In 1422 an ordinance required houses to be built of stone to prevent fires. A parish school was established in Kranj in 1423, and the same year the right was granted to Kranj to elect its own judge. Kranj was laid waste in 1471 in an Ottoman attack. Emperor Frederick III granted Kranj the right to collect tolls in documents from 1488 and 1493, and a 1493 document also granted the town the right to hold fairs twice a year. The town hospice records date back to the 15th century.[6]

Crafts developed in Kranj during the Middle Ages. Mills first developed along the Sava and Kokra rivers, and this was followed by butchers, fur merchants, hide and wood processors, and then weavers of canvas and woolen cloth. Habsburg efforts to maintain Vienna's monopoly on trade with Italy resulted in trade routes bypassing Kranj.[6]

Kranj was affected by peasant revolts in the 16th century; the leaders of the 1515 peasant revolt were beheaded in Kranj, and in 1525, when a new revolt threatened Carniola, hussars commanded by Johann Katzianer occupied the town and caused more damage than the Ottomans had inflicted half a century earlier. In 1668 half of the houses in Kranj were destroyed by a fire, and the entire town burned in 1749. Kranj was affected by plague outbreaks in 1552, 1557, 1625, 1627, and 1657. In the mid-16th century, most of the townspeople converted to Protestantism; the merchants of Kranj opened a Protestant school and Slovenian books by Protestant authors were imported from Germany. The Protestant Reformation in Kranj was led by Gašpar Rokavec, who was succeeded by Jernej Knafel after his death. Knafel was forced to withdraw from Kranj to Brdo Castle during the Counter-Reformation.[6]

Economically, teamster services developed in Kranj in the 16th century, with connections to the rest of Upper Carniola and Carinthia. There were also several blacksmith workshops and two foundries along the Sava River. Sieve-making also developed at this time; horsehair was imported from around Europe and the sieves were exported to France, Belgium, Germany, and Greece. Several breweries and leather works operated in the town. Kranj went into an economic decline in the 17th century, when there was much emigration from the town, leaving many houses empty, and business did not revive again until the second half of the 18th century.[6]

Kranj was affected by plague outbreaks in 1836 and 1855. A Slovene reading room was established in 1863. Artisans' workshops became established in Kranj in the 19th century, with roots going back to a number of painters in the 17th and 18th centuries. Prominent among these was the workshop of Josip Egartner Jr. (1833–1905), who settled in Kranj in 1875. An upper secondary school was established in 1861, and a vocational school for textile workers opened in 1930. A water supply system was installed in Kranj in 1901, supplied by Čemšenik Spring on the Kokra River.[6]

There was limited industry in Kranj until the late 19th century. Until this time, trade in agricultural products, livestock, and wood was economically most important. The Majdič Mill, which operated from 1874 until the Second World War, was an early industry, producing up to 70,000 kg (154,324 lb) of milled products per day. A leather factory was established in 1875. Large-scale industrialization occurred after the First World War, starting with the founding of a rubber factory in 1921. The Jugo-Češka textile works was established in 1923. Additional textile works were established after this, making Kranj one of the most important centers of textile manufacturing in pre-war Yugoslavia. A major strike by textile workers occurred in 1936, when they occupied the factories. Two shoe factories were established in 1925, and a bakery in 1937.[6]

During the Second World War, Kranj, along with the rest of northern Slovenia, was annexed by Nazi Germany.[7] The German authorities dismantled the Jugo-Češka textile works, replacing the machinery with equipment to produce aircraft. On 21 March 1944, German forces discovered several communist activists and functionaries at the Šorli Mill in Rupa in the northern part of the town, where military supplies for the Partisans were being stored. Three of the men at the mill were killed and the German forces then burned the mill.[6]

Kranj is the site of a mass grave from the period immediately after the Second World War. The Planina Mass Grave (Slovene: Grobišče Planina) is located in a small woods in a field near the city cemetery. It contains the remains of an undetermined number of people murdered after the war; the victims may be German prisoners of war, Home Guard soldiers repatriated from Austria, or Slovene civilians from Kranj and the surrounding area.[8][9]

The St. Cantianus and Companions Parish Church (Župnijska cerkev sv. Kancijana in tovarišev) is the largest church in Kranj and also the seat of the Kranj Parish and Deaconates.[10] It was built in the 14th century, and measures 442 cubic metres (15,600 cu ft). Construction was commissioned by the counts of Kranj.