A political veteran, Vajpayee spent his first 20 years in Parliament leading the opposition Jana Sangh and building a reputation as a forceful and articulate critic of the ruling Congress Party. In 1975, Vajpayee was among a group of dissident politicians jailed under the state of emergency declared by Prime Minister Indira Gandhi. In 1977, the Jana Sangh merged with other anti-Congress forces to form the Janata Party. Elections held that year swept the Janata Party into power, and Vajpayee became the new government's minister for external affairs. However, divisions within the Janata Party caused this government's collapse in 1980, and the Congress Party was returned to power. Rather than resurrect the now disbanded Jana Sangh, Vajpayee helped found a new party (the BJP), aimed at continuing the nationalist legacy of its predecessor.
As the BJP's first president (1980–86), Vajpayee downplayed the party's ideological ties to the more extremist Hindu nationalism of the RSS. His efforts failed to resonate with voters. The BJP fared poorly in the 1984 elections, and Vajpayee lost his seat in the People's Assembly. In 1986, Vajpayee won a seat in the Council of States and became the leader of his party's small parliamentary delegation. But hardliners had taken over the BJP and embraced a highly controversial agenda emphasizing the paramount importance of Hindu culture, values, and traditions in India. Critics accused the BJP of fomenting communal and ethnic strife and abandoning the Constitution's principle of secularism. Nonetheless, this strategy successfully galvanized support among upper-caste Hindus in the north and, in the 1991 elections, the BJP emerged as the major opposition in Parliament. Vajpayee was among the 120 BJP members elected to the People's Assembly that year. In 1992, however,
the BJP was put on the defensive when militants affiliated with the party demolished a mosque in Ayodhya, sparking violent Hindu-Muslim riots across India.
In the run-up to the 1996 elections, Vajpayee ran as the BJP's prime ministerial candidate. He denounced the Ayodhya incident, and many observers interpreted his selection as the BJP's attempt to present a less confrontational face to voters, in hopes of broadening the party's appeal. The elections unseated the ruling Congress Party, and Vajpayee became prime minister of a short-lived (13-day) BJP government. The "secular" Congress-UF axis, which ousted Vajpayee's government, did not endure, and the ensuing parliamentary impasse forced new elections. Seizing its opportunity, the BJP formed electoral alliances with a heterogeneous group of smaller parties to increase its chances of a parliamentary majority and promised voters a stable government. Buoyed by the UF's weak showing at the polls, the BJP and its allies came within striking distance of a majority in the new People's Assembly. After intense maneuvers, the BJP gained the additional support needed to form a government.
On 19 March 1998, Vajpayee was sworn in as the nation's third prime minister in a year. Ten days later his government won Parliament's vote of confidence, although only by a margin of 13 votes. With such a slim majority, Vajpayee's government was always subject to pressure from his coalition allies. It required all of the prime minister's considerable political skills to hold the coalition together. In April 1999, however, the All India Anna Dravida Munnetra Kazhagam party (AIADMK) withdrew its support from the government. On 17 April, Vajpayee lost a parliamentary vote of confidence by a single vote, and he resigned as prime minister. As Congress was unable to form a coalition government, Parliament was dissolved, and new elections were scheduled for the fall of 1999. During the summer, Sonja Gandhi, widow of former prime minister Rajiv Gandhi, entered the political arena, actively campaigning for Congress. Her involvement, however, did little to boost Congress in the polls. Vajpayee emerged from the September–October elections in a strong position, with the BJP and its allies controlling 298 seats in the People's Assembly. Vajpayee was sworn in as prime minister on 14 October 1999 as head of the NDA government. He became the first Indian leader in several years to have a working majority in Parliament.