Hales and Bairstow shared an unbroken sixth-wicket partnership of 88 to revive the hosts, who collapsed to 83 for five after Sri Lanka seam bowler Dasun Shanaka picked up three quick wickets on his debut.
Hales moved on 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.
The 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.
Cook, needing 36 runs to become the first Englishman to reach 10,000 in tests, looked in good form until he drove rashly at a wide ball from Shanaka and was caught by wicketkeeper Dinesh Chandimal.
Compton, under pressure for his place at number three, was undone by a full delivery just outside off stump which he edged low to Lahiru Thirimanne at first slip.
Root was caught at gully following another loose drive and James Vince arrived at the crease on his test debut with England reeling at 51 for three.
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 83 for five.
Hales and Bairstow, however, dug in.
The former, trying to cement his place at the top of the order, reached his second test fifty 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.