U.S. investors' overall confidence fell in July, hurt by the housing slump and rising energy costs, but a buoyant stock market provided a silver lining for individual investments, a survey showed on Monday.

The UBS/Gallup Index of Investor Optimism fell to 87 in July after checking in at 89 in June. The index stood at 95 in May.

Investors' views on the U.S. economy in the next 12 months fell even more steeply. The index's Economic Dimension plummeted to 16 in July from 22 the prior month.

But strong stock market gains in the first half of the month actually pushed the Index's Personal Dimension, which measures optimism about investors' personal investment portfolios, up four points in July to 71, UBS said.

The survey sampled 800 investors in households with at least $10,000 in savings and investments. The sampling error was plus or minus 4 percentage points.

Despite the sharp rise in the stock market this summer, investors are showing concern about the health of the overall U.S. economy given the ongoing weakness in the housing market, rising food and energy prices and turmoil in the subprime mortgage market, said Maury Harris, chief U.S. economist at UBS Investment Bank.

Seven in 10 investors say they believe energy prices are hurting the current investment climate a lot, UBS reported. That's a touch below 73 percent in June but still above 63 percent in March and 58 percent in February.

Investors on average reported paying $2.96 per gallon of gas in the first half of July and expect the cost to rise to $3.12 over the next three months.

International tension, the U.S. budget deficit and local real estate markets all remain high on investors' list of concerns, UBS said.