Just as with the Napoleonic Wars, World War I faced Spain with the difficult problem of having close relationships with countries on both sides of the war: Alfonso III was descended from French kings, but Spain had a long tradition of being allied with Austria/Germany too. So Alfonso chose to stay out of the war entirely.
After World War I, in 1931, Alfonso was forced out and people created Spain's first really democratic government, whose new Constitution guaranteed free speech, took power from the Catholic Church, and made all citizens equal, including rich aristocrats and women.
But the world-wide depression put a lot of people out of work and made the new government unpopular. By 1936, General Franco seized power and ignored the new Constitution. The Republicans fought back. All over Spain, people were experimenting with small anarchist and socialist collectives. People who believed in these methods came from all over the world to help the Republicans in Spain. But by 1939, with military help from Hitler's Germany and Mussolini's Italy, Franco had won the civil war.
In return, Franco supported Germany in World War II, but Spain again stayed out of the war. After the war, Franco stayed in power by promising to help Britain, France, and the United States against Russia in the Cold War, and by killing or jailing anyone in Spain who spoke against him.
When Franco died, in 1975, Spain brought back Alphonso's grandson, Juan Carlos, as king, but with very limited powers and a new democratic government. This time democracy was successful, and the people of Spain became richer than before. As in many other democracies, though, no women have held power. In 1993, Spain joined the European Union.