Buying for your own child is a matter of knowing them. Buying for somebody else's is a matter of not knowing them, and of a parent you will see again.
Here is the short version.
The two rules that solve most of it
Do not buy noise. Whatever you buy will live in somebody else's house, and the parent will remember who brought the thing with the speaker in it. This is the single most common mistake and it is entirely avoidable.
Do not buy the biggest thing. A present that dominates the pile embarrasses the other guests and puts the parents in an awkward position. Being the most generous person in the room is not the goal.
What is nearly always safe
A book. Goes on a shelf, needs no storage, and no parent has ever resented one. Write the child's name inside, which converts an ordinary present into a kept one.
Art materials. Consumable, so they do not accumulate, and every house needs replacing.
A card game. Small, quiet, needs a second person. Also a very safe present for a child you do not know, because the appeal is the playing rather than the theme.
Something outdoors. A ball, a kite, chalk. Nobody has ever run out of space for a ball.
What to avoid
Anything with a character on it, unless you have been told which one. Get it wrong and it is worse than a plain thing.
Anything requiring assembly, unless you enjoy being thought of at eleven at night in the way people think about flat-pack furniture.
Anything that needs an account, a subscription, or a parent's phone number.
Sweets, unless you have asked. It is the parent's decision and not yours.
When you know nothing at all
Ask the age and buy for six months younger. A present that is slightly too easy is enjoyed. A present that is too hard tells a child something unpleasant about themselves, and they remember it.
The one that works when you have no idea
A good hardback book plus a pack of playing cards. Under twenty dollars in most places, works from five upwards, needs no batteries and no storage, and it looks considered rather than expensive.
I would take that to a party over anything else on this page.
And mine, honestly placed
The set is $49.95 during the launch, which makes it a main present. For a child who is not yours, that is usually too much: it puts the parents in an awkward position and it dominates the pile.
Where it does fit is a godchild, a niece or nephew, or a child you buy for properly once a year. Then it is exactly right, because it lasts a school year and works at two ages at once, which is what an aunt wants a present to do.
For an ordinary party, take the book and the cards, and if you want to add something personal for nothing, print the free colouring poster for the letter of the child's name and put it in the card. All twenty-six are here.