The loss of the Spanish colonies started with Cuban uprising in 1894 (Grito of Baire or call for revolution) as well as a revolt in the Philippines in 1896. The causes for the revolts were the lack of political rights as well as the poor economic conditions. The United States supported the rebels and United States declared war against Spain after a USA Navy battleship was sunk in the Havana harbor accusing Spain of the sinking.
War was held in the Philippines, Cuba and Puerto Rico and Spain suffered a serious defeat and the Spanish fleets were destroyed. Spain signed in 1898 the Treaty of Paris recognizing the independence of Cuba, Puerto Rico and the Philippines.
The war had bad effects on Spanish economy (exports of sugar) and caused a deep crisis known as regenerations (regenarionationism) which was a political and intellectual movement in favor of solving political and economic problems.