Dynamic column of images defined height preserving natural aspect ratio possible using just css?












1















BLUF: I was wondering if it is possible reproduce the results in this jsfiddle using just CSS. If so, is it supported in ie11?



I want to create a column (or a row) of images that show the whole image, preserve their natural aspect ratio and fit within a defined height (or width, for rows). Furthermore, I would like the container to end up with the width of the images. These will be used in reveal.js slides, eventually.



screenshot of image column generated with linked jsfiddle



javascript



function imageLoad () {
var boxes = document.getElementsByClassName("st-imagecol");
for (var i =0; i < boxes.length; i++) {
var box = boxes[i]
for (var j=0; j < box.childElementCount; j++) {
image = box.children[j];
var aspectRatio = image.naturalWidth/image.naturalHeight;
image.style.flexGrow = 1/aspectRatio;
}
setWidth(box);
window.addEventListener( "resize", function (){setWidth(box); return false;});
}
}
function setWidth(box) {
var image = box.firstElementChild
var aspectRatio = image.naturalWidth/image.naturalHeight;
//would prefer to change box.style.width but it doesn't work in iframe on jsfiddle
// it does stand alone, though... weird
// box.style.width = aspectRatio*image.height;
image.width = aspectRatio*image.height;
}


html



<body onload="imageLoad()">

<Section>
<div class="st-container-h st-available-parent">
<div class="st-flex-v st-available">
<h3>Lorem Ipsum</h3>
<div class="st-overflow-auto">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</p>
</div>
</div>
<div>
<div class="st-imagecol">
<img src="https://loremflickr.com/800/430/cat?random=1" alt=" Cat 1" />
<img src="https://loremflickr.com/680/700/cat?random=2" alt=" Cat 2" />
<img src="https://loremflickr.com/720/470/cat?random=3" alt="Cat 3" />
<img src="https://loremflickr.com/600/600/cat?random=4" alt="Cat 4" />
<img src="https://loremflickr.com/413/618/cat?random=5" alt="Cat 5" />

</div>
</div>
</div>
</Section>

</body>


css



.st-container-h p, .st-container-h h3 {
background-color: rgba( 0,0,0, 0.2);
border-radius: 12px;
}

.st-available {
display: flex;
align-items: center;
justify-content: center;
flex-grow: 1 !important;
}

.st-container-h > .st-available, .st-flex-h > .st-available {
width: 1px;
height: auto;
}
.st-available> * {
display: block!important;
margin: 0!important;
}
.st-available-parent > * {
flex-grow: 0;
}

.st-overflow-auto {
overflow: auto;
}

/*

.st-container-h {
display: flex;
justify-content: space-around;
height: 700px;
}

.st-flex-v {
display: flex;
flex-direction: column;
justify-content: space-around;
}

body {
display: flex;
align-items: center;
justify-content: center;
}
section {
width: 100vh;
height: 70vh;
}
.st-container-h {
height: 100%;
}
.st-imagecol {
display: flex;
flex-direction: column;
height: 100%;
max-width: 650px;
}
.st-imagecol > * {
height: 1px;
}









share|improve this question



























    1















    BLUF: I was wondering if it is possible reproduce the results in this jsfiddle using just CSS. If so, is it supported in ie11?



    I want to create a column (or a row) of images that show the whole image, preserve their natural aspect ratio and fit within a defined height (or width, for rows). Furthermore, I would like the container to end up with the width of the images. These will be used in reveal.js slides, eventually.



    screenshot of image column generated with linked jsfiddle



    javascript



    function imageLoad () {
    var boxes = document.getElementsByClassName("st-imagecol");
    for (var i =0; i < boxes.length; i++) {
    var box = boxes[i]
    for (var j=0; j < box.childElementCount; j++) {
    image = box.children[j];
    var aspectRatio = image.naturalWidth/image.naturalHeight;
    image.style.flexGrow = 1/aspectRatio;
    }
    setWidth(box);
    window.addEventListener( "resize", function (){setWidth(box); return false;});
    }
    }
    function setWidth(box) {
    var image = box.firstElementChild
    var aspectRatio = image.naturalWidth/image.naturalHeight;
    //would prefer to change box.style.width but it doesn't work in iframe on jsfiddle
    // it does stand alone, though... weird
    // box.style.width = aspectRatio*image.height;
    image.width = aspectRatio*image.height;
    }


    html



    <body onload="imageLoad()">

    <Section>
    <div class="st-container-h st-available-parent">
    <div class="st-flex-v st-available">
    <h3>Lorem Ipsum</h3>
    <div class="st-overflow-auto">
    <p>
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    </p>
    </div>
    </div>
    <div>
    <div class="st-imagecol">
    <img src="https://loremflickr.com/800/430/cat?random=1" alt=" Cat 1" />
    <img src="https://loremflickr.com/680/700/cat?random=2" alt=" Cat 2" />
    <img src="https://loremflickr.com/720/470/cat?random=3" alt="Cat 3" />
    <img src="https://loremflickr.com/600/600/cat?random=4" alt="Cat 4" />
    <img src="https://loremflickr.com/413/618/cat?random=5" alt="Cat 5" />

    </div>
    </div>
    </div>
    </Section>

    </body>


    css



    .st-container-h p, .st-container-h h3 {
    background-color: rgba( 0,0,0, 0.2);
    border-radius: 12px;
    }

    .st-available {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-grow: 1 !important;
    }

    .st-container-h > .st-available, .st-flex-h > .st-available {
    width: 1px;
    height: auto;
    }
    .st-available> * {
    display: block!important;
    margin: 0!important;
    }
    .st-available-parent > * {
    flex-grow: 0;
    }

    .st-overflow-auto {
    overflow: auto;
    }

    /*

    .st-container-h {
    display: flex;
    justify-content: space-around;
    height: 700px;
    }

    .st-flex-v {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    }

    body {
    display: flex;
    align-items: center;
    justify-content: center;
    }
    section {
    width: 100vh;
    height: 70vh;
    }
    .st-container-h {
    height: 100%;
    }
    .st-imagecol {
    display: flex;
    flex-direction: column;
    height: 100%;
    max-width: 650px;
    }
    .st-imagecol > * {
    height: 1px;
    }









    share|improve this question

























      1












      1








      1








      BLUF: I was wondering if it is possible reproduce the results in this jsfiddle using just CSS. If so, is it supported in ie11?



      I want to create a column (or a row) of images that show the whole image, preserve their natural aspect ratio and fit within a defined height (or width, for rows). Furthermore, I would like the container to end up with the width of the images. These will be used in reveal.js slides, eventually.



      screenshot of image column generated with linked jsfiddle



      javascript



      function imageLoad () {
      var boxes = document.getElementsByClassName("st-imagecol");
      for (var i =0; i < boxes.length; i++) {
      var box = boxes[i]
      for (var j=0; j < box.childElementCount; j++) {
      image = box.children[j];
      var aspectRatio = image.naturalWidth/image.naturalHeight;
      image.style.flexGrow = 1/aspectRatio;
      }
      setWidth(box);
      window.addEventListener( "resize", function (){setWidth(box); return false;});
      }
      }
      function setWidth(box) {
      var image = box.firstElementChild
      var aspectRatio = image.naturalWidth/image.naturalHeight;
      //would prefer to change box.style.width but it doesn't work in iframe on jsfiddle
      // it does stand alone, though... weird
      // box.style.width = aspectRatio*image.height;
      image.width = aspectRatio*image.height;
      }


      html



      <body onload="imageLoad()">

      <Section>
      <div class="st-container-h st-available-parent">
      <div class="st-flex-v st-available">
      <h3>Lorem Ipsum</h3>
      <div class="st-overflow-auto">
      <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
      </p>
      </div>
      </div>
      <div>
      <div class="st-imagecol">
      <img src="https://loremflickr.com/800/430/cat?random=1" alt=" Cat 1" />
      <img src="https://loremflickr.com/680/700/cat?random=2" alt=" Cat 2" />
      <img src="https://loremflickr.com/720/470/cat?random=3" alt="Cat 3" />
      <img src="https://loremflickr.com/600/600/cat?random=4" alt="Cat 4" />
      <img src="https://loremflickr.com/413/618/cat?random=5" alt="Cat 5" />

      </div>
      </div>
      </div>
      </Section>

      </body>


      css



      .st-container-h p, .st-container-h h3 {
      background-color: rgba( 0,0,0, 0.2);
      border-radius: 12px;
      }

      .st-available {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-grow: 1 !important;
      }

      .st-container-h > .st-available, .st-flex-h > .st-available {
      width: 1px;
      height: auto;
      }
      .st-available> * {
      display: block!important;
      margin: 0!important;
      }
      .st-available-parent > * {
      flex-grow: 0;
      }

      .st-overflow-auto {
      overflow: auto;
      }

      /*

      .st-container-h {
      display: flex;
      justify-content: space-around;
      height: 700px;
      }

      .st-flex-v {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      }

      body {
      display: flex;
      align-items: center;
      justify-content: center;
      }
      section {
      width: 100vh;
      height: 70vh;
      }
      .st-container-h {
      height: 100%;
      }
      .st-imagecol {
      display: flex;
      flex-direction: column;
      height: 100%;
      max-width: 650px;
      }
      .st-imagecol > * {
      height: 1px;
      }









      share|improve this question














      BLUF: I was wondering if it is possible reproduce the results in this jsfiddle using just CSS. If so, is it supported in ie11?



      I want to create a column (or a row) of images that show the whole image, preserve their natural aspect ratio and fit within a defined height (or width, for rows). Furthermore, I would like the container to end up with the width of the images. These will be used in reveal.js slides, eventually.



      screenshot of image column generated with linked jsfiddle



      javascript



      function imageLoad () {
      var boxes = document.getElementsByClassName("st-imagecol");
      for (var i =0; i < boxes.length; i++) {
      var box = boxes[i]
      for (var j=0; j < box.childElementCount; j++) {
      image = box.children[j];
      var aspectRatio = image.naturalWidth/image.naturalHeight;
      image.style.flexGrow = 1/aspectRatio;
      }
      setWidth(box);
      window.addEventListener( "resize", function (){setWidth(box); return false;});
      }
      }
      function setWidth(box) {
      var image = box.firstElementChild
      var aspectRatio = image.naturalWidth/image.naturalHeight;
      //would prefer to change box.style.width but it doesn't work in iframe on jsfiddle
      // it does stand alone, though... weird
      // box.style.width = aspectRatio*image.height;
      image.width = aspectRatio*image.height;
      }


      html



      <body onload="imageLoad()">

      <Section>
      <div class="st-container-h st-available-parent">
      <div class="st-flex-v st-available">
      <h3>Lorem Ipsum</h3>
      <div class="st-overflow-auto">
      <p>
      Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
      </p>
      </div>
      </div>
      <div>
      <div class="st-imagecol">
      <img src="https://loremflickr.com/800/430/cat?random=1" alt=" Cat 1" />
      <img src="https://loremflickr.com/680/700/cat?random=2" alt=" Cat 2" />
      <img src="https://loremflickr.com/720/470/cat?random=3" alt="Cat 3" />
      <img src="https://loremflickr.com/600/600/cat?random=4" alt="Cat 4" />
      <img src="https://loremflickr.com/413/618/cat?random=5" alt="Cat 5" />

      </div>
      </div>
      </div>
      </Section>

      </body>


      css



      .st-container-h p, .st-container-h h3 {
      background-color: rgba( 0,0,0, 0.2);
      border-radius: 12px;
      }

      .st-available {
      display: flex;
      align-items: center;
      justify-content: center;
      flex-grow: 1 !important;
      }

      .st-container-h > .st-available, .st-flex-h > .st-available {
      width: 1px;
      height: auto;
      }
      .st-available> * {
      display: block!important;
      margin: 0!important;
      }
      .st-available-parent > * {
      flex-grow: 0;
      }

      .st-overflow-auto {
      overflow: auto;
      }

      /*

      .st-container-h {
      display: flex;
      justify-content: space-around;
      height: 700px;
      }

      .st-flex-v {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      }

      body {
      display: flex;
      align-items: center;
      justify-content: center;
      }
      section {
      width: 100vh;
      height: 70vh;
      }
      .st-container-h {
      height: 100%;
      }
      .st-imagecol {
      display: flex;
      flex-direction: column;
      height: 100%;
      max-width: 650px;
      }
      .st-imagecol > * {
      height: 1px;
      }






      css image flexbox






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 19 at 3:10









      John KhaJohn Kha

      61




      61
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263742%2fdynamic-column-of-images-defined-height-preserving-natural-aspect-ratio-possible%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263742%2fdynamic-column-of-images-defined-height-preserving-natural-aspect-ratio-possible%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage