Mexico gained independence from Spain in 1821. Antonio Lopez de Santa Anna became president in 1833 and led Mexico during the Mexican-American War from 1846 to 1848, when Mexico lost territory to the U.S. Napoleon III of France sent large armies to Mexico from 1862 to 1867. Porfirio Diaz took control of Mexico in 1876 by ousting the president and remained a powerful leader until 1911.