Spain's cabinet has approved measures for Britons in Spain to continue living there as now if the UK leaves the EU without a deal.
Foreign Minister Josep Borrell said the main purpose was that no-one, British or Spanish, would be left unprotected.
Spain estimates that the measures, which would become law under a no-deal Brexit, would grant residency rights to about 400,000 UK citizens.
UK MPs have so far rejected PM Theresa May's withdrawal deal with the EU.
That raises the prospect of the UK leaving without a deal on 29 March.
More than 300,000 Britons are currently registered as residing in Spain, the government in Madrid says. There are at least 150,000 Spaniards currently residing in the UK.
Mr Borrell said the measures "of temporary nature" were aimed at protecting the interests of Spanish and British citizens as well as trade between the two countries.
Earlier on Friday, the EU rejected calls for an agreement to protect citizens from the UK and the rest of the bloc in the event of a no-deal Brexit. It said it would "not negotiate mini deals" as that would imply negotiations had failed.
About 1.3 million UK-born people are resident in the other 27 member states of the EU, while the UK hosts about 3.2 million EU27 nationals.
The withdrawal deal would enable UK citizens to keep their current freedom of movement and other EU citizenship rights until 31 December 2020, when the Brexit transition period is set to end.
There is huge uncertainty about what a no-deal Brexit would mean for Britons in the EU. The priority for most will be to register as residents, but the rules vary from country to country.
What is Spain's contingency plan?
The plan envisages that Britons living in Spain would have to apply for the "foreigner identity card" before 31 December 2020 to prove their legal residency status.
Spain's El Pais newspaper reports that the process would be "nearly automatic" for those UK nationals who already have permanent residency.
The plan of Spain's Socialist Prime Minister Pedro Sánchez would be dependent on the UK reciprocating with similar measures for Spaniards residing in the UK.
The decree has measures covering health care, social security, education and many other fields, ABC website reports.
Mr Sánchez has said he wants to secure all rights for British citizens in Spain regardless of what happens.
London and Madrid have already signed a deal ensuring voting and working rights for respective migrants - but healthcare was not mentioned in that agreement.
The contingency plan will also cover Gibraltar, although certain additional provisions may apply, including Spain's power of veto over issues relating to the British Overseas Territory in any future agreement between the UK and the EU.
Some 9,000 Spanish citizens work in Gibraltar, and the government in Madrid says the measures would be contingent on them receiving the same rights as British citizens, El País says.
Last year about 18 million Britons visited Spain, and the government in Madrid hopes the contingency plan will limit damage that a no-deal Brexit might do to the tourist industry - a key sector of Spain's economy.