Jesus Navas scored a dramatic late winner as Spain produced a nervy and unconvincing performance to qualify for the quarter-finals of Euro 2012.
The Seville winger walked in Andres Iniesta's clever pass two minutes from time to break Croatia hearts and ensure the champions top Group C.
Spain had come within a single goal of leaving the tournament but ultimately it was Croatia who missed out.
The world champions could face England, France or Ukraine in the last eight.
The landscape of the group might have been so different had Croatia scored the goal they had threatened to for long periods of the second half, however.
On a night of tension and frustration in Gdansk, Slaven Bilic's men stretched their more celebrated opponents time and again with Luka Modric tormenting the Spanish midfield with a vibrant, menacing performance.
The Tottenham Hotspur midfielder created the best chance of the match for Croatia on the hour mark, finding Ivan Rakitic from a scintillating cross delivered with the outside of his right boot which the Seville midfielder could only head directly at Spain goalkeeper Ilker Casillas.
Spain march on, however, now unbeaten in 17 competitive matches. But this was a performance that epitomised their faltering progress here: style but no substance, beauty but no brawn, panache but no penetration.
Much of the talk before the game had centred on Italy's fear that Spain and Croatia would contrive a 2-2 draw - a result that would have ensured both teams qualified for the quarter-finals at the Azzurri's expense. But at no stage did either side look capable of mustering two goals.
Bilic, in his last match as Croatia coach, had sent his side out intent on showing discipline and total determination to resist the mesmerising quality of Spain's football.
And for much of the opening 45 minutes, Croatia were outpassed, if not outclassed. But they stood firm, with the twin towers of Vedran Corluka and Gordon Schildenfeld at the heart of the defence epitomising Croatia's spirit and refusal to yield.
They could not, though, prevent Xavi from gradually getting Spain moving. Fernando Torres saw a fierce low shot turned away by Stipe Pletikosa after 22 minutes before David Silva and Iniesta twice almost linked up to devastating effect.
But for all their possession, Bilic's plan to stifle Spain was working and as the half wore on mistakes began to creep into the defending champions' normally flawless passing game.
Croatia were quick to pounce on the scraps, testing Casillas from Daniel Pranjic's low shot. There were vehement protests when the tireless Mario Mandzukic burst clear of Ramos and Pique, only for the Real Madrid defender to clatter into the Croatia striker on the right of the penalty area.
The referee waved away Croatia's penalty claims with Corluka booked for his protests.
News of an Italian goal shortly before half-time in Posnan changed things. Not only did Croatia now know they could no longer settle for a draw but now Spain's hopes of defending their trophy would, unthinkably, end if they conceded without reply.
Croatia were noticeably more progressive, competing for the ball in midfield and committing men forward in greater numbers. Modric skipped away from Spain's midfield trio from one such attack on the hour mark, to spark a move more fluid and threatening than anything they had produced until then.
The Tottenham playmaker evaded the attention of Jordi Alba and then Ramos, before picking out the unmarked Rakitic with a delightfully weighted cross that the Sevilla midfielder headed straight at Casillas from six yards. If Spanish hearts were in mouths then, the drama was to continue.
Bilic introduced Everton striker Nikica Jelavic and Borussia Dortmund's Ivan Perisic as Croatia pushed again and the pair linked up immediately only for the final pass to run astray. Spain were on the back foot now, prompting head coach Vincente Del Bosque to introduce Jesus Navas and former Arsenal midfielder Cesc Fabregas.
It was a decision that would alter the course of the game. Fabregas might have had his third goal of the tournament shortly after coming on and although substitute Ivan Perisic thumped a fierce low volley that Casillas turned away, Spain were looking increasingly dangerous as the game became stretched.
Sergio Busquets should have put Spain after a fluid counter-attack, while Fabregas freed Iniesta down the left of the penalty area only for Pletikosa to turn the ball away.
The Barcelona midfielders were at the heart of everything now as Croatia finally began to tire.
With three minutes remaining Spain finally found space. Again Fabregas was at the heart of things, scooping a delightful through-ball to Iniesta who brought the ball down on his chest, beat the off-side trap and squared to Navas who walked the ball towards goal, thumping it into the net from a yard out with a shot that spoke of Spain's frustration.
Croatia's players collapsed on the turf. For Spain the emotion was relief.