Barbara Babcock, a legal trailblazer and champion for gender equality who became the first director of the D.C. Public Defender Service and first female faculty member at Stanford Law School, died April 18 at her home in Stanford, Calif. She was 81.

Her death was announced by Stanford University, which said she had cancer.

When Ms. Babcock studied at Yale Law School in the early 1960s, women made up less than 5 percent of the country’s law students and “sex discrimination” scarcely existed as a concept. Male classmates asked why she bothered to pursue a career in the law, let alone in criminal defense, and few judges seemed interested in offering clerkships to women.

Yet over the next decade, Ms. Babcock helped usher in a new era for women in the legal profession. As one of only a few female criminal lawyers in Washington, she built what was arguably the country’s preeminent public-defender service, offering indigent defendants the same level of service they could expect from white-shoe firms. At Stanford, where she began teaching in 1972, she brought a feminist approach and a focus on civil rights.

“In my opinion — shared by many — she was the best legal educator of her generation and beyond,” said Toni M. Massaro, a former dean of the University of Arizona’s law school who co-authored a civil procedure textbook with Ms. Babcock. In an email, she added that Ms. Babcock was “charismatic, inspirational, unforgettable, unique and above all committed to students’ personal and professional well-being.”

Ms. Babcock mentored “countless students at Stanford who were often living in the shadows” as a result of their gender, sexuality or race, said Mark G. Kelman, a professor and vice dean at the university’s law school. “Her obvious charisma, coupled with a one-on-one authenticity that few charismatics possess, made her beloved by hundreds of students in a way no other professor I’ve known is beloved, even the most gifted ones.”

An expert on criminal and civil procedure, Ms. Babcock taught some of the country’s first “women and the law” courses and co-wrote a landmark 1975 textbook, “Sex Discrimination and the Law.” She also helped found Equal Rights Advocates, a San Francisco-based legal organization dedicated to ending sex discrimination, and went on leave during the administration of President Jimmy Carter to join the Justice Department as assistant attorney general in charge of the civil division.

“I was often asked what it ‘felt like to get my job because I was a woman,’ ” Ms. Babcock later wrote in a blog post. “I developed a stock answer: ‘It’s far better than not getting it because I’m a woman.’ ”

Ms. Babcock made her name with a job that she initially viewed as a sacrifice and duty, applying to become the director of Washington’s Legal Aid Agency because, in her telling, the organization was having trouble finding anyone else to take the position. “Back then the director’s salary was set at $16,000,” she told a Stanford interviewer in 2016. “You couldn’t raise a family on it.”

She had previously worked at Edward Bennett Williams’s powerhouse law firm, Williams & Connolly but grew tired of defending the wealthy and joined the Legal Aid Agency in 1966, only to find that the organization was little more than “a guilty-plea mill.”

Within two years, she was promoted to director, and began persuading Congress to grow the organization into the Public Defender Service. By 1972, according to a Washington Post report, the agency represented roughly a quarter of all criminal defendants in Washington and boasted a staff of 40 lawyers, including Ivy League graduates and former Supreme Court clerks.

Her legal team found itself faced with an unexpected challenge in the spring of 1971, when thousands of demonstrators took to the streets in Washington to protest the Vietnam War. Going beyond her original mandate, Ms. Babcock oversaw legal efforts to represent the protesters, who were swept up in “the largest bust in U.S. history,” said former Post journalist Lawrence Roberts, author of the forthcoming book “Mayday 1971.”

Some 12,000 demonstrators were rounded up in a police dragnet, according to Roberts, with many held in a makeshift detention camp near RFK Stadium. “Barbara filed a mass habeas corpus case,” he wrote in an email, “and led her PDS lawyers into a round the clock, thrilling legal battle that resulted in the release of the prisoners. (‘It was like being in a war,’ she told me.)”

Almost all the arrests were later found to be unconstitutional, and some rules on speech and assembly were changed for future demonstrations. In an email, Michael S. Wald, a longtime Stanford Law colleague of Ms. Babcock’s, credited her and the PDS with helping “establish the contours of law in situations of mass arrests during mass demonstrations.”

Barbara Allen Babcock was born in Washington on July 6, 1938. She was raised in part by her grandparents in Hope, Ark., her mother’s hometown, while her father served in the Navy during World War II and her mother worked a government clerical job in Washington.

The family eventually settled in Hyattsville, Md., where her father “made being a lawyer seem both heroic and fun,” tackling murder cases as well as mundane incorporation issues.

In a 2016 memoir, “Fish Raincoats,” Ms. Babcock recalled that he was also an alcoholic whose drinking cast a shadow over her adolescence. “I thought of his periods of sobriety as our real life,” she wrote, “and the rest as a bad dream that might not recur if we were lucky and good.”

Ms. Babcock received a bachelor’s degree in 1960 from the University of Pennsylvania. After graduating from Yale in 1963, she clerked at the U.S. Court of Appeals for the District of Columbia Circuit for Judge Henry W. Edgerton, one of the few federal judges of his era to hire female and African American clerks.

As assistant attorney general, Ms. Babcock supervised 700 lawyers, presiding over “the world’s largest law firm,” as she called it. She also encouraged President Carter to appoint minorities and women — including future Supreme Court Justice Ruth Bader Ginsburg — to the federal bench.

“Barbara ran that giant law office with a secure hand and spent countless hours helping literally to change the complexion of the U.S. Judiciary,” Ginsburg wrote in 1997, according to a profile in Stanford, a university magazine. Regarding her own appointment to the U.S. Court of Appeals for D.C., Ginsburg added: “I do not believe I would have gained that good job without her constant endeavors to place and move up my name on the candidates list.”

At Stanford, Ms. Babcock aimed to promote the overlooked histories of women in the legal profession, launching an online biography project to spotlight female lawyers. She became a professor emerita in 2004 (by then, women made up half of the school’s student body) and later wrote “Woman Lawyer” (2011), a well-reviewed biography of Clara Foltz, considered the first female lawyer on the West Coast and a pioneer of the public-defender role.

Her first marriage, to law professor Addison M. Bowman, ended in divorce. (She adopted his last name for several years while running the Public Defender Service.) In 1979, she married Stanford Law colleague Thomas C. Grey. He survives her, in addition to a stepdaughter, Rebecca Grey; two brothers; and a granddaughter.

Ms. Babcock was a colorful raconteur, frequently weaving stories from her life and career into classroom lectures. One of her favorites involved a Washington client known as Geraldine, who faced a 20-year sentence for heroin possession and had already spent 15 years behind bars for drug use.

Grasping at legal straws, Ms. Babcock sent her to a psychiatric hospital, where she was diagnosed with “inadequate personality” — forming the basis of a long-shot insanity defense, which Ms. Babcock augmented in the courtroom with discussions of her client’s addiction and impoverished background.

When the jury found her “not guilty by reason of insanity,” Geraldine “burst into tears,” Ms. Babcock wrote in her memoir. “Throwing her arms around me, she said, ‘I’m so happy for you.’ ”

“Geraldine was right when, at the moment of the verdict, she saw that my life, too, had been saved,” Ms. Babcock added. “Her case had become my case. And it had given me what I treasured most: the unalloyed pleasure of ‘not guilty.’ In life I have found few joys so pure.”