Hales and Bairstow shared an unbroken sixth-wicket partnership of 88 to revive the hosts, who collapsed to 5-83 after Sri Lanka seam bowler Dasun Shanaka picked up three quick wickets on his debut.
Hales moved to 71 not out, his highest Test score, with Bairstow unbeaten on 54 at tea and persistent rain prevented any play in the final session to leave the match finely poised.
Sri Lanka captain Angelo Mathews won the toss in overcast conditions at Headingley and England openers Alastair Cook and Hales negotiated the first hour with few alarms in a partnership of 49.
The game changed, however, when Shanaka came on.
Bowling a full length, the 24-year-old dismissed captain Cook for 16 with his seventh delivery before removing Nick Compton and Joe Root for ducks in an inspired spell.
James Vince survived to lunch and hit two fluent fours after the interval on his way to nine, but he perished when another drive off Shaminda Eranga flew straight to Kusal Mendis at gully.
Ben Stokes played in typically aggressive fashion, striking three fours in his 12, but he carelessly chipped Nuwan Pradeep to Mathews at mid-on to leave England tottering on 5-83. Hales and Bairstow, however, dug in.
The former, trying to cement his place at the top of the order, reached his second Test half-century and struck 12 fours, while Bairstow was his usual busy self at the crease.
The wicketkeeper, who survived two confident lbw appeals following reviews, launched spinner Rangana Herath over long-on for six before posting a half-century for the seventh time in Tests.
After play, the debutant Shanaka was trying to keep a level head after making a flying start to his time in Test cricket.
“It’s a great start to my career, the conditions suited my bowling,” said Shanaka, who has figures of 3-30 from nine overs.
As for claiming England great Cook as his maiden Test scalp, the all-rounder added: “I will remember the moment I got that wicket until the end.”