I think that whether the child is inside or outside the mother's body matters for both legality and morality, but far moreso for legality.
The right not to be killed, like all rights, is a legal invention. A good one, but still a matter of legality that we cannot ascribe to all things in all situations, or we couldn't eat or defend ourselves. To ensure consistent application of such a legal principle, we must ascribe that right to all organisms that fall within a category that can be specified by clearcut objective facts, so that there can be no doubt when a person must not violate that right and no doubt when they have.
Thus, we have and should have a legal rule that the right to life (to not be killed by another) is ascribed to all organisms that meet the objective criteria of having been birthed by a human mother. We can and do specify other objective rules that grant some lesser level of rights to other organisms that don't meet this criteria, such as those that protect pre-brith fetuses and some animals from needless abuse and suffering. This rule will make infanticide a crime and abortion not a crime.
Using the moment of birth as the legal criteria makes perfect sense, both because it is an objectively verifiable moment, and because it keeps the right to life from becoming in conflict with the right to self-determine one's own body (without which the right to life has little value). Pre-birth restrictions on abortions inherently intrude upon a mother's right to self-determine their body.
But all that involves issues of pragmatism and our ability to apply the law consistently based on objective evidence and to avoid bias and abuse in the application of the law, which undermines its legitimacy and the social contract.
That doesn't really apply to whether a particular act should be judged as immoral. Plenty of acts that we should regard as immoral need to remain legal for those pragmatic reasons, and plenty of moral acts need to be treated as violations of the law.
Take a woman that decides at 27 weeks to kill the fetus that she deliberately conceived but now they just got a new job letting them travel the world and an infant would make that impossible. Compare that act to a woman who got raped but chose to have the baby, but then it suffered trauma during birth and was born with severe long long defects which basically means that it lacks 90% of the traits that phenotypically define a human being despite being genetically human, and is less "human" cognitively and behaviorally than the average dog. So, she kills the newborn.
I don't think that any remotely defensible ethical principles would deem the second woman as engaging in a moral wrong any greater than the first woman, while plenty of reasonable systems could deem the first as more immoral than the second, or at most equal in moral status.
I used extreme examples to illustrate the point that the mere pre vs. post birth difference cannot determine the morality in the way that is should determine the legality. But post birth killing can be less immoral with less extreme circumstances, such the second woman doesn't need to have been raped and could have even chosen to get pregnant, or the first woman could have a reason for her last minute abortion that is not so shallow but still far from anything neccessary to protect her from physical harm.
Note that unlike the rather dichotomous legal/illegal distinction, there is no such dichotomy in morality, but rather a wide continuum where actions stand in relative morality to each other. So, my answer to the OP is that because abortion can be a by-product of self-determination of one's body whereas infanticide cannot have that justification, infanticide will be less morally acceptable on the whole than abortion. Yet, there are plenty of possible exceptions where infanticide is no more immoral (and arguably less so) than some instances of abortion, but we should not expect the law to recognize those exceptions.