In pathology, the learned model may suffer from performance degradation due to stain variations between the training and testing datasets. To address this challenge, this paper proposes a feature disentanglement approach to learn the domain-invariant features to achieve domain generalization. The adaptive instance normalization (AdaIN)-based reconstruction is introduced to preserve the important semantic information. The generalization ability of the proposed method is further improved by using a contrastive loss function based on color augmentation to attract the domain-invariant features and repel the domain-specific features in the feature disentanglement process. The proposed method is evaluated on liver tumor and liver lipid droplet segmentation tasks. The results demonstrate that the proposed method can be applied to unseen datasets scanned by different scanners without significant performance degradation.