A man who posted a Facebook message following the deaths of six British soldiers which said: "All soldiers should die and go to hell" has been sentenced to a community order and told to pay costs of £300 by magistrates.

Azhar Ahmed, 20, from Dewsbury, West Yorkshire, admitted posting the message two days after the deaths of the soldiers in March this year but told a trial at Huddersfield magistrates court last month that he did not think it was offensive. The remarks were derogatory, disrespectful and inflammatory, the court ruled in September as a district judge found him guilty of a grossly offensive communication.

He will complete 240 hours of community service over a two-year period, the court ruled.

Sentencing him on Tuesday, the district judge Jane Goodwin told him he was "particularly foolish" and the damage caused by his comments, which could be seen by at least 600 Facebook users, had been substantial.

"You posted the message in response to tributes and messages of sympathy," she said. "You knew at the time that this was an emotive and sensitive issue." She said with freedom of speech comes responsibility and on 8 March "you failed to live up to that responsibility".

The comments were made two days after Sergeant Nigel Coupe, 33, of the 1st Battalion the Duke of Lancaster's Regiment was killed by a massive improvised explosive device on 6 March in the deadliest attack on British forces in Afghanistan for more than a decade alongside Corporal Jake Hartley, 20, Private Anthony Frampton, 20, Private Christopher Kershaw, 19, Private Daniel Wade, 20 and Private Daniel Wilford, 21, all of the 3rd Battalion the Yorkshire Regiment.

Ahmed made his comments on the social networking site two days later, writing: "People gassin about the deaths of Soldiers! What about the familys who've been brutally killed. The women who have been raped. The children who have been sliced up!" He went on to be abusive about soldiers.

He told the court he had deleted the message as soon as he realised what reaction it was getting. He said he had replied with apologies to many people who had commented on his page, and when some told them they had lost relatives in Afghanistan, he realised how serious it had been. But he denied that the message was grossly offensive and said he thought it would just have been upsetting and caused distress.

He said he was only trying to make his point that many other deaths in Afghanistan were being ignored.

Nicholas Barker, defending Ahmed, said it was "a serious matter" but not one that warranted custody. He said Ahmed had initially been voicing legitimate concerns about the victims of war, but went on to overstep the mark. He added that when he realised the comments were causing distress, he removed them.

The sentence was greeted with cries of "disgusting" from protesters in the public gallery, some of whom walked out while the judge was speaking. One man was detained by police while leaving the court after shouting out comments.

On the steps of the court, a dozen police officers watched a crowd of demonstrators, many of whom appeared to be linked to far-right organisations. An hour later, Ahmed left the building via a side door, flanked by police officers and ran into a waiting car.

At the trial in September, Goodwin said the law was not there to stop legitimate political opinion being strongly voiced. But she said the test was whether what had been written was "beyond the pale of what is tolerable in our society". She said Ahmed's post cleared this hurdle and she was satisfied that the message was grossly offensive.