How do social group boundaries evolve? Does the appearance of a new outgroup change the ingroup's perceptions of other outgroups? We introduce a conceptual framework of context-dependent categorization, in which exposure to one minority leads to recategorization of other minorities as in- or outgroups depending on perceived distances across groups. We test this framework by studying how Mexican immigration to the US affected White Americans' attitudes and behaviors towards Black Americans. We combine survey and crime data with a difference-in-differences design and an instrumental variables strategy. Consistent with the theory, Mexican immigration improves Whites' racial attitudes, increases support for pro-Black government policies and lowers anti-Black hate crimes, while simultaneously increasing prejudice against Hispanics. Results generalize beyond Hispanics and Blacks and a survey experiment provides direct evidence for recategorization. Our findings imply that changes in the size of one group can affect the entire web of intergroup relations in diverse societies.