Earlier this week, Olympic champion Katie Ledecky placed a glass of chocolate milk atop her head, started swimming and made waves across the Internet. She kept that glass steady and swam 50 meters — from one end of the pool to the other — without spilling a drop. When she reached the wall, she punctuated the achievement by retrieving the milk and taking a big gulp.

For at least a few seconds, she made people forget that the Tokyo Olympics aren’t taking place right now, as originally scheduled. And while her feat won’t be recorded in any record books, as far as sports-related viral content goes, it was legen-dairy, drawing more than 4 million views on Twitter and nearly 3 million more on TikTok.

It was a spontaneous stunt serving as an advertisement for a sponsor that recently relaunched the successful “Got Milk?” campaign, but it was also an unusual athletic achievement that inspired youth swimmers to balance milk on their heads and Australians to do the same with pints of beer.

“The Got Milk team told me they were launching this campaign and asked if I’d be up for trying to something fun in the pool,” Ledecky told The Washington Post. “They kind of left it up to me to see what I could do. I said, ‘Let me get in the pool and play around a little bit.’ ”

Like everything she does in the pool, Ledecky made it look easier than it actually was. So how did she do it?

Steady head

If she had longer hair, it might not have been so simple. But Ledecky was able to stuff her hair inside a swim cap and create a surface just flat enough for the cup.

“I really wasn’t sure it would even work. I didn’t know if it’d sit on my head,” she said.

She performed the stunt in late July and first tried it a couple of times with an empty cup before adding the chocolate milk. On the first take, she made it across the pool without spilling any milk.

“I think a lot of people see it, and they of course think that I’m trying not to drop the cup,” she said. “But I was also trying not to get any water in the cup because I wanted to drink it afterwards. I didn’t want to ruin the milk.”

Breathing

Ledecky’s head position is perhaps the biggest departure from her typical swim. When she races, fans will notice that her head is regularly tilting to the side, a movement that’s built into her stroke. That’s because Ledecky is breathing throughout a race, turning her head to her right and barely allowing her mouth to exit the water and draw in air.

“There’s no way to bring your head up to breathe with a cup on your head,” said Ledecky, who resumed her Olympic training at the Stanford University pool in mid-June and is expected to chase five medals next summer in Tokyo.

For the milk stunt, she donned a snorkel, something she regularly uses in practice, which allowed her to keep her head steady and underwater the entire time.

Balance

Because she didn’t have to worry about breathing, her stroke actually was more metronomic than usual: left arm, right arm, repeat. When she has to turn her head to breathe, the result is a slightly uneven (though remarkably consistent) stroke: short left, long right, short left, long right. She began the viral swim with just a light kick off the wall, which steadied her, provided a bit of momentum and established a smooth pace.

“It doesn’t look like my stroke. Racing is more aggressive,” she said. “So it doesn’t look like my freestyle, but you still need technique.”

Balancing a cup is actually a common training tool used by backstrokers, who position it on their forehead as they move across the pool.

“It really helps with head position, keeping your head very still,” she said. “I really had to brace my core really hard. I could feel it afterwards. I had to be very smooth, and it just helps engage kind of your whole body.”

Stroke

It’s safe to say that if Ledecky was a breaststroker, with her head bobbing in and out of the water with each stroke, that glass would not have lasted long.

But Ledecky is a freestyle specialist — among the best ever, with world records at three distances — and by alternating arms, she was able to establish a steady rhythm.

While she wasn’t trying to generate much power, she took 36 strokes to cross the pool, which isn’t far off her usual number. Ledecky typically takes 38 to 39 strokes to swim the length of an Olympic-size pool. Perhaps most important, Ledecky wore fins and steadily kicked her legs the entire length of the pool, propelling her through the water but also keeping much of the most turbulent work far away from her head and the teetering milk glass.

“What I saw is that she’s able to do it with the help of an awesome, steady kick behind her and very rigid core to keep her body and stroke extremely stabilized,” explained Russell Mark, USA Swimming’s high-performance manager who helps the country’s top swimmers, including Ledecky, dissect and hone their technique. “She modified her stroke a little bit to not extend her arms forward as much, which helped minimize the side-to-side rotation and keeping her movement more compact than usual.”

Relaxation

Ledecky felt no pressure because the stakes were low. There was no cheering crowd or ticking clock. Only two other people were present, and though she said she nailed it in on the first try, there would be no penalty if she needed a couple of attempts. In fact, Ledecky did two more successful milk swims afterward but realized the first take was perfect.

Maybe the best part was watching the reaction after the video posted to her social media accounts Monday as her milk swim quickly went viral and inspired several copycat attempts, both in and out of water.

“I think it surprised all of us,” she said. “I thought maybe in the swimming world, people would find it fun and exciting. But it really resonated. It was fun.”

Read more about Olympic sports: