You can include terms in the shareholders' agreement requiring anyone who ceases to be a family member to offer their shares back to the company, at a pre-determined price. However, this could have consequences for the way a divorce court orders assets to be divided: if the requirement to sell the shares back reduces their value, the spouse may be entitled to a correspondingly large share of other assets.
An alternative may be to grant family members and their spouses shares that carry the same right to dividends but are non-voting. A former spouse who retains non-voting shares after a divorce is likely to welcome a fair offer for them, and in any case will at least be unable to interfere in the running of the business.
Or you might choose to put shares in an appropriate trust - for the benefit of your family - rather than transferring them directly to family members.