Substitute Jesus Navas scored a late goal to send Spain through to the Euro 2012 quarter-finals as Group C winners but only after a mighty scare against Slaven Bilic's unfortunate Croatia.
Mario Mandzukic and Ivan Rakitic both had earlier chances to break the deadlock and Croatia kept pushing deep into injury-time as they sought the equaliser that could still have sent them through.
Resolute in defence and surging down the flanks on the counter-attack, Croatia kept the Spaniards at bay for long periods and Vicente Del Bosque's team will be extremely relieved to have come out on top of their group.
In a poor first period, Fernando Torres came closest to breaking the deadlock when he brought a good save out of Croatia goalkeeper Stipe Pletikosa in the 23rd minute.
Although Spain dominated most of the early possession, chances were at a premium, with Jordi Alba blazing wide and Andres Iniesta poking a shot goalwards where it was saved by Pletikosa.
Torres skipped past Gordon Schildenfeld in the 23rd minute before firing straight at the keeper from a tight angle, then moments later Sergio Ramos drilled a long-range effort which Pletikosa held.
But after Gerard Pique shot over the bar, Croatia replied with some pressure of their own, Danijel Pranjic shooting low at Iker Casillas and Mandzukic blazing over the bar from just inside the box.
Croatia had a loud penalty appeal turned down in the 27th minute when Mandzukic's charge down the right flank was brought to an end by a crashing challenge from Ramos on the edge of the box, but referee Wolfgang Stark waved Croatian appeals away.
Knowing they were likely to need a goal to give them a chance of reaching the last eight, Croatia made a brighter start to the second half with a dangerous corner from Rakitic punched away by Casillas.
Iniesta almost threaded a ball through to Torres in the 52nd minute but Croatia were still brighter, and Mandzukic was clearly showing no ill effects from his first-half knock as he stretched the Spanish defence.
And Croatia came agonisingly close to taking the lead in the 59th minute when Luka Modric beat Ramos on the right and crossed into the box where Rakitic's low header was brilliantly saved at point-blank range by Casillas.
Perhaps shaken by the near-miss, Del Bosque responded by replacing the ineffective Torres with winger Navas, as his side sought desperately to regain control of the game.
But Croatia came close again in the 69th minute when two of their substitutes combined, Ivan Perisic finding space on the left to loft a cross which floated inches too high for Nikica Jelavic in front of goal.
Del Bosque introduced Cesc Fabregas who failed to get his shot off in a crowded box in the 78th minute, then Sergio Busquets missed a glorious chance with 10 minutes to go when he hesitated in the box and was dispossessed by Croatia captain Darijo Srna.
Fabregas was having a major influence in calming Spanish nerves and he made the crucial move in the 87th minute when he lobbed Pletikosa to send Iniesta clear, and he squared for substitute Navas to tap home the simplest of chances.