The Committee on Advertising Practice (CAP) said its restrictions would also apply to all other media where under-16s made up a quarter of the audience.
The rules are an attempt to help tackle obesity when children are spending more time online than ever before.
But critics say the new rules do not go far enough and may not have any impact.
They point to the thousands of children watching TV shows and videos online not specifically targeted at children, which these rules will not cover.
However, the advertisers' body said the move would lead to "a major reduction" in the number of "junk food" ads seen by children on platforms such as YouTube and children's games websites.
And it said the new rules would bring non-broadcast media, such as online, social media, cinema and billboard advertising, in line with TV rules introduced in 2007, which restricted the advertising of junk food during children's TV programmes.
The CAP said the rules were a response to research suggesting children aged five to 15 spent about 15 hours online every week - overtaking time spent watching TV.
Last month, the World Health Organization warned that governments should be protecting children from targeted junk food adverts in apps, social media and video blogs.
While the CAP acknowledged the impact of the rule changes could be small, it said they demonstrated the industry was putting "the protection of children at the heart of its work".
Recent figures showed a third of children in the UK were overweight or obese by the time they left primary school.
Prof Neena Modi, president of the Royal College of Paediatrics and Child Health, said the measures would help in the fight against the damaging effects of junk food and fizzy drinks - but more could be done.
"Surely it is time for government to strengthen rules around all advertising, and in particular ban the advertising of foods high in salt, sugar and fat on television before the 21:00 watershed."
Dr Alison Tedstone, chief nutritionist at Public Health England, said the advertising restrictions were encouraging but the real test would be whether they made any difference to the exposure of high sugar, salt and fat products to children and young people.
The government's childhood obesity strategy was heavily criticised in the summer for not including measures banning advertising of junk food to children, and campaign groups still want the government to take a stronger stance.
Action on Sugar said: "This is industry regulating itself, but we need to know if advertisers are complying with the rules.
"There is a need for an independent monitor."
Malcolm Clark, co-ordinator of the Children's Food Campaign, said there were still too many loopholes.
He said: "Just as many of the TV programmes most watched by children aren't covered by the rules, so it looks like many of the most popular social media sites won't be either; neither will billboards near schools, or product packaging itself."
And he said it was not clear what ads would be banned under the new rules, if children had to make up 25% of the audience.
He added: "Ultimately, the new rules are only as good as the body which enforces them."
The Advertising Standards Authority, which regulates all media in the UK, has said it will administer the new rules.
The rules will come into effect on 1 July 2017.