kansas anti lgbt bill
A man waves a rainbow flag, symbolizing gay pride, at a rally of high school students in Washington, June 9, 2014. AFP/Getty Images/Paul J. Richards

Shouts of "Shame, shame, shame," erupted in the U.S. House of Representatives on Thursday as Republican lawmakers narrowly defeated legislation to protect the civil rights of lesbian, gay, bisexual and transgender employees of government contractors.

The measure, an amendment to a veterans and military construction spending bill introduced by New York Democrat Sean Maloney, initially had enough "yes" votes to pass, according to the count in the House chamber.

But House Republican leaders extended the time allowed for the vote as they urged enough party members to change their positions to defeat it by a vote of 213 to 212.

All 183 Democrats who voted backed the amendment, joined by 29 Republicans. All 213 "no" votes were from Republicans. Eight House members - three Republicans and five Democrats - did not vote. Lawmakers are allowed to change their vote in the few minutes between voting and the result being declared.

LGBT rights have been a hot-button issue during the 2016 election season. National politics have featured debate over whether making cakes for same-sex couples violates bakers' religious freedom or whether the government should decide which public bathrooms are used by transgender people.

Democrats chanted "Shame, shame, shame," at Republicans. "House Republicans are so committed to discriminating against LGBT Americans, that they broke regular order to force their members to reverse their votes and support Republicans' bigotry," Nancy Pelosi, the House Democratic leader, said in a statement.

Republicans who backed the provision said it was necessary to protect the religious rights of military contractors and not intended to be discriminatory.

Thursday's emotionally-charged vote came hours after the House late on Wednesday passed a $602 billion defense authorization bill that included "religious freedom" language that critics said would allow contractors to discriminate against LGBT individuals.

The House Rules committee had angered Democrats by voting along party lines late on Tuesday not to allow a vote on an amendment to the National Defense Authorization Act that would have eliminated that language.

The House also backed by 265-159 a separate Democratic amendment to the spending bill that limits the display of the Confederate battle flag on flagpoles at federal veterans' cemeteries.

The measure angered some conservative Republicans from southern states, who argue that the flag is a symbol of their heritage.

Some lawmakers have been seeking to limit displays of the Confederate banner since a racially motivated murder of nine black parishioners at a church in Charleston, S.C. last year.