Gibraltar is a sore point for Spain. It sits on a small peninsula in the south-western reaches of Spain, jutting out into the Mediterranean Sea at the only entrance from the Atlantic, just a few miles from North Africa.

But Gibraltar isn't part of Spain, even though it's connected to the mainland. It's a British Overseas Territory, of which there are 14, legacies of the Empire over which the British government maintains its sovereignty.

Gibraltar, like the others, is self-governing, though it relies on the British government for defence, foreign policy and trade. Spain disputes British sovereignty over Gibraltar and wants it to be brought under the control of Madrid, making it a flashpoint in Brexit negotiations.

The former Tory leader Lord Howard caused a stir by suggesting there could arise a situation similar to the Falklands War in the early 1980s, when Prime Minister Margaret Thatcher defended the islands - a British Overseas Territory - from Argentinian attack.

But how did Gibraltar, which is over a thousand miles from London, end up under British sovereignty in the first place?

Tarik Ibn Zeyad, the military commander who led the Muslim conquest of Spain, captured Gibraltar in 711. It did not come back into Spanish hands until 1501, under Queen Isabella I of Spain, a few years after Muslim rule had ended.

Gibraltar Britain Spain EU
A general view shows the Spanish city of La Linea de la Concepcion (rear) and the tarmac of the Gibraltar International Airport (bottom L) while tourists stand on the top of the Rock (R) next to the European Union flag, in the British overseas territory of Gibraltar, September 14, 2016 Reuters

Spanish rule lasted only a couple of centuries. During the War of the Spanish Succession, a conflict between European powers over which monarchy should inherit Spain and its territories after heirless Charles II's death in 1700, Britain captured Gibraltar with a fleet led by Admiral George Rooke in 1704, and held onto it.

Its geographic location on the Strait of Gibraltar, a gateway to the Mediterranean, made it a peninsula of great strategic importance.

The war ended with the Treaties of Utrecht between 1713 and 1714, a series of agreements between the countries to bring about peace and an end to the Spanish succession question. On 13 July, 1713, Spain signed a treaty with the British handing them Gibraltar.

"The Spanish nevertheless made several attempts to retake Gibraltar from Britain, most notably in a protracted but unsuccessful military siege that lasted from 1779 to 1783," says Encyclopedia Britannica.

"In 1830 Gibraltar became a British crown colony. The opening of the Suez Canal (1869) heightened British determination to keep possession of Gibraltar, since the Mediterranean was the main route to Britain's colonies in East Africa and southern Asia."

The Gibraltarians claim the right to self-determination and reject demands from Spain that the territory, which has a population of 30,000, should fall back under the sovereignty of Madrid. In 2002, on an 88% turnout in a referendum, Gibraltar voted by 99% against Spain sharing sovereignty with Britain.

But they also voted in the EU referendum by 96% to remain, with an 84% turnout, creating friction with the British government as it pursues Brexit and uncertainty around the future of its sovereignty.

"Gibraltar is not a bargaining chip in these negotiations," said Gibraltar's chief minister Fabian Picardo. "Gibraltar belongs to the Gibraltarians and we want to stay British."