WASHINGTON — How does this happen?

On Wednesday, the House passed a $1.2 trillion spending bill by a lopsided margin of 309 to 118. A majority of both Democrats and Republicans supported the measure, which sailed through the Senate.

House Minority Leader Nancy Pelosi, D-Calif., and House Speaker Paul Ryan, R-Wis., alike celebrated, and President Trump said of the spending bill: “This is what winning looks like!”

Yet the day after this bill passed the House, the chamber split down the middle as it passed the “Trumpcare” legislation repealing Obamacare. All 193 Democrats and 20 Republicans opposed the bill, which survived by a margin of 217 to 213 and goes to the Senate, where it is so toxic that lawmakers there don’t even plan to consider it.

Pelosi bitterly told Republicans they would have the bill “tattooed on your forehead. You will glow in the dark on this one.” Democrats sang “Hey, hey, hey, goodbye” to Republicans on the House floor.

How could the people’s representatives swing, in the space of 24 hours, from bipartisan congratulations to partisan recriminations? The difference was in Trump’s approach.

In the case of the spending bill, he acted as president of all Americans. He negotiated a compromise that thrilled nobody but satisfied the vast majority. In the case of the health care bill, he catered to the most extreme elements of the Republican Party, the 30-or-so members of the House Freedom Caucus, winding up with a radical and slapdash bill that would be at direct odds with what Americans say they want.

Democrats crowed, a bit too much, about the spending agreement, which avoided funds for a border wall and a deportation force and avoided the defunding of sanctuary cities and Planned Parenthood. It didn’t embrace Trump’s proposed non-defense cuts. Pelosi bragged that “we handed President Trump a resounding defeat.”

That wasn’t entirely true. Trump got spending increases for defense and for border security, and more school choice in the form of the D.C. Scholarship Opportunity Program. Trump called the bill “a clear win for the American people.”

Unfortunately, that wasn’t the lesson he wound up taking away from the experience. An aide said Trump was angry that Democrats were “spiking the ball” by declaring victory. Trump tweeted that, when the current agreement expires, “Our country needs a good ‘shutdown’ in September to fix mess!”

This is exactly the wrong instinct, because such thinking created the Trumpcare abomination on Thursday. Republicans hypocritically jammed the bill through the House before members could read it or it could be given a price tag. They would eliminate popular protections for people with pre-existing conditions, make older people pay more, and give huge tax breaks to the wealthy. They would even eliminate protections against catastrophic costs for people on their employers’ plans.

Thus did Trump and congressional leaders cause House Republican lawmakers to walk the plank on health care, passing a bill that the Senate won’t even consider, just to satisfy 30 extremists from safe districts.

And it was unnecessary. For all the partisan fireworks, there is a health care policy consensus out there for the taking. A Kaiser Family Foundation poll last month found a broad consensus on many health care policies that crosses party lines: allowing the federal government to negotiate Medicare drug prices (92 percent), making it easier to bring generic drugs to market (87 percent) and requiring drug companies to disclose how they set prices (86 percent). The major elements of Obamacare, except for the individual mandate, are broadly popular. What Americans want, across party lines, is what Obamacare didn’t do well: making overall health care and prescriptions cheaper overall.

In fact, on most issues, there is more consensus among Americans than most people realize. Research by the centrist group Third Way finds there is a consensus in favor of renewable energy (if it isn’t paired with rules cracking down on fossil fuels), citizenship for illegal immigrants (if paired with crackdowns on border security and illegal hiring), American leadership abroad (if it doesn’t mean boots on the ground) and universal background checks on gun sales (if there aren’t gun bans).

The problem is the consensus on these and many other issues breaks down when it is filtered through the lens of party. Once parties take sides, Americans take cues from leaders and shift their views to get in line.

If Trump, who only recently became Republican, wants to win until we’re tired of winning, there’s an obvious path to consensus. Alternatively, he and the members of the Freedom Caucus can celebrate the virtues of partisan polarity and ideological purity — in a very small room.

Follow Dana Milbank on Twitter, @Milbank.(c) 2017, Washington Post Writers Group

graphic: shutterstock.com

Dana Milbank, Washington Post Columnist