Vitamin D deficiency (VDD) during early life emerges as a potential risk factor for autism spectrum disorder (ASD). Individuals with autism commonly exhibit lower vitamin D (VD) levels compared to the general population, and VD deficiency is prevalent during pregnancy and lactation. Moreover, gastrointestinal comorbidity, prevalent in ASD patients, correlates closely with disruptions in the gut microbiota and altered intestinal permeability. Therefore, it is fascinating and significant to explore the effects of maternal VD deficiency during pregnancy and lactation on the maturation of the gut microbiota of the offspring and its relevance to autism spectrum disorders. In this study, we established maternal pregnancy and lactation VD-deficient mouse models, employed shotgun macrogenomic sequencing to unveil alterations in the gut microbiome of offspring mice, and observed autism-related behaviours. Furthermore, fecal microbial transplantation (FMT) reversed repetitive and anxious behaviours and alleviated social deficits in offspring mice by modulating the gut microbiota and increasing short-chain fatty acid levels in the cecum, along with influencing the concentrations of claudin-1 and occludin in the colon. Our findings confirm that VDD during pregnancy and lactation is a risk factor for autism in the offspring, with disturbances in the structure and function of the offspring's gut microbiota contributing at least part of the effect. The study emphasises the importance of nutrition and gut health early in life. Simultaneously, this study further demonstrates the effect of VDD on ASD and provides potential ideas for early prevention and intervention of ASD.