SAN FRANCISCO — Edwin M. Lee, the mayor of San Francisco, began his career as a lawyer fighting for public housing tenants in the city’s Chinatown and likes to think of himself as a champion for those who are struggling.

But with his city flourishing and dotted with construction cranes and tech start-ups, he finds himself in an unexpected position: backed by the city’s new and powerful technology elite, and condemned by housing advocates who accuse him of aggravating a shortage of affordable units and making life difficult for middle-class residents increasingly anxious about rising prices.

Perhaps nowhere in America is the debate over income inequality being carried out as fiercely as in San Francisco, where the technology industry’s success has led to a roaring economy, social disruption and widespread protests.

“Probably 25 years ago I would have been in this cultural war on the advocacy side,” Mr. Lee, a Democrat, said in a recent interview, describing himself as “a little bit” surprised by the intensity of the anger.