By Anna Isaac and Ben Eisen

U.S. stocks rallied Wednesday, pushing the S&P 500 index to the cusp of a record last reached before the pandemic shutdown took effect.

The benchmark index rose 1.6% to 3387.62 in afternoon trading, putting it on track for its first closing record since February. The index would need to close above 3386.15 to eclipse the previous high.

The Dow Jones Industrial Average climbed about 1.2%, and the technology-heavy Nasdaq Composite Index advanced 2.3%.

Stocks have risen 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.

The steady rise also leaves the stock market susceptible to reversal if cases begin to rise again or future stimulus efforts disappoint, traders said.

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.

U.S. stock gains were led by the technology giants that had dragged indexes lower Tuesday. The sector jumped 2.3% in the S&P 500, with Apple, Amazon.com, Facebook and Microsoft all up more than 2%. Tesla shares rose 12% 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.66%, from 0.657% Tuesday. The yield on Tuesday reached its highest level since early July.

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 Wednesday, 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 about 2% to $42.44.

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