Prisoners in the UK must be given the right to vote, the European court of human rights (ECHR) has ruled, though ministers may determine which inmates should be enfranchised.

The appeals section of the Strasbourg court reaffirmed its decision that blanket disenfranchisement of all those serving time is illegal and imposed a fresh timetable for Britain's delayed compliance with similar past rulings.

The keenly anticipated ruling on Scoppola vs Italy brings some political relief in confirming there is significant leeway allowed in how the rights are granted, meaning that individual countries may choose to exclude certain groups of serious offenders, such as murderers and rapists.

The UK government has been repeatedly ordered to grant prisoners greater democratic rights. The House of Commons last year voted overwhelmingly to defy the ECHR and retain the ban on any UK prisoners being allowed to vote at elections.

However, the latest Strasbourg ruling does not avert a collision between Westminster and the European judges. The attorney general, Dominic Grieve, had intervened in the appeal over the Italian case of Scoppola because it bears so many similarities to the unresolved UK case of Hirst, which dates back to 2005.

"General, automatic and indiscriminate disenfranchisement of all serving prisoners, irrespective of the nature or gravity of their offences, is incompatible with Article 3 of Protocol No 1 [the right to free elections] of the European Convention on Human Rights," the Strasbourg court declared.

However, it accepted "the UK government's argument that each state has a wide discretion as to how it regulates the ban, both as regards the types of offence that should result in the loss of the vote and as to whether disenfranchisement should be ordered by a judge in an individual case or should result from general application of a law".

The Strasbourg court gave the UK government six months to comply with the ruling, saying "further unnecessary delay could not be contemplated, having regard to the time which had already passed since the court's ruling in Hirst".A Cabinet Office spokesman said: "This is a judgment which is for the Italian government to implement. But we will consider it carefully, and its implication on the issue of prisoner voting in the UK."