A student has been jailed for 56 days for posting offensive comments on Twitter about the on-pitch collapse of Bolton Wanderers footballer Fabrice Muamba.

Liam Stacey was arrested after his tweets were reported to police by Twitter users from across Britain, including the former England striker Stan Collymore. The 21-year-old pleaded guilty to the Racially Aggravated s4A Public order Act 1986. He posted his offensive comments shortly after Muamba suffered a cardiac arrest during his team's FA Cup quarter-final tie against Tottenham Hotspur on 17 March.

Stacey sobbed throughout the hearing and held his head in his hands when he was sentenced. He was led away in handcuffs.

District judge John Charles told Stacey: "It was racist abuse via a social networking site instigated as a result of a vile and abhorrent comment about a young footballer who was fighting for his life. At that moment, not just the footballer's family, not just the footballing world but the whole world were literally praying for his life. Your comments aggravated this situation.

"I have no choice but to impose an immediate custodial sentence to reflect the public outrage at what you have done. "You committed this offence while you were drunk and it is clear you immediately regretted it. But you must learn how to handle your alcohol better."

Other Twitter users criticised Stacey's original post, prompting him to retaliate by posting a series of offensive and racist insults, some of a sexual nature, aimed at his attackers.

In mitigation, Gareth Jones, defending, said: "The first comment [he] wrote was in relation to Fabrice Muamba. He does not bear him any grudge and he is glad he is making a good recovery. My client simply lost his head and posted these disgusting comments to the bitter shame of himself and his parents."

Jones said Stacey feared his university career was over: "He is no longer welcome at the institution because he has damaged their reputation and there are fears of reprisals."

Jones depicted Stacey, originally from Pontypridd, as a "kind and caring person" with friends of different ethnic backgrounds. He said Stacey was due to take final exams in his biology degree and had ambitions to become a forensic scientist, a career which was now almost certainly lost to him.

Jim Brisbane

, chief crown prosecutor for CPS Cymru-Wales, said: "Racist language is inappropriate in any setting and through any media. We hope this case will serve as a warning to anyone who may think that comments made online are somehow beyond the law."

Swansea University, where Stacey is a biology undergraduate, said after the hearing that he had been suspended and a disciplinary hearing would be held.

Lisa Jones, prosecuting, told Swansea magistrates at an earlier hearing: "Fabrice Muamba collapsed on the pitch and was believed to have died. Shortly after, Stacey posted on Twitter: 'LOL, Fuck Muamba. He's dead.'"

After other Twitter users criticised Stacey, prompting him to post further offensive and racist comments, users reported him to police forces around Britain.

Stacey branded people who criticised him on Twitter as "wogs" and told one to "go pick some cotton".

The court heard Stacey tried to claim his account had been hacked and tried to delete his page.

He texted a friend to say: "I said something about Muamba that I shouldn't have and tweeted back to some people who abused me. Getting police on me now, which isn't good at all."

Police arrested him in Swansea the day after Muamba's collapse. When interviewed, Stacey said he was drunk at the time, having been drinking since early Saturday afternoon while watching the Wales v France Six Nations rugby match.

He told police: "I was at the bar when I heard what had happened to Muamba. I don't know why I posted it. I'm not racist and some of my friends are from different cultural backgrounds."

The swiftness of the arrest demonstrates how seriously police are taking the posting of potentially criminal comments on social networking sites by so-called trolls.

After his initial court appearance, where he was warned he could be jailed, Stacey was released on bail on condition that he stayed off Twitter and other social networking sites.

A statement released on Monday by Barts and the London NHS Trust, where the footballer is being treated, said: "Fabrice Muamba remains in intensive care at the London chest hospital where his condition is serious but stable. He continues to make encouraging progress in his recovery.

"Over the weekend, he has been able to sit out of bed for a short time, watch television and has begun to eat. However, he will need to continue to be closely monitored by the medical team for some time."

• This article was amended on 28 March 2012. The original said Liam Stacey admitted incitement to racial hatred. This has been corrected.