In much of the country, the stereotype that boys do better than girls at math isn’t true – on average, they perform about the same, at least through eighth grade. But there’s a notable exception.

In school districts that are mostly rich, white and suburban, boys are much more likely to outperform girls in math, according to a new study from Stanford researchers, one of the most comprehensive looks at the gender gap in test scores at the school district level.

The test score gender gap in about 1,800 large school districts On English tests , girls test better than boys regardless of their parents’ socioeconomic status. But on math tests , boys from richer districts tend to test better than girls from richer districts. Larger circles represent districts with more students.

The research, based on 260 million standardized test scores for third through eighth graders in nearly every district in the country, suggests that local norms influence how children perform in school from early ages – and that boys are much more influenced than girls.

“It could be about some set of expectations, it could be messages kids get early on or it could be how they’re treated in school,” said Sean Reardon, professor of poverty and inequality in education at Stanford, who conducted the study with Erin Fahle, a doctoral candidate in education policy there, and colleagues. “Something operates to help boys more than girls in some places and help girls more than boys in other places.”

The study included test scores from the 2008 to 2014 school years for 10,000 of the roughly 12,000 school districts in the United States. In no district do boys, on average, do as well or better than girls in English and language arts. In the average district, girls perform about three-quarters of a grade level ahead of boys.

But in math, there is nearly no gender gap, on average. Girls perform slightly better than boys in about a quarter of districts – particularly those that are predominantly African-American and low-income. Boys do slightly better in the rest – and much better in high-income and mostly white or Asian-American districts.

In the Montgomery Township district in New Jersey, for example, the median household income is $180,000 and the students are about 60 percent white and 30 percent Asian-American. Boys and girls both perform well, but boys score almost half a grade level ahead of girls in math. Compare that with Detroit, where the median household earns $27,000 and students are about 85 percent black. It’s one of the districts in which girls outperform boys in math.

In Montgomery Township, the interest in academic achievement is high. “The students are very, very interested in their progress,” said Christopher Herte, the district’s math and science supervisor for Grades 5 to 8. “The biggest thing is family expectations and parents as role models. They don’t have to look far to see somebody who went to college or who’s doing extremely well.”

Boys are much more likely than girls to sign up for math clubs and competitions, he said, to the point that the district started a girls-only math competition this year, the Sally Ride Contest.

The gender achievement gap in math reflects a paradox of high-earning parents. They are more likely to say they hold egalitarian views about gender roles. But they are also more likely to act in traditional ways – father as breadwinner, mother as caregiver.

The gap was largest in school districts in which men earned a lot, had high levels of education, and were likely to work in business or science. Women in such districts earned significantly less. Children might absorb the message that sons should grow up to work in high-earning, math-based jobs.

High-income parents spend more time and money on their children, and invest in more stereotypical activities, researchers said, enrolling their daughters in ballet and their sons in engineering.

There is also a theory that high-earning families invest more in sons, because men in this socioeconomic group earn more than women, while low-earning families invest more in daughters, because working-class women have more job opportunities than men.

Compare a large school district with its neighbors

District Math gap (months) Reading gap (months) Includes school districts with at least 200 boys and 200 girls

In the districts in which boys do better than girls in math, they also have a smaller gender gap in language arts – so it might be that they get more encouragement to do well in school in general. Boys’ grades, behavior and future earnings seem to be more influenced by the circumstances in which they grow up, research has found.

“Both girls and boys benefit from being in more academic and more resource-rich environments,” said Thomas DiPrete, a sociologist at Columbia who has studied gender and educational performance. “It’s just that boys benefit more.”

When boys think of academic achievement as desirable and tied to their future success, they do better. Boys who have fathers who are involved in their lives, and who are highly educated with white-collar jobs, are more likely to receive this message, according to research by Mr. DiPrete and Claudia Buchmann, a sociologist at Ohio State.

At the opposite end of the spectrum, in districts that are mostly black, less affluent and in the South, girls do better than boys in math. Recent research has found that black boys in particular struggle in the face of poverty and racism. Black and Latino boys and those in poor neighborhoods often get the message that doing well in school is not manly, a variety of research has found.

“We live in a society where there’s multiple models of successful masculinity,” Mr. DiPrete said. “One depends for its position on education, and the other doesn’t. It comes from physical strength.”

Although well-off districts encourage boys in math, they don’t seem to encourage girls in the same way. Researchers say it probably has to do with deeply ingrained stereotypes that boys are better at math.

Teachers often underestimate girls’ math abilities, according to research by Sarah Lubienski of Indiana University and Joseph Cimpian of New York University, who also found the gender gap in math was largest for students from high-income families. They found that as girls move through elementary school, they lose confidence in their math skills – more than they lose interest or achievement.

“We will have boys shouting out the answer, and if they're wrong they don't care,” said Melissa Kondrick, a sixth-grade math teacher in Pleasanton, Calif., another high-performing district with a large gender math gap. “If a girl gets it wrong, they will not answer another question. You'll see them shut down.”

Girls continue to outperform boys in reading in school districts across the United States, regardless of income, and in most other rich countries. Parents have been found to talk more to girls from the time they are infants. Teachers say girls concentrate more on reading. Perhaps boys’ reading skills mature later. There could also be a role model effect: Women say they read more than men, while boys are steered more toward sports and video games.

One way to boost achievement in math, researchers say, is to avoid mention of innate skill and stress that math can be learned. Another is to expose children to adults with different areas of expertise, and offer a wide variety of activities and books. Gaps are smaller when extracurricular activities are less dominated by one gender.