That’s a great question that requires some decent Java expertise
Its per-case, per-mod, per-version
If you’re lucky someone else has done the work for you and ported it to both versions. But in general if a mod is made exclusively for Fabric or Forge, it’s gonna stay that way
Check the wikis first - they both are extremely comprehensive and should cover most questions!
Even though I prefer Fabric I recommend making a mod first and getting started with Forge. Trust me when I say converting a mod after you’ve made one makes so much more sense
https://wiki.fabricmc.net/
https://docs.minecraftforge.net/en/1.21.x/gettingstarted