Yes, you can compost onions – but with a few considerations.
The top & tails you cut off when prepare onions to eat will rot down quickly – no problem.
The outer layers of onion skin can be pretty tough and needs to be good and wet before it’ll start to break down – stir it into the heap so they’re covered with lots of moist materials. (They can also be used as a natural dye first – white/yellow onions produce lovely golden shades, red onions produce warm browns.)
Like potatoes though, whole onions have a tendency to regrow – they’ll probably sprout new shoots and try to grow new onions before they rot down. If you want to avoid that, chop up the onion into halves or quarters before you put it in the compost bin.
Cooked onions can be composted – they’ll rot down quickly – but if they have been soaked up fats or juices (especially meat juices) during the cooking process, it’s best to leave them out — the fats or juices might attract rats or other unwanted visitors to your compost heap.