The Niu–Li factional strife (Chinese: 牛李黨爭; pinyin: Níu Lǐ dǎngzhēng) was an ongoing contention at the court of the mid-to late Tang dynasty. It is largely viewed to have started during the reign of Emperor Muzong, circa 821, but having its seeds in the events of his father Emperor Xianzong—between two court factions later to be referred to by Chinese historians as the Niu Faction (牛黨), named after Niu Sengru, which was largely viewed as a faction of officials from humble origins and who passed the imperial examinations to get into government; and the Li Faction (李黨), named after Li Deyu, which was largely viewed as a faction of officials from aristocratic origins. The two factions struggled for decades at court, during the reigns of Emperor Muzong and his sons Emperor Jingzong, Emperor Wenzong, and Emperor Wuzong. The struggles are viewed as having ended at the start of the reign of Emperor Wuzong's successor and Emperor Muzong's younger brother Emperor Xuānzong, in 846. His clear dislike for Li Deyu, and systematic demotion of related officials, led to the complete defeat of the Li Faction.
Traditionally, the seeds of the Niu–Li factional strife were seen to have been sown in 808, when Emperor Xianzong held a special imperial examination for the examinees to give honest criticism of government. The officials in charge of the examination, Wei Guanzhi and Yang Yuling (楊於陵), selected three examinees who gave blunt criticism—Niu Sengru, Huangfu Shi (皇甫湜), and Li Zongmin—for top marks. However, Chancellor Li Jifu was stung by the criticism they provided and viewed it as a personal attack against him. Li Jifu tearfully complained to Emperor Xianzong that the reviewers of the scores that Wei and Yang gave the imperial scholars - Pei Ji and Wang Ya — had conflicts of interest, as Huangfu was Wang's nephew. As a result of his accusations, Pei, Wang, Yang, and Wei were each demoted, with Wei initially demoted to be the prefect of Guo Prefecture (果州, in modern Nanchong, Sichuan), and then further moved to be the prefect of Ba Prefecture (巴州, in modern Bazhong, Sichuan). Niu, Huangfu, and Li Zongmin were not exiled, but were said in the Zizhi Tongjian to be effectively stalled in their careers, forcing them to find governmental positions themselves under regional governors. Despite this, Li Jifu's son Li Deyu viewed the insult on his father to be too great to bear, and thereafter an enmity developed between Li Deyu and those he viewed as having criticized his father unfairly.