Government is focused on controlling the people to follow their rules, morality is a sociological beast. Laws are constantly being changed to suit certain people and constrain other people. Ignorance of the law prompted the addition of an Ethics exam for the BAR, because Nixon's team of lawyers said that they didn't think what they were doing was wrong.
Sodomy until recent times was considered illegal and immoral, but today it is neither. Laws and morality vary across the local, and state governments, but the trend today is to have the federal government become the law.
Ignorance of the law is not a defense, because everyone would claim that they didn't know it was illegal. However, it should be a mitigating factor in determining guilt or innocence in a mens rea crime, that is one where one of the elements of the crime requires a criminal intent.
Most felonies are mens rea crimes requiring an intent to commit the crime. Some felonies transfer one intent to act as the required intent mentioned in the law. Drunk driving resulting a death is one of those crimes. It totally removes intent, as you have the intent even if you didn't intend to get drunk. At one time, being drunk was a mitigating defense to an intentional crime.
The point is that morality is subjective, and depending on the lawmakers, morality is a fuzzy element of their laws.
Morality is not an objective or even absolute concept. Take pornography, it can be illegal, and immoral, but it is not absolute, and it cannot be assessed objectively. A supreme court justice once said about pornography, I can't define it, but I know it when I see it. Notice the use of the pronoun, I.
I is most subjective, not objective.
The point is that the Law cannot be equal to Morality for a multitude of reasons.
If I have not answered your ? please delete my comment.