WASHINGTON — The Supreme Court’s conservative and liberal justices clashed Tuesday over campaign finance restrictions in a dispute involving candidates for elected state and local judgeships.

Five years after the court’s Citizens United ruling freed corporations and labor unions to spend as much as they want in elections for Congress and president, the court heard arguments in a case from Florida that tests whether states can prohibit judicial candidates from making personal appeals for campaign donations.

The court’s conservative justices prevailed in Citizens United by a 5-4 vote. The same ideological divide was apparent Tuesday in a case that could free judicial candidates in 30 states that elect state and local judges to ask personally for campaign contributions. In all, 39 states hold elections for judges and some allow personal appeals.

Chief Justice John Roberts was among those who suggested that candidates for judicial offices have a broad right to seek contributions.

“The fundamental choice was made by the state when they said we’re going to have judges elected,” Roberts said.

Several liberal justices said lawyers might have a hard time refusing to contribute when a judge personally asks for cash.

Justice Elena Kagan imagined a judge’s letter asking for a donation and reminding the lawyer that he will have future cases in front of the judge. “‘I hope I always will be fair and you know I’m running for judge, and I’d really like a contribution of a thousand dollars,”‘ Kagan said, indicating it might be wise for a sensible attorney to write a check.

Justice Anthony Kennedy could hold the deciding vote, and he said little during the session. He has been a critical voice against campaign finance limits and restrictions on judicial candidates’ speech in the past.

The case of Lanell Williams-Yulee of Tampa, Florida, was before the court Tuesday. Williams-Yulee received a public reprimand for violating a Florida Bar rule that bans candidates for elected judgeships from personally soliciting donations.

Williams-Yulee signed a mass-mailing asking for campaign dollars, and also posted the letter on her website. The appeal didn’t yield a penny, said her lawyer, Andrew Pincus.

Arguing before the court, Pincus said the interest in an impartial judiciary might justify restrictions on face-to-face solicitations, but not the kind of impersonal letter sent by Williams-Yulee.

Candidates can learn who contributed to their campaign and also write personal thank-you notes, undermining the state’s interest in prohibiting the solicitation, Pincus said.

The bar and many good government groups argued that the ban that is in place in Florida and 29 other states is important to preserve public confidence in an impartial judiciary.