In the Middle Ages, Austria was a semi-independent state within the Holy Roman Empire ruled by the Habsburgs. Hungary was a sovereign state outside the empire. In 1526 Hungary was defeated and partially conquered by the Ottoman Empire, but the crown of Hungary was inherited by the Habsburgs, with the Ottomans subsequently driven out in 1699. From 1526 to 1806, Austria and Hungary were a personal union under the Habsburgs.