06-57-flexbox-nested-teasers-auto-margin.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Flexbox columns: article teaser, auto margin trick</title>
  6. <script src="js/html5shiv.min.js"></script>
  7. <style>
  8. body {
  9. font-family: Georgia, Times New Roman, serif;
  10. line-height: 1.5;
  11. max-width: 70em;
  12. margin: 0 auto;
  13. padding: 1em;
  14. }
  15. .article-teaser-group {
  16. display: -webkit-flex;
  17. display: -ms-flexbox;
  18. display: -webkit-box;
  19. display: -moz-box;
  20. display: flex;
  21. }
  22. .article-teaser {
  23. display: -webkit-flex;
  24. display: -ms-flexbox;
  25. display: -webkit-box;
  26. display: -moz-box;
  27. display: flex;
  28. -webkit-flex-direction: column;
  29. -ms-flex-direction: column;
  30. -webkit-box-orient: vertical;
  31. -webkit-box-direction: normal;
  32. -moz-box-orient: vertical;
  33. -moz-box-direction: normal;
  34. flex-direction: column;
  35. max-width: 20em;
  36. padding: 1em;
  37. margin: 1em;
  38. border: 1px solid #ccc;
  39. }
  40. .article-teaser h2 {
  41. font-family: Avenir, SegoeUI, Franklin Gothic, sans-serif;
  42. font-size: 1.25em;
  43. margin: 0 0 .5em 0;
  44. }
  45. .article-teaser-text {
  46. margin: .5em 0;
  47. }
  48. .article-teaser-text p {
  49. margin: 0 0 .5em;
  50. }
  51. /**
  52. * 1. This is a fix for a peculiar sizing bug on images in flexbox columns
  53. * that appears to only happen in Chrome. The intrinsic dimensions of the
  54. * image are taken to mean the main-axis size, and are interpreted as
  55. * the minimum size. This distorts the aspect ratio of the image.
  56. * Setting an explicit min-height value counters this problem.
  57. */
  58. .article-teaser img {
  59. width: 100%;
  60. min-height: 0; /* [1] */
  61. order: -1;
  62. margin: 0 0 .5em 0;
  63. }
  64. .article-teaser-more {
  65. margin-top: auto;
  66. }
  67. .article-teaser-more a {
  68. display: block;
  69. border-radius: .5em;
  70. background-color: #3372a0;
  71. font-family: Avenir, SegoeUI, Franklin Gothic, sans-serif;
  72. color: #fff;
  73. text-decoration: none;
  74. text-align: center;
  75. padding: .25em;
  76. }
  77. </style>
  78. </head>
  79. <body>
  80. <div class="article-teaser-group">
  81. <div class="article-teaser">
  82. <h2>The Dragon and other spaceships</h2>
  83. <div class="article-teaser-text">
  84. <p>There are actual spaceships,
  85. flying in space right now, probably. For example, there’s the International Space Station, which is
  86. a spaceship of sorts. Well, actually it’s a space station, which is even cooler!</p>
  87. </div>
  88. <!-- image from SpaceX on Flick: https://flic.kr/p/rzuQdb -->
  89. <img src="images/medium_spaceship.jpg" alt="The Dragon spaceship in orbit around Earth.">
  90. <p class="article-teaser-more">
  91. <a href="/spaceships">Read the whole Spaceship article</a>
  92. </p>
  93. </div>
  94. <div class="article-teaser">
  95. <h2>Mission to mars</h2>
  96. <div class="article-teaser-text">
  97. <p>This is a pretty cool article about spaceships flying to Mars.</p>
  98. </div>
  99. <!-- image from SpaceX on Flick: https://flic.kr/p/xGdh8d -->
  100. <img src="images/medium_mars.jpg" alt="A spaceship about to land on Mars.">
  101. <p class="article-teaser-more">
  102. <a href="/spaceships">Read the whole Mars article</a>
  103. </p>
  104. </div>
  105. </div>
  106. </body>
  107. </html>