Image copyright AFP Image caption Some US schools have already introduced gender neutral bathrooms or allowed students to choose

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.

Image copyright Reuters Image caption Many in North Carolina supported the state's law but there was a commercial backlash

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.

Bathroom battleground