Interactive online courses HTML Academy
2026-03-09 12:22 Diff

Loading…
Everything will be ready in few seconds

  • Theory
  • Theory
  • Comments

Perfect centering, flex alignment

Now let’s center the items using the flex container properties without margin: auto in the child elements.

Notice how items are distributed differently depending on the various values of the justify-content property.

Comments

  • index.html
  • style.css

HTML

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Perfect centering, flex alignment</title> <link href="course.css" rel="stylesheet"> <link href="style.css" rel="stylesheet"> </head> <body class="subtle"> <article class="item"> <h1>“Wolverine” Scratching Post</h1> <div class="pictures"> <div class="picture"> <img src="img/toy.png" alt=""> </div> <div class="picture"> <img src="img/toy.png" alt=""> </div> </div> <div class="controls"> <a href="/">Buy</a> </div> </article> </body> </html>

CSS

.pictures { display: flex; } .picture { margin: auto; }

Thanks! We’ll fix everything at once!

The code has changed, click “Refresh” or turn autorun on.

You’ve gone to a different page

Click inside the mini-browser to shift the focus onto this window.

100%

  1. Remove margin: auto from the .picture blocks,
  2. then set flex items to be distributed along the center of the main axis for the .pictures block,
  3. and center-align the cross axis.
  4. Then substitute space-around for arrangement along the main axis.