The law in Spain states that, following the end of a marriage, custody of the children may be awarded to either parent.
Although there have been recent developments that suggest the judiciary is beginning to apply more weight to the role of the father in the upbringing of the children, in approximately 95% of cases, custody is awarded to the mother of the children.
In Spain, in order for the custody of younger children to be taken from the mother, it would be necessary to demonstrate some type of incapacity on her part to look after them.
However, once the children are 13 years of age (or less if they demonstrate sufficient maturity) they will be heard by the court before any judgment is made that will personally affect them.
It may be the case of course that the spouses come to an agreement between themselves that sets out the custody of the children including the visitation rights of the spouse who does not take custody. This would have to appear in the divorce agreement.
Assuming that the agreement does not put any child at risk in any way the judge will ratify such an agreement.