A California school district acted properly when it suspended five students who "liked" or commented on racist images on another student's Instagram account that included nooses drawn around the necks of a black student and coach and comparisons of African-American women and students to gorillas, a judge has ruled.

The case raised thorny questions about how strictly schools can regulate student speech and whether "likes" on social media should be treated similarly to the original posts.

U.S. District Judge James Donato said in his decision late Wednesday the five students contributed to disruption at Albany High School in the San Francisco Bay Area.

The judge rejected arguments that the Albany Unified School District violated the students' free speech rights because the offensive posts were on a private account and made off campus.

Donato said the accounts' followers were mainly Albany High School students. The posts pictured students and school employees and created a risk of substantial disruption at the school, the judge said.

"These cases establish that students have the right to be free of online posts that denigrate their race, ethnicity or physical appearance, or threaten violence," he said. "They have an equivalent right to enjoy an education in a civil, secure, and safe school environment."

An attorney for three of the students had no immediate comment. An attorney for the other two, Darryl Yorkey, said "liking" images does not necessarily express approval.

"I think the judge took an opinion that many of us do in the older generations in assuming that a 'like' on its face is just that, 'I like it, I approve of it,'" he said.

He said many of the students who were suspended over the images were Asian and were just trying to shock each other.

"Taken out of that context, it definitely looks really bad," he said. He did not immediately know whether his clients would appeal Donato's ruling.

A phone message left with the school district seeking comment on the ruling was not immediately returned.

A male student identified in court documents only as "C.E." created the account in November 2016 and posted on it over the next several months.

One post showed a screenshot of a female African American student that she had captioned, "I wanna go back to the old way."

C.E. juxtaposed an image on the student's post of a white man beating a black slave hung by his hands, according to Donato's ruling.

School officials learned about the account in March. They called in mental health counselors to help calm students and contacted police over concerns references to lynching, nooses and the Ku Klux Klan in the posts could constitute threats of violence, Donato said.

C.E. was suspended and later expelled. Donato upheld his discipline as well on Wednesday.

The judge said the district went too far when it disciplined four other students. One followed the account but appeared to have done nothing else. Still another commented that the account was "racism solely directed at black people" with an emoji of a laughing face, the judge said.

The judge overturned all four students' suspensions, saying none of them either approved of or adopted any content targeting specific individuals at Albany High School.