A federal judge on Thursday largely upheld a measure approved by San Francisco voters last year requiring political advertisements to identify their main sources of funding. The ruling prohibited only disclosure notices so extensive that they drowned out the ad.

Proposition F, which passed with nearly 77% of the vote in November, requires political campaign ads — posted, mailed or broadcast — to state their top three contributors of $5,000 or more, along with how much they gave. That threshold was previously $10,000.

If one of the top three donors was another political group — a common technique that can be used to hide the source — the ads must also reveal the umbrella group’s top two contributors and the amount of their donations.

That secondary-disclosure requirement was a central target of the lawsuit by the Yes on Prop. B Committee, one of the groups supporting an earthquake safety bond on next month’s San Francisco ballot. The committee, which includes allies of Mayor London Breed, argued that the requirement violated its freedoms of speech and association, and said some potential contributors have backed off because they did not want their names in the committee’s ads.

But U.S. District Judge Charles Breyer said the requirement to disclose secondary donors serves the interests of both the government and the public in revealing funding sources of political advertising, and imposes only a “modest burden” on free speech or association.

“Yes on Prop. B is not being forced to associate with anyone,” Breyer said. “What it is required to do is accurately report that it has chosen to associate, at least indirectly, with certain organizations and individuals by taking money from groups they support financially.”

And in this case, he said, the city is requiring the committee to disclose not only that one of its top donors was the United Democratic Club of San Francisco, but also that the United Democratic Club received substantial funding from the San Francisco Association of Realtors and another business group, the Committee on Jobs Government Reform Fund.

As for the size of the disclosures, San Francisco has already agreed not to require them when they would take up more than 40% of a display ad, a mailed notice or audio or video commercials, a concession that exempts smaller promotions such as 5-by-5-inch newspaper ads and 30-second campaign spots.

Breyer agreed that it would violate free speech to require funding disclosures on small ads that would “leave little or no room for the political message.” But he said the U.S. Supreme Court — in its 2010 Citizens United ruling, which allowed corporations to spend unlimited amounts on elections — had also ruled that “a disclaimer may commandeer a prominent position in a political ad without offending the First Amendment.”

In this case, he said, San Francisco’s disclosure notices would take up no more than about 35% of the committee’s advertisements, leaving enough space to communicate its political message.

Attorney Jon Golinger, co-author of the Prop. F measure that required the disclosures, said Breyer “resoundingly rejected the lawsuit’s arguments that forcing secretive super PACs to reveal their top donors in ads has no value.”

Supervisor Gordon Mar, a sponsor of Prop. F, said the ruling reinforced the voters’ decision that “the culture of pay-to-play politics must end.”

The lead plaintiff, Todd David, executive director of the Housing Action Coalition, said he was glad Breyer had found that Prop. F unconstitutionally interfered with some of his committee’s ads, but disappointed that the ruling did not go further.

“Grassroots organizations in future elections will have to deal with these issues on an ad hoc basis which will likely chill their speech,” said David, who is considering an appeal.

Chronicle staff writer Dominic Fracassa contributed to this story.

Bob Egelko is a San Francisco Chronicle staff writer. Email: begelko@sfchronicle.com Twitter: @BobEgelko