When Cardi B arrived at Queens Supreme Court last month to face two felony assault charges, the artist was decked out in designer duds: a two-toned designer suit from Barneys, Stuart Weitzman stilettos and a rare pink Hermès Birkin bag.

The 26-year-old “Bodak Yellow” rapper — who pleaded not guilty to charges of hurling a hookah and Champagne bottles at two bartenders in a Flushing strip club last August — waved at photographers and adjusted her cleavage as she strutted into the courtroom.

“Cardi treats her trips to the courthouse like a runway show,” says Joseph Tacopina, the celebrity criminal defense attorney representing the alleged victims, sisters Jade and Baddie Gi, whose real names are Sarah and Rachel Wattley.

But the Grammy Award-winning musician is hardly alone.

Scores of high-profile defendants, from Manhattan socialite Anna Sorokin (who scammed her way to a life of luxury), to “Full House” star Lori Loughlin (who allegedly paid bribes to have her kids admitted to elite universities), to “Mean Girls” heroine Lindsay Lohan (who was busted for drunk driving), are treating somber legal proceedings more like glamorous red-carpet photo ops, dressing — and preening — for the eyes of a worldwide audience.

Experts say that social media is to thank — or blame — for the trend.

“We’re in a world now where there’s 24/7 access to everything,” says Todd Spodek, Sorokin’s attorney. “People have always been concerned about court looks, but now it’s not just about the people in the room. The whole world is watching.”

On Oct. 1, Cardi showed up to the 109th precinct in a blond, Marilyn Monroe-esque wig, a slitted khaki pencil skirt and sheer white blouse. That’s when the rapper was formally charged for allegedly starting the brawl over claims that the sisters had sex with Cardi’s husband, Offset, and siccing members of her crew on the women.

The look might have been over-the-top, but it was thought out down to her fingertips, says the rapper’s manicurist, Jenny Bui.

“She just wanted black gel with diamonds around the perimeters,” Bui tells The Post, calling the choice “more simple than usual.” She also filed the star’s talons down to a tasteful 1-inch length.

Cardi’s other courthouse style moments have been equally striking: The navy and lavender suit she wore on June 25 — her fifth fashionable court appearance since she was charged — was made by upscale Danish designer, Brøgger. It was then specially tailored with the rapper’s risqué style in mind.

“Even when it comes to something in such a traditionally conservative setting, she still is who she is,” the rapper’s tailor and owner of Olá Alterations, Ongell Fereria, tells The Post, adding that she made the jacket “more form-flattering.”

But when it came to the pants, they kept it old-fashioned.

“Usually she does a more fitted pant, but for this we all decided for it to stay straight, because it still sticks to a proper suiting for a very proper engagement,” says Fereria, who, after being summoned by Cardi’s stylist, Kollin Carter, spent about three hours on the alterations.

The Brøgger suit is exclusively for sale at Barneys New York, but the luxury retailer had no idea that Cardi planned to don it in court. “We didn’t have anything to do with the styling, but we loved it,” says Barneys spokeswoman Brigitte Timmins.

The store posted a photo on Instagram of the Bronx-born rapper entering the courthouse with the caption, “Can you be sued for looking this good? We’ll let you be the judge of that.”

The Insta post has since been widely circulated on social media, garnering nearly 40,000 likes and thousands of comments praising Cardi — whose real name is Belcalis Almánzar — for her impeccable style.

But some social-media users weren’t fans: “Seriously, Barneys? This is in poor taste,” commented user @thevfcastro.

Critics like Tacopina — who has defended rappers Meek Mill and Jay-Z, as well as retired Yankees player Alex Rodriguez and Michael Jackson — say Cardi’s peacocking makes a mockery of the courtroom.

“Here’s a woman who got indicted by a grand jury with felony charges, and appears to only be concerned about what she’s wearing,” says Tacopina. “There’s going to be a ‘Come to Jesus’ moment with her, because it’s not consistent with someone who’s taking this seriously.”

Cardi’s attorney, who wouldn’t comment on the case, says his client is simply being authentic. “The most important thing is for them to feel comfortable on the witness stand,” says Jeff Kern. “I want them to be them and not channel me.”

Meanwhile, Loughlin — who was dropped from Netflix’s “Fuller House” after allegedly participating in a $25 million college admissions scam — seemed to treat her Boston court appearance on April 3 as a Hail Mary to salvage her wholesome image.

She showed up camera-ready in camel wide-legged trousers and a fitted blazer, with a gray sweater and cow-printed belt underneath. The 54-year-old sported a sleek blowout and told reporters, “I’m great!” as she strolled inside.

Clothing was the main focus of the highly publicized five-week trial of Anna Sorokin, aka Anna Delvey, that ended in May.

The 28-year-old — who was sentenced to up to 12 years in prison for posing as a German heiress, ripping off nearly $200,000 from lenders and attempting to steal millions from a hedge fund — broke down in tears and delayed proceedings three separate times because her stylist-curated wardrobe didn’t arrive on time at Rikers.

Before the trial began, Sorokin’s lawyer and the wannabe heiress had tapped celebrity stylist Anastasia Walker to dress her in largely black and white pieces from Yves Saint Laurent, Miu Miu and Victoria Beckham.

But the stylish garments, according to her lawyer, were about more than just Sorokin’s fussy demands.

“We were trying to broadcast that this was a woman who’s ambitious and an entrepreneur,” says Spodek. “Making her look fashionable . . . was important because, from our position, she didn’t trick anyone. [Her style] is what helped convince these people to want to help her out.”

Lohan, now 33, is perhaps the originator of the courtroom diva trend, after wearing dozens of attention-grabbing looks to her hearings in LA. Among the most sophisticated was a sky blue tailored suit with a navy turtleneck, oversize sunglasses and platforms that she wore in March 2012.

Spodek maintains that high-profile defendants such as Lohan and Cardi B should dress like themselves in court, instead of pretending to be “an angel going to church.”

“Trials are like live theater and ultimately a jury convicts or acquits based on what they see and what they hear,” says Spodek. “You want to be honest with the jurors. You don’t want to look like a liar.”

Men, on the other hand, have an easier time striking the perfect balance with their clothing.

“A-Rod dressed high fashion, but in a subtle and classy way that didn’t show disrespect for the system,” says Tacopina, referencing the baseball player’s array of well-fitted navy suits from his 2013 court hearings about his use of performance-enhancing drugs. “Not only did he pick the clothes himself, he even dressed me, too!”

And fashion can actually make or break a verdict, says Tacopina.

“It’s scary,” he says, “but if you listen to what these jurors talk about in the back, it’s often: ‘Did you see that tie he wore? Where does he think he is? At the Grammy Awards?’ ”