Unable to invade Britain after the Battle of Trafalgar, Napoleon tried to economically strangle Britain with a European boycott of British goods, creating what he hoped would be a self-sustaining internal European economy: the “Continental System.” By late 1807 all continental European nations, except Denmark, Sweden, and Portugal, had closed their ports to British commerce. But far from buckling under the strain of the Continental System, Britain was getting richer, seizing the remains of the French Empire in the Caribbean and smuggling cheap but high-quality manufactured goods into Europe. Napoleon's own quartermasters (i.e. the officers who purchased supplies) bought the French army's uniforms from the British!
Napoleon demanded that Denmark and Portugal comply with his Continental System. Britain countered by bombarding Copenhagen and seizing the Danish fleet, an example that encouraged the Portuguese to defy Napoleon and to protect their profitable commerce with Britain. Napoleon responded with an invasion of the Iberian peninsula in 1808 (initially an ally of the Spanish monarchy, Napoleon summarily booted the king from his throne and installed his own brother Joseph as the new monarch), which in turn sparked an insurrection in deeply conservative Spain. The British sent a small but effective expeditionary force under the Duke of Wellington to support the insurrection, and Napoleon found himself tied down in a guerrilla war - the term “guerilla,” meaning “little war,” was invented by the Spanish during the conflict.
Napoleon's forces ended up trapped in this new kind of war, one without major battles or a clear enemy army. The financial costs of the invasion and occupation were enormous, and over the next seven years almost 200,000 French soldiers lost their lives in Spain. Even as Napoleon envisioned the further expansion of his empire, most of his best soldiers were stuck in Spain. Napoleon came to refer to the occupation as his "Spanish ulcer," a wound in his empire that would not stop bleeding.
The problem for the French forces was that they had consistently defeated enemies who opposed them in large open battles, but those kind of battles were in short supply in Spain. Instead, the guerrillas mastered the art of what is now called "asymmetrical warfare," in which a weaker but determined force defeats a stronger one by whittling them down over time. The French controlled the cities and most of the towns, but even a few feet beyond the outskirts of a French camp they could fall victim to a sudden ambush. French soldiers were picked off piecemeal as the years went on despite the fact that the Spanish did not field an army against them. In turn, the French massacred villagers suspected of collaborating with the guerrillas, but all the massacres did was turn more Spanish peasants against them. Napoleon poured hundreds of thousands of men into Spain in a vain attempt to turn the tide and pacify it; instead, he found his best troops caught in a war that refused to play by his rules.
Meanwhile, while the Spanish ulcer continued to fester, Napoleon faced other setbacks of his own design. In 1810, he divorced his wife (who had not produced a male heir) and married the princess of the Habsburg dynasty, Marie-Louise. This prompted suspicion, muted protest, and military desertion since it appeared to be an open betrayal of anti-monarchist revolutionary principles: instead of defying the kings of Europe, he was trying to create his own royal line by marrying into one! In the same year, Napoleon annexed the Papal States in central Italy, prompting Pope Pius VII to excommunicate him. Predictably, this alienated many of his Catholic subjects.