Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

WASHINGTON — Minnesota's century-old restriction on what people can wear to the polling place on election day appeared to be in trouble after a spirited showdown over the law before the U.S. Supreme Court on Wednesday.

A state law bans wearing t-shirts, hats, buttons and any other clothing containing political messages. The state defended the law as necessary to preserve a safe voting environment, free from intimidation.

But a majority of the court seemed to believe that the law sweeps too broadly and is difficult to enforce.

In answer to a series of questions from Justice Samuel Alito, who pressed the state most aggressively, Daniel Rogan of the Hennepin County Attorney's office said the state could ban t-shirts with the text of the Second Amendment or that said simply "NRA" but not a shirt that said "Parkland Strong."

A shirt with a rainbow flag might be allowed, Rogan said, but not if the ballot included a gay rights issue.

A few of the court's liberals said the law has the benefit of keeping the polling place free of outright electioneering or even more subtle intimidation. Justice Stephen Breyer said that state has a legitimate interest in preserving "a place free from the hurly-burly."

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

But another of the court's liberals, Elena Kagan, asked, "Why should a polling place be a place of solemnity like a church?"

Rogan noted that the Supreme Court seems perfectly comfortable banning all forms of expression in some places, including the marble plaza in front of the court's own building.

A majority of the justices seemed prepared to uphold the state's ban only insofar as it prohibits outright expression of support for a candidate or ballot proposition. That would be a follow-on to its 1992 ruling that bans displaying or distributing campaign materials within 100 feet of the entrance to a polling place.

But Minnesota and a handful of other states go further, prohibiting clothes or buttons that mention not only a candidate, a political party, or a ballot issue, but also any group with recognizable political views, such as the Tea Party or MoveOn.Org.

Andy Cilek poses with a Tea Party shirt at his home in Eden Prairie, Minnesota on Feb. 16, 2018. Cilek was one of two voters who defied elections officials after he was asked to cover up a tea-party shirt and button. A Minnesota law that bars voters from wearing political hats, T-shirts, buttons and other apparel to the polls is about to get a look from the Supreme Court. Jim Mone / AP

When Andrew Cilek showed up to vote in 2010 wearing a "Don't Tread on Me" t-shirt and a button that said "Please I.D. Me," he was told to cover them up or take them off before he could cast a ballot. He was finally allowed to vote after a poll worker took down his name and address for possible prosecution.

"That sounds more disruptive, having a discussion about a voter's apparel, than simply wearing the clothes," said Justice Anthony Kennedy.

Delaware, Kansas, Montana, New Jersey, New York, South Carolina, Tennessee, Texas and Vermont have laws similar to Minnesota's, according to the challengers.

The National Association of Counties and other local government groups, in a friend-of-court brief, said delays and fights on Election Day remain common problems. "For all the progress the United States has made in the past century, polling-place problems are still widespread."

If a voter shows up with clothing that election judges decide is prohibited, the voter is asked to remove the item. A voter who refuses can still cast a ballot, but the voter's name is recorded for possible prosecution.

The court will issue a decision in the case by late June.