By Anna Isaac and Ben Eisen

U.S. stocks rallied Wednesday, pushing the S&P 500 to the cusp of its first record close since before the pandemic shutdown took effect.

The benchmark index rose 46.66 points, or 1.4%, to 3380.35, just shy of its last record high from Feb. 19. The index briefly topped its prior record close in afternoon trading but pared gains in the session's final minutes.

The Dow Jones Industrial Average climbed 289.93 points, or 1%, to close at 27976.84 and the technology-heavy Nasdaq Composite Index advanced 229.42 points, or 2.1%, to 11012.24.

Stocks have risen in all but one day in August, boosted by the prospect of declining coronavirus cases at a time when the federal government and central banks are still supporting the economy.

"There's optimism right now about an environment where the virus situation gets better but we still have a ton of stimulus in the system, " Ilya Feygin, a managing director at broker-dealer WallachBeth Capital.

Investors are keeping a close eye on lawmakers' negotiations over a new coronavirus-relief package for American households and businesses. Senate Majority Leader Mitch McConnell said talks were "at a bit of a stalemate," dimming appetite for stocks and other risky assets on Tuesday. Still, many investors remain optimistic that a deal will be reached.

"Markets, particularly in the last day or so, seem to be pricing in a stimulus even as lawmakers play down the odds," said Edward Park, deputy chief investment officer at Brooks Macdonald, an investment management firm.

Traders also said the market was supported Wednesday by Democratic presidential candidate Joe Biden's announcement that Kamala Harris would be his running mate. Wall Street veterans widely consider Ms. Harris to be a more moderate choice than others who were in contention for a spot on the ticket.

The steady rise leaves the stock market susceptible to reversal if cases begin to rise again or future stimulus efforts disappoint, traders said. Some have pointed to trading behavior where stocks continually rise with few fundamental drivers, much like during the dot com boom.

"You have a lot of things that are very reminiscent of a late-90s melt-up," said Chris Harvey, head of equity strategy at Wells Fargo & Co. He said he doesn't expect the market to crash but believes stock gains are likely to be muted in the next few months.

Wednesday's gains were led by the technology giants that had dragged indexes lower a day earlier. The sector jumped 2.2% in the S&P 500, with Apple, Amazon.com, and Microsoft all up more than 2%. Tesla shares rose 13% after the electric-car maker on Tuesday said it would enact a 5-for-1 stock split.

In bond markets, the yield on the benchmark 10-year Treasury note gained for a fourth day, ticking up to 0.669%, from 0.657% Tuesday. The yield on reached its highest level since July 6.

Fresh inflation data showed that U.S. consumer prices increased by 0.6% in July, more than the average expectation of 0.3%, according to FactSet.

Gold prices edged 0.1% higher, after the commodity on Tuesday fell by the most since March. Analysts said appetite for gold has been eroded this week by the rise in U.S. government bond yields. The precious metal -- usually viewed as a haven asset that investors flock to when stocks are in tumult -- has climbed this year even as equities advanced.

"There's been a breakdown in the relationship between equities and gold, " said Geoff Yu, senior markets strategist at BNY Mellon. "This happens if you believe there's going to be moderate inflation, driven by an increase in productivity from companies."

The U.S. crude oil benchmark rose 2.5% to $42.67 a barrel.

Overseas, the pan-continental Stoxx Europe 600 closed 1.1% higher, its fourth straight day of gains.

In the U.K., fresh data showed that the country recorded a steeper second-quarter contraction than any other rich nation, signaling that it has suffered the worst economic hit from coronavirus in Europe. The British economy shrank 20.4% in the second quarter, the country's statistics agency said Wednesday. The British pound slipped 0.1% against the dollar and fell 0.5% against the euro.

In Asia, the Shanghai Composite Index fell 0.6%, while Japan's Nikkei 225 rose 0.4%. Hong Kong's Hang Seng Index rose 1.4%.

Write to Anna Isaac at anna.isaac@wsj.com and Ben Eisen at ben.eisen@wsj.com