U.S. Code › Title 36 › Subtitle I › Part A › Chapter 3 › § 301
36 U.S. Code § 301 – National anthem
(a)Designation.—
The composition consisting of the words and music known as the Star-Spangled Banner is the national anthem.
(b)Conduct During Playing.—During a rendition of the national anthem—
(1) when the flag is displayed—
(A) individuals in uniform should give the military salute at the first note of the anthem and maintain that position until the last note;
(B) members of the Armed Forces and veterans who are present but not in uniform may render the military salute in the manner provided for individuals in uniform; and
(C) all other persons present should face the flag and stand at attention with their right hand over the heart, and men not in uniform, if applicable, should remove their headdress with their right hand and hold it at the left shoulder, the hand being over the heart; and
(2) when the flag is not displayed, all present should face toward the music and act in the same manner they would if the flag were displayed.
https://www.law.cornell.edu/uscode/text/36/301
“The Star-Spangled Banner” was recognized for official use by the United States Navy in 1889, and by U.S. President Woodrow Wilson in 1916, and was made the national anthem by a congressional resolution on March 3, 1931 (46 Stat. 1508, codified at 36 U.S.C. § 301), which was signed by President Herbert Hoover.
https://en.wikipedia.org/wiki/The_Star-Spangled_Banner
h/t MaximusRex