Early in the 10th century, as China became politically fragmented, successive lords from the Khúc family, followed by Dương Đình Nghệ, ruled Giao Châu autonomously under the Tang title of Tiết Độ Sứ, Virtuous Lord, but stopping short of proclaiming themselves kings.
In 938, Southern Han sent troops to conquer autonomous Giao Châu. Ngô Quyền, Dương Đình Nghệ's son-in-law, defeated the Southern Han fleet at the Battle of Bạch Đằng River (938). He then proclaimed himself King Ngô and effectively began the age of independence for Vietnam.
Ngô Quyền's untimely death after a short reign resulted in a power struggle for the throne, the country's first major civil war, The upheavals of Twelve warlords (Loạn Thập Nhị Sứ Quân). The war lasted from 945 AD to 967 AD when the clan led by Đinh Bộ Lĩnh defeated the other warlords, unifying the country. Dinh founded the Đinh Dynasty and proclaimed himself First Emperor (Tiên Hoàng) of Đại Cồ Việt (literally "Great Viet Land"), with its capital in Hoa Lư (modern day Ninh Bình). However, the Chinese Song Dynasty only officially recognized him as Prince of Jiaozhi (Giao Chỉ Quận Vương). Emperor Đinh introduced strict penal codes to prevent chaos from happening again. He tried to form alliances by granting the title of Queen to five women from the five most influential families.
In 979 AD, Emperor Đinh Bộ Lĩnh and his crown prince Đinh Liễn were assassinated, leaving his lone surviving son, the 6-year-old Đinh Toàn, to assume the throne. Taking advantage of the situation, the Chinese Song Dynasty invaded Đại Cồ Việt. Facing such a grave threat to national independence, the court's Commander of the Ten Armies (Thập Đạo Tướng Quân) Lê Hoàn took the throne, founding the Former Lê Dynasty. A capable military tactician, Lê Hoan realized the risks of engaging the mighty Chinese troops head on; thus he tricked the invading army into Chi Lăng Pass, then ambushed and killed their commander, quickly ending the threat to his young nation in 981 AD. The Song Dynasty withdrew their troops yet would not recognize Lê Hoàn as Prince of Jiaozhi until 12 years later; nevertheless, he is referred to in his realm as Đại Hành Emperor (Đại Hành Hoàng Đế). Emperor Lê Hoàn was also the first Vietnamese monarch who began the southward expansion process against the kingdom of Champa.
Emperor Lê Hoàn's death in 1005 AD resulted in infighting for the throne amongst his sons. The eventual winner, Lê Long Đĩnh, became the most notorious tyrant in Vietnamese history. He devised sadistic punishments of prisoners for his own entertainment and indulged in deviant sexual activities. Toward the end of his short life – he died at 24 – Lê Long Đĩnh became so ill that he had to lie down when meeting with his officials in court.