Faf du Plessis and Temba Bavuma built on Dean Elgar and Aiden Markram's opening partnership of 141 to help South Africa get within 79 runs of Sri Lanka's first innings total of 396 on the second day of the first Test on Sunday.
Du Plessis was on 55 and Bavuma on 41 as stumps were called on Day 2 in Centurion. The pair have put up 97 for the fifth wicket and South Africa will start Day 3 on 317/4.
Earlier, Dasun Shanaka scored an unbeaten 66 to take Sri Lanka close to the 400-run mark. However, the visitors lost frontline fast bowler Kasun Rajitha as he pulled up short during his third over of the innings. Coupled with Dhananjaya de Silva being ruled out of the series on Day 1, Sri Lanka leaked runs and Elgar and Markram made the most of the situation.
Their partnership was finally ended by Vishwa Fernando after which Sri Lanka dismissed Rassie van der Dussen and Quinton de Kock in relatively quick succession. Elgar fell to Shanaka just five runs short of a century.
However, du Plessis and Bavuma then dropped anchor and saw South Africa through to stumps.
Brief scores: Sri Lanka 396 all out (Dinesh Chandimal 85, Dhananjaya de Silva 79; Lutho Sipamla 4/76) vs South Africa 317/4 (Dean Elgar 95, Aiden Markram 68; Dasun Shanaka 1/56)