A Texas judge has blocked an order by the US government that schools allow transgender students to use bathrooms consistent with their gender identity.
The temporary injunction signed by Judge Reed O'Connor follows a challenge to President Barack Obama's directive by 13 states and applies nationwide.
The injunction was passed the day before many students go back to school.
The right of transgender people to use bathrooms of their choice has become highly controversial in the US.
Judge O'Connor, who was appointed by George W Bush, said schools should have been allowed to have a say before the White House directive was issued.
He said: "This case presents the difficult issue of balancing the protection of students' rights and that of personal privacy... while ensuring that no student is unnecessarily marginalised while attending school."
The injunction does not prohibit schools from allowing transgender students to use bathrooms consistent with their gender identity, but those that don't will not have to conform.
Texas Attorney General Ken Paxton, a Republican, argued that President Obama was "attempting to rewrite the laws enacted by the elected representatives of the people".
The Justice Department issued a brief statement saying it was disappointed in the ruling and was reviewing its options.
Paul Castillo, a Dallas attorney for the gay rights group Lambda Legal, which argued in court against the injunction, said the ruling was the latest attack on transgender rights.
He said: "I think today is going to be a hard day for transgender students. The decision is certainly emotional and certainly an attack on transgender students' dignity.''
Public bathrooms have become a flashpoint in the battle over transgender rights in the US. In March, North Carolina passed a law requiring people to use public toilets that correspond with the sex listed on their birth certificate.
The Justice Department sued the state over the law and US Attorney General Loretta Lynch likened that law to the policies of racial segregation. The state's Republicans argued that the law was necessary for privacy and safety.
With disagreement at state and federal level over whether anti-discrimination laws apply to transgender people, the issue may go to the Supreme Court for resolution.
The lawsuit against President Obama's directive was filed in May by Texas, Alabama, Wisconsin, West Virginia, Tennessee, Oklahoma, Louisiana, Utah and Georgia, and the Republican governors of Maine, Mississippi and Kentucky.
Two small school districts in Arizona and Texas, which have fewer than 600 students combined and no transgender people, also joined the effort.
In 2015, O'Connor granted an order that temporarily blocked federal rules that would have expanded medical leave benefits to some gay couples.
- The US state of North Carolina enacted a law in March which means people must use the toilet that matches the gender on their birth certificates
- Legislators pushed for the so-called Bathroom Bill after the city of Charlotte passed an ordinance allowing transgender people to use restrooms according to gender identity
- The law has prompted a huge backlash from campaigners, businesses and artists, with stars such as Bruce Springsteen and Ringo Starr cancelling concerts in the state
- In May the Obama administration hit back, issuing a directive ordering public schools to allow transgender students to use the toilet that corresponds to their gender identity
- 12 states announced they will sue the federal government over that directive, including Texas, Alabama and Wisconsin
- In August a Texas judge approved a temporary injunction suspending the Obama administration's directive