The decision set new limits on disciplining students for off-campus speech but did not totally bar administrators from doing so.

WASHINGTON — The Supreme Court ruled on Wednesday that a Pennsylvania school district had violated the First Amendment by punishing a student for a vulgar social media message sent while she was not on school grounds.

The decision, on a vote of 8 to 1, did not establish a categorical ban on regulating student speech outside of school, citing the need of school systems to be able to deal with issues like bullying and threats.

Instead, it set out factors that courts should assess in weighing the right of administrators to punish speech in nonschool settings, with one important component being whether parents are better suited to handle the situation.

But it was the first time in more than 50 years that a high school student won a free-speech case in the Supreme Court, and the decision emphasized that courts should be skeptical of efforts to constrain off-campus speech.

Justice Stephen G. Breyer, writing for the majority, said part of what schools must teach students is the value of free speech.

“America’s public schools are the nurseries of democracy,” he wrote. “Our representative democracy only works if we protect the ‘marketplace of ideas.’”

“Schools have a strong interest in ensuring that future generations understand the workings in practice of the well-known aphorism, ‘I disapprove of what you say, but I will defend to the death your right to say it,’” he wrote.

Justice Clarence Thomas dissented.

The ruling came at a time when social media has complicated issues of free speech for students, giving wide circulation to opinions, comments, gossip and other utterances that might otherwise attract little notice. In its ruling, the court appeared to acknowledge that it needed to set some boundaries on the power of school systems to decide what was appropriate in the current era.

“The opinion reaffirms that schools’ authority over the lives of students is not boundless,” said Justin Driver, a law professor at Yale and the author of “The Schoolhouse Gate: Public Education, the Supreme Court and the Battle for the American Mind.”

But he added that the nuanced ruling, which raised as many questions as it answered, “offers little in the way of clarity to students, educators or lower-court judges.”

The case concerned Brandi Levy, a Pennsylvania high school student who had expressed her dismay over not making the varsity cheerleading squad by sending a colorful Snapchat message to about 250 people.

She sent the message on a Saturday from the Cocoa Hut, a convenience store popular with teenagers. It included an image of Ms. Levy and a friend with their middle fingers raised, along with a string of words expressing the same sentiment. Using a swear word four times, Ms. Levy objected to “school,” “softball,” “cheer” and “everything.”

Though Snapchat messages are meant to vanish not long after they are sent, another student took a screenshot and showed it to her mother, a coach. The school suspended Ms. Levy from junior varsity cheerleading for a year, saying the punishment was needed to “avoid chaos” and maintain a “teamlike environment.”

Ms. Levy sued the school district, winning a sweeping victory from a divided three-judge panel of the United States Court of Appeals for the Third Circuit, in Philadelphia. The court said the First Amendment did not allow public schools to punish students for speech outside school grounds, relying on a precedent from a different era.

In 1969, in Tinker v. Des Moines Independent Community School District, the Supreme Court allowed students to wear black armbands to protest the Vietnam War, saying the students had not “shed their constitutional rights to freedom of speech or expression at the schoolhouse gate.” But disruptive speech, at least on school grounds, could be punished, the court added.

Though the Third Circuit was united in ruling for Ms. Levy, the judges disagreed about the rationale. The majority announced a categorical rule banning discipline for off-campus speech that seemed to limit the ability of public schools to address many kinds of disturbing communications by students on social media, including racist threats and cyberbullying.

Justice Breyer wrote that the appeals court had gone too far.

“Unlike the Third Circuit,” he wrote, “we do not believe the special characteristics that give schools additional license to regulate student speech always disappear when a school regulates speech that takes place off campus. The school’s regulatory interests remain significant in some off-campus circumstances.”

“These include,” he wrote, “serious or severe bullying or harassment targeting particular individuals; threats aimed at teachers or other students; the failure to follow rules concerning lessons, the writing of papers, the use of computers, or participation in other online school activities; and breaches of school security devices, including material maintained within school computers.”

Those issues required a more cautious approach than the one adopted by the appeals court, Justice Breyer wrote.

“We do not now set forth a broad, highly general First Amendment rule stating just what counts as ‘off campus’ speech and whether or how ordinary First Amendment standards must give way,” he wrote. Schools, he said, can address, for instance, “substantial disruption of learning-related activities or the protection of those who make up a school community.”

Instead of applying a categorical rule, Justice Breyer said three factors should make courts wary of allowing schools to supervise what students say off campus: Parents rather than administrators are better suited to disciplining children away from school, the specter of round-the-clock surveillance is at odds with free speech values, and schools should teach students that unpopular speech is worthy of protection.

First, he wrote, “geographically speaking, off-campus speech will normally fall within the zone of parental, rather than school-related, responsibility.”

Second, he wrote, “courts must be more skeptical of a school’s efforts to regulate off-campus speech, for doing so may mean the student cannot engage in that kind of speech at all.” He added that some kinds of expression must be treated with special sensitivity: “When it comes to political or religious speech that occurs outside school or a school program or activity, the school will have a heavy burden to justify intervention.”

Finally, he wrote, schools should teach students that the free exchange of ideas “facilitates an informed public opinion, which, when transmitted to lawmakers, helps produce laws that reflect the people’s will.”

Ms. Levy’s speech, Justice Breyer wrote, included a critique worthy of protection. “Putting aside the vulgar language,” he wrote, “the listener would hear criticism, of the team, the team’s coaches and the school — in a word or two, criticism of the rules of a community.”

It mattered, too, where and how she spoke, his opinion said.

“Her posts appeared outside of school hours from a location outside the school,” Justice Breyer wrote. “She did not identify the school in her posts or target any member of the school community with vulgar or abusive language.”

Ms. Levy “also transmitted her speech through a personal cellphone, to an audience consisting of her private circle of Snapchat friends,” Justice Breyer wrote.

In dissent, Justice Thomas wrote that “the majority fails to consider whether schools often will have more authority, not less, to discipline students who transmit speech through social media.”

“Because off-campus speech made through social media can be received on campus (and can spread rapidly to countless people),” Justice Thomas wrote, “it often will have a greater proximate tendency to harm the school environment than will an off-campus in-person conversation.”

In a concurring opinion at the appeals court, Judge Thomas L. Ambro wrote that he would have ruled for Ms. Levy on narrower grounds than the broad approach adopted by the majority. It would have been enough, he said, to say that her speech was protected by the First Amendment because it did not disrupt school activities. The majority was wrong, he said, to protect all off-campus speech.

The Supreme Court largely endorsed Judge Ambro’s approach.

David Cole, the legal director of the American Civil Liberties Union, which represented Ms. Levy, said the case was a triumph for the First Amendment. “Protecting young people’s free speech rights when they are outside of school is vital,” he said, “and this is a huge victory for the free speech rights of millions of students who attend our nation’s public schools.”

For her part, Ms. Levy expressed satisfaction with the Supreme Court’s ruling.

“The school went too far, and I’m glad that the Supreme Court agrees,” said Ms. Levy, who is now a college student. “I was frustrated, I was 14 years old, and I expressed my frustration the way teenagers do today. Young people need to have the ability to express themselves without worrying about being punished when they get to school.”