WASHINGTON — The Supreme Court ruled on Thursday that Texas did not violate the First Amendment when it refused to allow specialty license plates bearing the Confederate battle flag. Such plates, Justice Stephen G. Breyer wrote for the majority, are the government’s speech and are thus immune from First Amendment attacks.

The vote was 5 to 4. The court’s other three liberal members joined Justice Breyer’s majority opinion, as did Justice Clarence Thomas.

“As a general matter,” Justice Breyer wrote, “when the government speaks it is entitled to promote a program, to espouse a policy or to take a position.” Were this not so, he said, the government would be powerless to encourage vaccinations or promote recycling.

People use specialty license plates to suggest that the government endorses the messages they bear, he wrote. Otherwise, he said, people “could simply display the message in question in larger letters on a bumper sticker right next to the plate.”