Before the dispute began, Spain controlled Texas as part of its New Spain colony. The Spanish empire declined in the early 19th century, and Texas gained its independence from Spain in 1836. Mexico recognized the independence of Texas in 1845, but the US did not. The US claimed that Texas was part of its territory, and it annexed Texas in 1845.
Mexico was unhappy that the US had annexed Texas, and it claimed that the border between the two countries should be the Rio Grande. The US claimed that the border should be the Nueces River, which is located much further north. The dispute over the border between the US and Mexico led to the Mexican-American War (1846-1848).
The Mexican-American War ended with the Treaty of Guadalupe Hidalgo, which settled the border dispute. The treaty set the border between the US and Mexico at the Rio Grande, and it also gave the US control over the Mexican Cession, which included present-day California, Nevada, Utah, and parts of Arizona, Colorado, and New Mexico. In return, the US paid Mexico $15 million.