Responsive (background) image in a bootstrap column to match the height of the content in adjacent column











up vote
0
down vote

favorite












The image in the left column needs to be responsive based on the height of the content in the adjacent column.



In this screenshot here:
enter image description here
the image takes up too much vertical space.



Just using the img-fluid class on an image doesn't work because that's not stretching the image to the required height.



background-size: cover; is what I'd normally need but then I'd need to set a fixed height which is the opposite of responsive.



So, how to make the image (or background image) responsive AND at the same time have it match the height of the content in the adjacent column?



Current code:






<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container-fluid">
<div class="row">
<div class="col-6 px-0">
<img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
</div>
<div class="col-6">
Some text...<br>
more<br>
text<br>
and<br>
stuff<br>
for<br>
more<br>
height
</div>
</div>
</div>












share|improve this question


























    up vote
    0
    down vote

    favorite












    The image in the left column needs to be responsive based on the height of the content in the adjacent column.



    In this screenshot here:
    enter image description here
    the image takes up too much vertical space.



    Just using the img-fluid class on an image doesn't work because that's not stretching the image to the required height.



    background-size: cover; is what I'd normally need but then I'd need to set a fixed height which is the opposite of responsive.



    So, how to make the image (or background image) responsive AND at the same time have it match the height of the content in the adjacent column?



    Current code:






    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <div class="container-fluid">
    <div class="row">
    <div class="col-6 px-0">
    <img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
    </div>
    <div class="col-6">
    Some text...<br>
    more<br>
    text<br>
    and<br>
    stuff<br>
    for<br>
    more<br>
    height
    </div>
    </div>
    </div>












    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      The image in the left column needs to be responsive based on the height of the content in the adjacent column.



      In this screenshot here:
      enter image description here
      the image takes up too much vertical space.



      Just using the img-fluid class on an image doesn't work because that's not stretching the image to the required height.



      background-size: cover; is what I'd normally need but then I'd need to set a fixed height which is the opposite of responsive.



      So, how to make the image (or background image) responsive AND at the same time have it match the height of the content in the adjacent column?



      Current code:






      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
      <div class="container-fluid">
      <div class="row">
      <div class="col-6 px-0">
      <img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
      </div>
      <div class="col-6">
      Some text...<br>
      more<br>
      text<br>
      and<br>
      stuff<br>
      for<br>
      more<br>
      height
      </div>
      </div>
      </div>












      share|improve this question













      The image in the left column needs to be responsive based on the height of the content in the adjacent column.



      In this screenshot here:
      enter image description here
      the image takes up too much vertical space.



      Just using the img-fluid class on an image doesn't work because that's not stretching the image to the required height.



      background-size: cover; is what I'd normally need but then I'd need to set a fixed height which is the opposite of responsive.



      So, how to make the image (or background image) responsive AND at the same time have it match the height of the content in the adjacent column?



      Current code:






      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
      <div class="container-fluid">
      <div class="row">
      <div class="col-6 px-0">
      <img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
      </div>
      <div class="col-6">
      Some text...<br>
      more<br>
      text<br>
      and<br>
      stuff<br>
      for<br>
      more<br>
      height
      </div>
      </div>
      </div>








      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
      <div class="container-fluid">
      <div class="row">
      <div class="col-6 px-0">
      <img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
      </div>
      <div class="col-6">
      Some text...<br>
      more<br>
      text<br>
      and<br>
      stuff<br>
      for<br>
      more<br>
      height
      </div>
      </div>
      </div>





      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
      <div class="container-fluid">
      <div class="row">
      <div class="col-6 px-0">
      <img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
      </div>
      <div class="col-6">
      Some text...<br>
      more<br>
      text<br>
      and<br>
      stuff<br>
      for<br>
      more<br>
      height
      </div>
      </div>
      </div>






      bootstrap-4






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 13:03









      WebDevBooster

      7,92072642




      7,92072642
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You could use an absolute positioned inner div to contain the image. Since the row is flexbox, that's the only way to shrink the left col to the height of the text in the right col.



          <div class="container-fluid px-0">
          <div class="row">
          <!-- img-fluid normal up to 'md' width -->
          <div class="col d-md-none">
          <img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
          </div>
          <!-- from 'md' width onwards convert to flex column
          + add an absolute positioned inner div to contain the image -->
          <div class="col-md-6 px-0 d-none d-md-flex flex-column justify-content-center o-hidden">
          <div class="position-absolute o-hidden">
          <img class="mh-100 mw-100 d-block" src="https://via.placeholder.com/3000x2000" alt="">
          </div>
          </div>
          <div class="col-md-6">
          Some text...
          <br> more
          <br> text
          <br> and
          <br> stuff
          <br> for
          <br> more
          <br> height
          </div>
          </div>
          </div>

          .o-hidden {
          overflow:hidden;
          }


          https://www.codeply.com/go/4fyb94J3OX






          share|improve this answer























          • You are the best, dear. Thank you!
            – WebDevBooster
            Nov 9 at 14:39


















          up vote
          0
          down vote













          You can set image height based on the text height in the other col via js like this






          $(document).ready(function() {
          var colTwoHeight = $('#colTwoInner').height();
          $('#colOneImg').css('cssText', 'height:'+colTwoHeight+'px !important;');
          });

          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
          <div class="container-fluid">
          <div class="row">
          <div id="colOne" class="col-6 px-0">
          <img id="colOneImg" class="" src="https://via.placeholder.com/3000x2000" alt="">
          </div>
          <div id="colTwo" class="col-6">
          <div id="colTwoInner">
          Some text...<br>
          more<br>
          text<br>
          and<br>
          stuff<br>
          for<br>
          more<br>
          height
          </div>
          </div>
          </div>
          </div>
          <script
          src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
          integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
          crossorigin="anonymous"></script>








          share|improve this answer





















            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',
            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%2f53226250%2fresponsive-background-image-in-a-bootstrap-column-to-match-the-height-of-the-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            You could use an absolute positioned inner div to contain the image. Since the row is flexbox, that's the only way to shrink the left col to the height of the text in the right col.



            <div class="container-fluid px-0">
            <div class="row">
            <!-- img-fluid normal up to 'md' width -->
            <div class="col d-md-none">
            <img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
            </div>
            <!-- from 'md' width onwards convert to flex column
            + add an absolute positioned inner div to contain the image -->
            <div class="col-md-6 px-0 d-none d-md-flex flex-column justify-content-center o-hidden">
            <div class="position-absolute o-hidden">
            <img class="mh-100 mw-100 d-block" src="https://via.placeholder.com/3000x2000" alt="">
            </div>
            </div>
            <div class="col-md-6">
            Some text...
            <br> more
            <br> text
            <br> and
            <br> stuff
            <br> for
            <br> more
            <br> height
            </div>
            </div>
            </div>

            .o-hidden {
            overflow:hidden;
            }


            https://www.codeply.com/go/4fyb94J3OX






            share|improve this answer























            • You are the best, dear. Thank you!
              – WebDevBooster
              Nov 9 at 14:39















            up vote
            1
            down vote



            accepted










            You could use an absolute positioned inner div to contain the image. Since the row is flexbox, that's the only way to shrink the left col to the height of the text in the right col.



            <div class="container-fluid px-0">
            <div class="row">
            <!-- img-fluid normal up to 'md' width -->
            <div class="col d-md-none">
            <img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
            </div>
            <!-- from 'md' width onwards convert to flex column
            + add an absolute positioned inner div to contain the image -->
            <div class="col-md-6 px-0 d-none d-md-flex flex-column justify-content-center o-hidden">
            <div class="position-absolute o-hidden">
            <img class="mh-100 mw-100 d-block" src="https://via.placeholder.com/3000x2000" alt="">
            </div>
            </div>
            <div class="col-md-6">
            Some text...
            <br> more
            <br> text
            <br> and
            <br> stuff
            <br> for
            <br> more
            <br> height
            </div>
            </div>
            </div>

            .o-hidden {
            overflow:hidden;
            }


            https://www.codeply.com/go/4fyb94J3OX






            share|improve this answer























            • You are the best, dear. Thank you!
              – WebDevBooster
              Nov 9 at 14:39













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            You could use an absolute positioned inner div to contain the image. Since the row is flexbox, that's the only way to shrink the left col to the height of the text in the right col.



            <div class="container-fluid px-0">
            <div class="row">
            <!-- img-fluid normal up to 'md' width -->
            <div class="col d-md-none">
            <img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
            </div>
            <!-- from 'md' width onwards convert to flex column
            + add an absolute positioned inner div to contain the image -->
            <div class="col-md-6 px-0 d-none d-md-flex flex-column justify-content-center o-hidden">
            <div class="position-absolute o-hidden">
            <img class="mh-100 mw-100 d-block" src="https://via.placeholder.com/3000x2000" alt="">
            </div>
            </div>
            <div class="col-md-6">
            Some text...
            <br> more
            <br> text
            <br> and
            <br> stuff
            <br> for
            <br> more
            <br> height
            </div>
            </div>
            </div>

            .o-hidden {
            overflow:hidden;
            }


            https://www.codeply.com/go/4fyb94J3OX






            share|improve this answer














            You could use an absolute positioned inner div to contain the image. Since the row is flexbox, that's the only way to shrink the left col to the height of the text in the right col.



            <div class="container-fluid px-0">
            <div class="row">
            <!-- img-fluid normal up to 'md' width -->
            <div class="col d-md-none">
            <img class="img-fluid" src="https://via.placeholder.com/3000x2000" alt="">
            </div>
            <!-- from 'md' width onwards convert to flex column
            + add an absolute positioned inner div to contain the image -->
            <div class="col-md-6 px-0 d-none d-md-flex flex-column justify-content-center o-hidden">
            <div class="position-absolute o-hidden">
            <img class="mh-100 mw-100 d-block" src="https://via.placeholder.com/3000x2000" alt="">
            </div>
            </div>
            <div class="col-md-6">
            Some text...
            <br> more
            <br> text
            <br> and
            <br> stuff
            <br> for
            <br> more
            <br> height
            </div>
            </div>
            </div>

            .o-hidden {
            overflow:hidden;
            }


            https://www.codeply.com/go/4fyb94J3OX







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 9 at 14:38









            WebDevBooster

            7,92072642




            7,92072642










            answered Nov 9 at 13:25









            Zim

            178k45375356




            178k45375356












            • You are the best, dear. Thank you!
              – WebDevBooster
              Nov 9 at 14:39


















            • You are the best, dear. Thank you!
              – WebDevBooster
              Nov 9 at 14:39
















            You are the best, dear. Thank you!
            – WebDevBooster
            Nov 9 at 14:39




            You are the best, dear. Thank you!
            – WebDevBooster
            Nov 9 at 14:39












            up vote
            0
            down vote













            You can set image height based on the text height in the other col via js like this






            $(document).ready(function() {
            var colTwoHeight = $('#colTwoInner').height();
            $('#colOneImg').css('cssText', 'height:'+colTwoHeight+'px !important;');
            });

            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
            <div class="container-fluid">
            <div class="row">
            <div id="colOne" class="col-6 px-0">
            <img id="colOneImg" class="" src="https://via.placeholder.com/3000x2000" alt="">
            </div>
            <div id="colTwo" class="col-6">
            <div id="colTwoInner">
            Some text...<br>
            more<br>
            text<br>
            and<br>
            stuff<br>
            for<br>
            more<br>
            height
            </div>
            </div>
            </div>
            </div>
            <script
            src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
            integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
            crossorigin="anonymous"></script>








            share|improve this answer

























              up vote
              0
              down vote













              You can set image height based on the text height in the other col via js like this






              $(document).ready(function() {
              var colTwoHeight = $('#colTwoInner').height();
              $('#colOneImg').css('cssText', 'height:'+colTwoHeight+'px !important;');
              });

              <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
              <div class="container-fluid">
              <div class="row">
              <div id="colOne" class="col-6 px-0">
              <img id="colOneImg" class="" src="https://via.placeholder.com/3000x2000" alt="">
              </div>
              <div id="colTwo" class="col-6">
              <div id="colTwoInner">
              Some text...<br>
              more<br>
              text<br>
              and<br>
              stuff<br>
              for<br>
              more<br>
              height
              </div>
              </div>
              </div>
              </div>
              <script
              src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
              integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
              crossorigin="anonymous"></script>








              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You can set image height based on the text height in the other col via js like this






                $(document).ready(function() {
                var colTwoHeight = $('#colTwoInner').height();
                $('#colOneImg').css('cssText', 'height:'+colTwoHeight+'px !important;');
                });

                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
                <div class="container-fluid">
                <div class="row">
                <div id="colOne" class="col-6 px-0">
                <img id="colOneImg" class="" src="https://via.placeholder.com/3000x2000" alt="">
                </div>
                <div id="colTwo" class="col-6">
                <div id="colTwoInner">
                Some text...<br>
                more<br>
                text<br>
                and<br>
                stuff<br>
                for<br>
                more<br>
                height
                </div>
                </div>
                </div>
                </div>
                <script
                src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
                integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
                crossorigin="anonymous"></script>








                share|improve this answer












                You can set image height based on the text height in the other col via js like this






                $(document).ready(function() {
                var colTwoHeight = $('#colTwoInner').height();
                $('#colOneImg').css('cssText', 'height:'+colTwoHeight+'px !important;');
                });

                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
                <div class="container-fluid">
                <div class="row">
                <div id="colOne" class="col-6 px-0">
                <img id="colOneImg" class="" src="https://via.placeholder.com/3000x2000" alt="">
                </div>
                <div id="colTwo" class="col-6">
                <div id="colTwoInner">
                Some text...<br>
                more<br>
                text<br>
                and<br>
                stuff<br>
                for<br>
                more<br>
                height
                </div>
                </div>
                </div>
                </div>
                <script
                src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
                integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
                crossorigin="anonymous"></script>








                $(document).ready(function() {
                var colTwoHeight = $('#colTwoInner').height();
                $('#colOneImg').css('cssText', 'height:'+colTwoHeight+'px !important;');
                });

                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
                <div class="container-fluid">
                <div class="row">
                <div id="colOne" class="col-6 px-0">
                <img id="colOneImg" class="" src="https://via.placeholder.com/3000x2000" alt="">
                </div>
                <div id="colTwo" class="col-6">
                <div id="colTwoInner">
                Some text...<br>
                more<br>
                text<br>
                and<br>
                stuff<br>
                for<br>
                more<br>
                height
                </div>
                </div>
                </div>
                </div>
                <script
                src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
                integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
                crossorigin="anonymous"></script>





                $(document).ready(function() {
                var colTwoHeight = $('#colTwoInner').height();
                $('#colOneImg').css('cssText', 'height:'+colTwoHeight+'px !important;');
                });

                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
                <div class="container-fluid">
                <div class="row">
                <div id="colOne" class="col-6 px-0">
                <img id="colOneImg" class="" src="https://via.placeholder.com/3000x2000" alt="">
                </div>
                <div id="colTwo" class="col-6">
                <div id="colTwoInner">
                Some text...<br>
                more<br>
                text<br>
                and<br>
                stuff<br>
                for<br>
                more<br>
                height
                </div>
                </div>
                </div>
                </div>
                <script
                src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
                integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="
                crossorigin="anonymous"></script>






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 9 at 13:28









                Sherif Salah

                37327




                37327






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226250%2fresponsive-background-image-in-a-bootstrap-column-to-match-the-height-of-the-c%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

                    Landwehr

                    Reims

                    Schenkenzell