Why was Britain willing to return Hong Kong to China, but not Gibraltar to Spain? Was it because Gibraltar has more strategic value than Hong Kong?
Despite common misconception, both Hong Kong Island and the Kowloon Peninsula were ceded to the United Kingdom in perpetuity, via the Treaty of Nanking and the Convention of Peking, respectively. London was under no legal obligation to return them to China. However, most of the Crown Colony of Hong Kong actually consists of the New Territories. That was indeed a 99-year lease granted by the Convention for the Extension of Hong Kong Territory of 1898.
By the time lease was coming up in the late 20th century, the three regions of Hong Kong were so integrated in their development that separation was impractical. Telecommunications, transportation links, utilities were all interlinked, and an artificial separation would have brought economic chaos. Meanwhile, China made it clear that it wished to resume sovereignty over the entirety of Hong Kong - both leased Chinese territory and British supposedly sovereign possessions. And that it was quite capable of doing so by force.
Britain was not exactly willing to return Hong Kong. London did, however, understand the changed realities on the ground. The British Empire had faded into history, the Royal Navy no longer mistress of the seas. Nor was Communist China still the crumbling sick man of Asia under Manchurian rule or Maoist mismanagement. If push came to shove, Britain could not hope to defend Hong Kong any better than the Portuguese at Goa.
Thus, Britain had no realistic choice but to agree to return Hong Kong in 1984 after two years of negotiations. Rather than face the humiliation of a forcible Chinese takeover, it negotiated a relatively
face-saving graceful exit that pretends attempts to safeguard some rights for Hong Kong residents.
Gibraltar was ceded to Britain in perpetuity by the Treaty of Utrecht. With over three hundred years of history under her belt, the United Kingdom is under no legal obligation to return any part of it. A key difference with the Hong Kong cessions is that neither Spain nor Britain wish to fight a war over Gibraltar. From a realpolitik perspective, compared to Hong Kong, Gibraltar is far more defensible, and Spain considerably less formidable.
Moreover, the British position is particularly strengthened by popular mandate, which are slightly more influential in western liberal democracies than
jingoistic pseudo-communist dictatorships.
I find it interesting that many answers belong to "armchair generals", that point only to the military power, and logistics,¹ but forget to mention the issues that could lead to a war in the first place. Remember, war is the continuation of politics....
First of all, I would point the main issue would be noticing how these issues affect the internal politics of every country.
To China, Western colonies in its former territories are a bitter reminder of the humiliations and invasions suffered at the hands of the Western powers first, and the Japanese later. It is a rallying point.
To Spain, Gibraltar is mostly just that, another neighbour. Apart from some hard-liners, there is no real interest in a conflict to get it. Most of the issues between Gibraltar and Spain are the regular issues of having a neighbour (Gibraltar vessels attacking Spanish fishing vessels, Gibraltar being used for smuggling drugs and cigarettes, Gibraltar as a fiscal paradise²).
Of course, the official line is that Gibraltar is Spanish and should be returned, but nobody really acts on it. Also, a hard-liner stance against "colonies" would have the issue of compromising the position of Spain in Ceuta and Melilla, Gibraltar-like cities in Morocco.
Second: the value
Hong Kong is an international financial hub, site of many important companies and an important source of GDP.
Gibraltar is a rock with a town of some thousands people, with no real industry . It may have had some strategical importance in defending the UK possessions in the Mediterranean and the route to India, but now India and the UK possessions are long gone. Real value: that of the land, and thanks.
Spain is part of EU and depends of it for most of its trade, a war of conquest would be unthinkable. Even if it was a "military ride", the diplomatic and economic fallout of a possible expulsion from the EU (not to mention the loss of British tourism income) would make it unprofitable even to win a war to conquer Gibraltar.
On the other hand, China's diplomacy and economic relationships are way more varied. Facing economic reactions from the UK and EU would be hard, but not that crippling. And they have a lot of economic influence they can leverage.
So, we have:
A country that sees recovering Hong Kong as a matter of national pride, which has the potential to win a big economic hub and industrial port and that risks losing only a portion of its trade as a result.
A country that has no real issues with Gibraltar being British, which in case of conflict could win just a small town and some beaches and that by doing so would severely hurt relationships with its closest allies and partners....
I mean, there is no need to count the tanks and aircraft to see why the UK would not be as pressured by Spain as by the Chinese. The comparison is just moot.
¹ Not to mention stupid remarks like "We are gonna nuke Spain because we have big booombs".
² Not taking a stance here about how much truth is with these issues, just pointing out that none of the hot issues are related to sovereignty.
Gibraltar was defensible, but Hong Kong was not. In the 1990s, the distance from the UK would have made Hong Hong impossible to defend. Also, Hong Kong is primarily occupied by Chinese, unlike Gibraltar, which is occupied by UK citizens, many of whom are soldiers. Gibraltar is a fortress, and it is relatively close to the UK and hence easily suppliable.
Spain is much weaker than the UK militarily. It has less population and the UK has almost double the GDP of Spain. The armed forces of the UK are far more numerous and advanced than those of Spain. The UK has nuclear weapons; Spain does not. The Royal navy is much, much more powerful than Spain's navy. Most of Spain's ships are just coast guard vessels. Just one of the submarines in the Royal Navy could sink every ship in the Spanish fleet.
The Spanish, in alliance with the French, once tried to seize Gibraltar in 1782 while the UK was busy fighting the American rebellion. This attack failed completely. By 1985, China, on the other hand, was more than strong enough to storm Hong Kong and have a near certainty of success. In fact, even in the 1950s, at the time of the Korean War, it would have been difficult for the UK to have held Hong Kong, if China had attacked.
One other aspect to this discussion is water. Once the UK had returned the leased territories (as they would have had to do), all China needed to do was turn off the tap supplying water to the rest of HK. (Where do you think it came from?) It is unlikely that the rest of HK would have lasted more than a few days - no tanks (yes, I did see the pun) needed.
Gibraltar, with a smaller population, could easily be supplied with water. There are large reservoirs inside the rock for this reason.
For an historical reference, consider Singapore in 1942. Once the Japanese controlled the water reservoirs on the Malaysian peninsular, its fall was inevitable.
In addition to realpolitik and defensive considerations mentioned above, here are two more reasons:
Gibraltar has been under British rule for significantly longer than Hong Kong was, both in absolute terms and (more significantly) in relative terms. Hong Kong was British for 100-150 years and Chinese for 2000, while Gibraltar has now been British for 310 years, Spanish for 242 and Muslim for 727. Combined with its relative closeness to the mainland and the its 'European' nature, this makes it appear less 'colonial' from the British (if not Spanish) perspective.
Due to migration from Britain, Italy and North Africa, the population of Gibraltar is ethnically far more separate from that of Spain than the population of Hong Kong ever was from that of China. Indeed, Hong Kong saw significant migration from China during the British presence (and 94% of the population is ethnically Chinese), while Spanish migration and interactions in Gibraltar largely stopped under Franco (meaning that even the 26% of Gibraltarians with Spanish surnames often don't identify as such). Again, this makes Hong Kong more obviously 'colonial' from a British perspective.
In 1984, Margaret Thatcher and Deng Xiaoping negotiated the British handover of Hong Kong to China 15 years later. Recently released archival documents suggest that one of Thatcher’s prime concerns was to further British business interests in China.
From a recent news report:
Around six weeks before her trip, the British embassy in Beijing wrote to the Foreign Office urging that Thatcher should ‘press for greater export and investment opportunities for British industry under the signboard of greater participation in China's modernisation.’
It added: ‘It will be important however to handle this in public in such a way that Hong Kong opinion does not see our moves to develop trade as exploitation of the Hong Kong agreement.’
In a nod to this concern, officials decided that Thatcher should not travel with a party of British businessmen to avoid suggestions ‘that we were now getting our prize for having sold out Hong Kong to the Chinese,’ Peter Ricketts, a senior Foreign Office civil servant, wrote to Thatcher advisor Charles Powell on November 16, 1984.
During the trip, business deals including around the development of a nuclear power station in Guangdong were discussed, the files reveal.
Another viewpoint, different from the other answers of "Britain could defend Gibraltar but not Hong Kong" is, who would we return it to?
Hong Kong was / is by and large made up a Chinese or cultural Chinese populace, with no real ties / history to the UK.
Gibraltarians on the other hand, whether the Spanish agree or not, see themselves as British, not Spanish. The culture is predominantly a British culture, they speak English, it's been under British rule / protection / association for 300+ years.
"Returning" it to Spain is like saying we'll return Cumbria to Scotland.
Gibraltar is much closer to home, and much more important to Britain than Hong Kong. Gibraltar is literally the gateway to the Mediterranean, all of southern Europe, and large parts of Africa and the Middle East. Hong Kong has nowhere a comparable level of strategic significance, at least to Britain, because it is halfway around the world.
Gibraltar is also much easier to defend. Besides shorter lines of communications, Britain is much stronger than Spain (similar populations, but Britain is much better armed) and much weaker than China (which has a much greater population, and is similarly armed).