Childhood is considered an important period for the development of healthy eating behaviours. This study aimed to evaluate the influence of early life factors and sociodemographic characteristics, including early diet quality, on diet quality at 7 years. The sample includes 5013 children evaluated at the ages of 4 and 7 years from the Portuguese birth cohort Generation XXI with complete information on FFQ. A healthy eating index was developed at both ages to assess adherence to the WHO’s dietary recommendations, including eight food groups. Consumption quartiles were obtained for each group at 4 years and assigned a score between 1 and 4. A higher score represents a higher adherence to a better diet (range: 8 to 32). The associations between early life factors and sociodemographic characteristics and the score of the healthy eating index at 7 years were evaluated through linear regression models. The healthy eating index had an average score of 21⋅4 ± 3⋅53 (range: 12 to 32) at 4 years and 20⋅3 ± 3⋅36 (range: 11 to 31) at 7 years. After adjustment for confounders, a positive association was found between the healthy eating index at 4 and 7 years (β = 0⋅384, 95 % CI 0⋅356, 0⋅441). Maternal years of education (β = 0⋅094, 95 % CI 0⋅071, 0⋅116) and dietary score (β = 0⋅182, 95 % CI 0⋅155, 0⋅209) were positively associated with increasing dietary quality from 4 to 7 years. A healthier diet at preschool age, higher maternal education and a healthier diet increase the likelihood of maintaining a high healthy eating index score at school age.