During the colonial period, Mexico was ruled by a viceroy who was appointed by the Spanish crown. The viceroyalty of New Spain was one of the most important and wealthy colonies in the Spanish Empire, and it produced a vast amount of silver and other minerals for Spain. However, the Spanish also imposed a strict social hierarchy on Mexico, with the Spanish at the top and the indigenous peoples at the bottom.
In the early 19th century, Mexico began to fight for independence from Spain. The Mexican War of Independence lasted for over a decade, and it resulted in Mexico gaining its independence in 1821.