06-37-flexbox-vertical-centering.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Vertical centering of multiple items with flexbox</title>
  6. <style>
  7. html, body {
  8. height: 100%;
  9. }
  10. body {
  11. margin: 0;
  12. font-family: Avenir Next, Avenir, Franklin Gothic, sans-serif;
  13. }
  14. .flex-container {
  15. height: 100%;
  16. display: -webkit-flex;
  17. display: -ms-flexbox;
  18. display: -webkit-box;
  19. display: -moz-box;
  20. display: flex;
  21. -webkit-flex-direction: column;
  22. -ms-flex-direction: column;
  23. -webkit-box-orient: vertical;
  24. -webkit-box-direction: normal;
  25. -moz-box-orient: vertical;
  26. -moz-box-direction: normal;
  27. flex-direction: column;
  28. -webkit-justify-content: center;
  29. -ms-flex-pack: center;
  30. -webkit-box-pack: center;
  31. -moz-box-pack: center;
  32. justify-content: center;
  33. -webkit-align-items: center;
  34. -ms-flex-align: center;
  35. -webkit-box-align: center;
  36. -moz-box-align: center;
  37. align-items: center;
  38. background-color: #12459e;
  39. }
  40. .flex-item {
  41. max-width: 25em;
  42. padding: 2em;
  43. margin: 1em;
  44. background-color: #fff;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div class="flex-container">
  50. <div class="flex-item">
  51. <h2>Not so lost in space</h2>
  52. <p>This item sits right in the middle of its container, regardless of the dimensions of either.</p>
  53. </div>
  54. </div>
  55. </body>
  56. </html>