Mexico gained its independence from Spain through a revolutionary war. This war is generally said to have begun in 1810 and to have ended in 1821. The Mexican bid for independence was greatly helped by events in Europe.
For a long time, Mexican criollos (people of pure Spanish descent but born in Mexico) had been displeased with the Spanish government. The Spanish government had treated them poorly, not letting them have the same rights and privileges as Spaniards born in Spain. In 1808, Napoleon invaded and occupied Spain. This made Mexicans even more eager to become independent from Spain since their mother country was no longer even controlled by Spaniards. In 1810, Miguel Hidalgo, a criollo priest, began a rebellion for equality for all Mexicans (including mestizos and Indians) and for independence. Although Hidalgo was captured and executed, the rebellion continued until it triumphed in 1827 and Mexico was officially independent.