When Baldwin IX, Count of Flanders and Hainault, left on the Fourth Crusade in 1202, he left his western domains under his eldest daughter Joanna. Joanna inherited the counties on Baldwin's death and, despite two marriages, died without heirs in 1244. She was succeeded by her younger sister, the aforementioned Margaret.
Margaret's first marriage, to Bouchard of Avesnes, was annulled in 1221 on orders from Joanna and the excommunication of Bouchard. By Bouchard, however, Margaret had already had three children, including John I of Avesnes. Nevertheless, in 1223, she remarried. With her second husband, William II of Dampierre (d.1231), she also had three children, including William III and Guy of Dampierre.
The contested division of the rights to Margaret's inheritance between the sons of Avesnes and those of Dampierre were the cause of the conflicts known as the "war of the succession of Flanders and Hainault."
The first conflict opened with Margaret's succession in 1244. John I of Avesnes and William of Dampierre, half brothers, fought between themselves until King Louis IX intervened in 1246. Louis gave Hainault (technically not his to give) to John I of Avesnes and Flanders (indeed his vassal) to William of Dampierre. Margaret, in light of this judgement, gave the government of Flanders over to William of Dampierre in 1247. She did not however relinquish her governance of Hainault to John I of Avesnes.
In 1251, William III of Dampierre died and Flanders passed to his brother Guy of Dampierre.
In 1248, Louis had left on the Seventh Crusade and remained abroad for six years. John I of Avesnes quickly apprehended that his mother did not intend to give him the government of Hainault as she had that of Flanders to her other sons. John I of Avesnes revolted against his mother and attacked his half-brother Guy of Dampierre, who had just become count of Flanders.
John I of Avesnes persuaded the German anti-king William of Holland to seize Hainault and Flemish territory within the empire. The fighting continued until the Battle of West-Capelle on 4 July 1253, when John I of Avesnes gained a brilliant victory over Guy of Dampierre and forced him and his mother to respect the division of Louis and grant him Hainault.