The best thing to do is to create a logical background for the bad things they do or it could be to make people feel empathy by telling about the difficult times in the past.
For example, although Thanos was a villain in the MCU, many were able to empathize with him because we knew where his idea came from. Or again, almost everyone loves Loki from the MCU. Because we learned that his past experiences and injustices made him this way.