Voters wait for ballots at a polling station in Hillsboro, Virginia, on Tuesday. Americans voted in midterm elections that marked the first major voter test of President Trump’s presidency. Democrats took control of the House of Representatives, while Republicans gained a larger majority in the Senate. (Andrew Caballero-Reynolds/Agence France-Presse/Getty Images)

Democrats took over the majority in the House of Representatives from President Trump’s Republican Party on Tuesday, but Republicans gained ground in the Senate and preserved key governorships.

The mixed verdict in the first nationwide election of Trump’s presidency revealed the limits of his attempts to worry Americans about immigration issues. College-educated voters in the nation’s suburbs rejected his warnings of a migrant “invasion.” But blue-collar voters and rural America embraced his aggressive talk and stances.

The new Democratic House majority will end the Republican Party’s dominance in Washington for the final two years of Trump’s first term with major questions looming about health care, immigration and government spending.

“Tomorrow will be a new day in America,” declared House Minority Leader Nancy Pelosi, who would be in line to become the next House speaker.

But the Democrats have a narrow edge. They need 218 seats for a majority. Democrats have won 219 and the Republicans 193, with winners undetermined in 23 races.

The president’s party will maintain control of the executive branch of the government, in addition to the Senate, but Democrats will have a foothold that gives them the power to examine Trump’s administration and his long-withheld tax returns.

Students from Vista College Preparatory School cheer voters on at a polling station in Phoenix, Arizona, on Tuesday. (Dominic Valente/For The Washington Post)

Early Wednesday, Trump sent a warning.

“If the Democrats think they are going to waste Taxpayer Money investigating us at the House level,” Trump tweeted, “then we will likewise be forced to consider investigating them for all of the leaks of Classified Information, and much else, at the Senate level. Two can play that game!” It wasn’t clear what “leaks” he was referring to.

It could have been a much bigger night for Democrats, who suffered stinging losses in Ohio and in Florida, where Trump-backed Republican Ron DeSantis ended Democrat Andrew Gillum’s bid to become the state’s first African American governor.

The 2018 elections also exposed an extraordinary political realignment in an electorate defined by race, gender, and education that could shape U.S. politics for years to come.

The GOP’s successes were fueled by a coalition that’s decidedly older, whiter, more male and less likely to have college degrees. Democrats relied more upon women, people of color, young people and college graduates.

Record diversity on the ballot may have helped drive turnout.

Voters were on track to send at least 99 women to the House, shattering the record of 84. The House was also getting its first two Muslim women, Massachusetts elected its first black congresswoman, and Tennessee got its first female senator.

Overall, women voted considerably more in favor of congressional Democratic candidates — with fewer than 4 in 10 voting for Republicans, according to VoteCast, a nationwide survey of more than 115,000 voters and about 20,000 nonvoters.

In House races, Democrats flipped seats in districts outside of Washington, Philadelphia, Miami, Chicago and Denver. The party’s dreams of a Senate majority, always unlikely, were shattered after losses in Indiana, Missouri, Tennessee, North Dakota and Texas.

Trump encouraged voters to view the 2018 midterms as a response to his leadership, pointing proudly to the surging economy at his recent rallies. Nearly 40 percent of voters cast their ballots to express opposition to the president, according to VoteCast, while one-in-four said they voted to express support for Trump.