BACKGROUND: Mental health in the elderly has multiple determinants, and studies indicate household and family composition, economic status, and family support are key factors. However, these are difficult to modify, and better lifestyle for the elderly can be a possible intervention. The current study examined the mediating role of lifestyle in the association between these three types of the household and family composition (living alone, living with a spouse, and living with children) and mental health in older adults. METHODS: We studied 5,407 participants (58.7% female, age 45 + years) from the Beijing Aging Brain Rejuvenation Initiative Project. All participants underwent a battery of examinations to measure degree loneliness, depression, and global cognitive function. We also surveyed personal lifestyles. We used a mediation analysis to determine the relative contribution of each lifestyle factor on mental health outcomes. RESULTS: Older adults living alone rarely participated in mental and social activities and often had irregular diets; those adults living with children spent most of their time caring for grandchildren and had irregular eating and sleeping schedules; those living with a spouse often engaged in a variety of leisure activities and had the best life habits. Mediation analyses showed that dietary and sleeping irregularity partially mediated the negative effects of living alone on mental health, and were moderated by age and gender. CONCLUSIONS: Living with a spouse was associated with benefits for the mental health of middle-aged and older adults (especially older and female individuals), through modifying better lifestyles than those of individuals with the other two types of the household and family composition.