During the reign of Carlos IV Spain declared war to revolutionary France, but the prime minister Godoy established good relations with France under Napoleon.
In this context, Spain joined Napoleon in his war against Great Britain (defeat at the Battle of Trafalgar, 1805) and against Portugal (sign of the Treaty of Fontainebleau, 1807).
He moved the royal family to Bayona after a popular revolt against Godoy (Mutiny of Aranjuez, 1808) and crowned his brother as José I Bonaparte (Abdications of Bayona, 1808).
In response to this event, a patriotic feeling spread across the country and the War of the Spanish Independence started in 3 May 1808 and lasted until 1814.
The majority of the Spanish population considered Fernando VII, son of Carlos IV, as the legitimate king, while a minority, mainly composed of intellectuals, supported the new monarchy of José I.
Finally, the French troops were defeated and Napoleon signed the Treaty of Valençay in 1813, in which he recognised Fernando VII as King of Spain and the Indies.
The Ley Sálica prevented women from inheriting the crown if there was a male heir available, which in this case was the Infante Carlos María Isidro, brother of Fernando VII.
María Cristina, the mother of Isabel, looked for support between the liberals (isabelinos), while the the absolutists supported the succession of Don Carlos (carlistas)
During the reign of Isabel II, the liberal State was constructed and consolidated, being conservative in character but organised in two political parties: the Moderados and the Progresistas.
The principles of liberalism were shared by both parties but they disagreed on matters such as the role of the crown in politics, the Church, the suffrage, and the economy.
The monarchy of Isabel II was marked by the confrontation between the two liberal ideologies, the intervention of the crown and the Army in political affairs, and the marginalisation of most of the population from politics.
During the regencies of María Cristina and Espartero, the Primera Guerra Carlista developed, ending in favour of the isabelinos in 1839 through the Convenio de Vergara.
The Constitution of 1837, of progressive ideology, was also approved during this period, as well as the desamortización of ecclesiastical lands of Mendizábal.
The disputes between liberals forced both regents to resign, and in order to avoid a new regency, the Cortes proclaimed Isabel II of age when she was 13 years old.
During the direct reign of Isabel II, the liberal regime was consolidated and reforms were applied to try to modernise the country, with mixed results.
The Moderados were in power most of the time with the support of the queen, and they passed a new moderate Constitution and the Reforma fiscal of Mon and Santillán in 1845, a Concordato con la Santa Sede was signed in 1851, and the Guardia Civil was created in 1844.
The Progresistas were able to hold power for some time, and they passed the Ley General de Desamortización of Madoz and Ley General de Ferrocarriles in 1855, as well as some bank laws, and redacted a new progressive Constitution in 1856 (it was never approved).
Despite the triumph of liberalism, the political discontent among Progresistas and other political forces, like democrats, led to the Pacto de Ostende in 1866 to end the monarchy of Isabel II.
The six-year democratic period (1868-1874) was marked by the attempt to democratise the liberal regime in Spain and to exile the Borbones, considered the cause of the problems of the nation.