Dean Elgar fell just short of a century as South Africa looked to take charge of the first Test against Sri Lanka in Centurion. (More Cricket News)
The home side reached the close on day two on 317-4 in reply to Sri Lanka's 396 all out, surviving a post-tea wobble that saw them lose three wickets for 20 runs.
Elgar was among the batsmen to fall as the Proteas slumped from 200-1 to 220-4, the opener losing his wicket on 95.
However, Faf du Plessis (55 not out) and Temba Bavuma (41no) saw the hosts to the close without any further setbacks.
Sri Lanka began the day on 340-6 in their first innings but were effectively seven down after all-rounder Dhananjaya de Silva retired hurt earlier in the innings.
All-rounder Dasun Shanaka crunched four sixes to add to a maximum he struck on Saturday as he reached 66 not out, comfortably his highest Test score, before running out of partners. Debutant Lutho Sipamla finished with 4-76 as he wrapped up the tail.
Elgar and Aiden Markram then put on 141 for South Africa's first wicket before the latter departed for 68, playing an aggressive drive outside off stump in keeping with the spirit of his innings but finding Shanaka at gully.
Sri Lanka had rattled along at above four runs an over, and South Africa were hurtling along at a similarly brisk rate.
They looked firmly in control when Elgar nudged a single to bring up the 200 mark, but that grip loosened with the fall of those wickets in quick succession, Rassie van der Dussen (15) nicking a delivery from Lahiru Kumara to Niroshan Dickwella to trigger the mini-collapse.
Elgar's knock ended disappointingly with a loose shot as the left-hander clipped the ball back down the wicket to give Shanaka a return catch, his 130-ball stay having included 16 fours, with a 13th Test century agonisingly out of reach.
Captain Quinton de Kock had raced to 18 from his first 10 deliveries when he was prised out by Sri Lanka debutant Wanindu Hasaranga, edging to slip.
Sri Lanka were hampered, however, by the loss to injury of paceman Kasun Rajitha early in the South Africa innings.
He could only deliver 2.1 overs in the day, and with their options depleted the tourists could not further capitalise on that flurry of wickets, Du Plessis and Bavuma building a platform for day three.