First, always keep in mind the moral and ethical boundaries. Know that your work may be read by different people. Second, focus on positive emotions and relationships, like love, respect, and friendship. Third, if you have any doubts about an idea, don't use it.
Well, one way is to have a clear set of values before you start writing. Think about what kind of message you want to convey through your fanfic. If it's something that might make people uncomfortable or is just plain wrong, then stay away from it. Also, look at other successful and positive fanfics for inspiration. They usually focus on things like character growth, solving problems in a positive way, and building good relationships. This can help you stay on the right track and avoid writing such inappropriate content.