Spain stayed neutral in the First World War. In 1921 a Spanish army was routed by Moroccan fighters at Annual, and in 1923 General Miguel Primo de Rivera (dictator of the 1920s) seized power with the support of King Alfonso XIII (king until 1931). He built roads and dams but lost support and resigned in 1930. After republicans won the town council elections of April 1931, the king left the country and the Second Republic was proclaimed.
The Republic separated Church and state, gave women the vote, began land reform, granted Catalonia self-government and cut the army’s officer corps. Its reforms delighted some and enraged others. A miners’ revolution in Asturias in 1934 was crushed by the army, and in February 1936 a left-wing Popular Front narrowly won the elections. Political killings followed on both sides.

On 17–18 July 1936 army officers rose against the Republic. The coup succeeded in about half the country and failed in Madrid, Barcelona and much of the east and north. General Francisco Franco (general and dictator), whose Army of Africa was flown across the strait with German and Italian help, became leader of the Nationalists and was named head of state in October 1936. The Republic was helped by the Soviet Union and by volunteers of the International Brigades.
Both sides killed civilians behind the lines. In 1937 German and Italian aircraft destroyed the Basque town of Guernica, which Pablo Picasso (painter of Guernica) painted. Franco’s forces took the north in 1937, cut the Republic in two in 1938 and won the Battle of the Ebro. Barcelona fell in January 1939 and Madrid on 28 March, and Franco declared the war over on 1 April 1939. Estimates of the dead, in battle and behind the lines, run to several hundred thousand, and around half a million refugees crossed into France.