Combine background size percentage, and background size cover











up vote
0
down vote

favorite












Here, I have some divs of various sizes, and I'm applying a background image to them:






div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





Now, I only want to display the image on the left half of the div, but I want it to cover the full height. The background shouldn't warp, instead it should zoom/stretch.



One way to do this is to add position right on it:






div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

background-position: top right 50px; //Actually in my situation I would just use vw;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





But the problem with this is that it only shows the right hand side of the image, whereas in my scenario I want it to either show from the left, or show from the center.



Is there a way to apply a clip to the background image? My alternative solution is to lay a div on top to hide it.










share|improve this question






















  • I don't get how you want it to show "either from the left or from the center". Can you please clarify that (I mean, when do you want it showing from the left and when to show it from the center)?
    – Wais Kamal
    Nov 8 at 6:53












  • @WaisKamal - I'm saying that it would be nice to have a property that allows me to specifiy which way it clips.
    – dwjohnston
    Nov 8 at 7:04















up vote
0
down vote

favorite












Here, I have some divs of various sizes, and I'm applying a background image to them:






div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





Now, I only want to display the image on the left half of the div, but I want it to cover the full height. The background shouldn't warp, instead it should zoom/stretch.



One way to do this is to add position right on it:






div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

background-position: top right 50px; //Actually in my situation I would just use vw;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





But the problem with this is that it only shows the right hand side of the image, whereas in my scenario I want it to either show from the left, or show from the center.



Is there a way to apply a clip to the background image? My alternative solution is to lay a div on top to hide it.










share|improve this question






















  • I don't get how you want it to show "either from the left or from the center". Can you please clarify that (I mean, when do you want it showing from the left and when to show it from the center)?
    – Wais Kamal
    Nov 8 at 6:53












  • @WaisKamal - I'm saying that it would be nice to have a property that allows me to specifiy which way it clips.
    – dwjohnston
    Nov 8 at 7:04













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Here, I have some divs of various sizes, and I'm applying a background image to them:






div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





Now, I only want to display the image on the left half of the div, but I want it to cover the full height. The background shouldn't warp, instead it should zoom/stretch.



One way to do this is to add position right on it:






div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

background-position: top right 50px; //Actually in my situation I would just use vw;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





But the problem with this is that it only shows the right hand side of the image, whereas in my scenario I want it to either show from the left, or show from the center.



Is there a way to apply a clip to the background image? My alternative solution is to lay a div on top to hide it.










share|improve this question













Here, I have some divs of various sizes, and I'm applying a background image to them:






div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





Now, I only want to display the image on the left half of the div, but I want it to cover the full height. The background shouldn't warp, instead it should zoom/stretch.



One way to do this is to add position right on it:






div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

background-position: top right 50px; //Actually in my situation I would just use vw;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





But the problem with this is that it only shows the right hand side of the image, whereas in my scenario I want it to either show from the left, or show from the center.



Is there a way to apply a clip to the background image? My alternative solution is to lay a div on top to hide it.






div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

background-position: top right 50px; //Actually in my situation I would just use vw;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>





div {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: cover;
background-repeat: no-repeat;

display: inline-block;
margin: 2em;

background-position: top right 50px; //Actually in my situation I would just use vw;

}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px ;
}

.c {
width: 200px;
height: 200px;
}

<div class ="a"> </div> 
<div class ="b"> </div>
<div class ="c"> </div>






css






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 6:51









dwjohnston

2,453104387




2,453104387












  • I don't get how you want it to show "either from the left or from the center". Can you please clarify that (I mean, when do you want it showing from the left and when to show it from the center)?
    – Wais Kamal
    Nov 8 at 6:53












  • @WaisKamal - I'm saying that it would be nice to have a property that allows me to specifiy which way it clips.
    – dwjohnston
    Nov 8 at 7:04


















  • I don't get how you want it to show "either from the left or from the center". Can you please clarify that (I mean, when do you want it showing from the left and when to show it from the center)?
    – Wais Kamal
    Nov 8 at 6:53












  • @WaisKamal - I'm saying that it would be nice to have a property that allows me to specifiy which way it clips.
    – dwjohnston
    Nov 8 at 7:04
















I don't get how you want it to show "either from the left or from the center". Can you please clarify that (I mean, when do you want it showing from the left and when to show it from the center)?
– Wais Kamal
Nov 8 at 6:53






I don't get how you want it to show "either from the left or from the center". Can you please clarify that (I mean, when do you want it showing from the left and when to show it from the center)?
– Wais Kamal
Nov 8 at 6:53














@WaisKamal - I'm saying that it would be nice to have a property that allows me to specifiy which way it clips.
– dwjohnston
Nov 8 at 7:04




@WaisKamal - I'm saying that it would be nice to have a property that allows me to specifiy which way it clips.
– dwjohnston
Nov 8 at 7:04












2 Answers
2






active

oldest

votes

















up vote
1
down vote













I would consider a pseudo element that will be your background layer:






.box {
border: solid 2px red;
background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
background-size: 0 0;
display: inline-block;
margin: 2em;
position: relative;
}
.box:before {
content:"";
position:absolute;
top:0;
left:0;
right:50%;
bottom:0;
background-image:inherit;
background-size:cover;
}

.a {
width: 100px;
height: 200px;
}

.b {
width: 200px;
height: 100px;
}

.c {
width: 200px;
height: 200px;
}

<div class="a box"> </div>
<div class="b box"> </div>
<div class="c box"> </div>








share|improve this answer




























    up vote
    -2
    down vote













    you can try to add background-attachment:fixed; to your background. hope it works.






    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%2f53202737%2fcombine-background-size-percentage-and-background-size-cover%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













      I would consider a pseudo element that will be your background layer:






      .box {
      border: solid 2px red;
      background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
      background-size: 0 0;
      display: inline-block;
      margin: 2em;
      position: relative;
      }
      .box:before {
      content:"";
      position:absolute;
      top:0;
      left:0;
      right:50%;
      bottom:0;
      background-image:inherit;
      background-size:cover;
      }

      .a {
      width: 100px;
      height: 200px;
      }

      .b {
      width: 200px;
      height: 100px;
      }

      .c {
      width: 200px;
      height: 200px;
      }

      <div class="a box"> </div>
      <div class="b box"> </div>
      <div class="c box"> </div>








      share|improve this answer

























        up vote
        1
        down vote













        I would consider a pseudo element that will be your background layer:






        .box {
        border: solid 2px red;
        background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
        background-size: 0 0;
        display: inline-block;
        margin: 2em;
        position: relative;
        }
        .box:before {
        content:"";
        position:absolute;
        top:0;
        left:0;
        right:50%;
        bottom:0;
        background-image:inherit;
        background-size:cover;
        }

        .a {
        width: 100px;
        height: 200px;
        }

        .b {
        width: 200px;
        height: 100px;
        }

        .c {
        width: 200px;
        height: 200px;
        }

        <div class="a box"> </div>
        <div class="b box"> </div>
        <div class="c box"> </div>








        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          I would consider a pseudo element that will be your background layer:






          .box {
          border: solid 2px red;
          background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
          background-size: 0 0;
          display: inline-block;
          margin: 2em;
          position: relative;
          }
          .box:before {
          content:"";
          position:absolute;
          top:0;
          left:0;
          right:50%;
          bottom:0;
          background-image:inherit;
          background-size:cover;
          }

          .a {
          width: 100px;
          height: 200px;
          }

          .b {
          width: 200px;
          height: 100px;
          }

          .c {
          width: 200px;
          height: 200px;
          }

          <div class="a box"> </div>
          <div class="b box"> </div>
          <div class="c box"> </div>








          share|improve this answer












          I would consider a pseudo element that will be your background layer:






          .box {
          border: solid 2px red;
          background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
          background-size: 0 0;
          display: inline-block;
          margin: 2em;
          position: relative;
          }
          .box:before {
          content:"";
          position:absolute;
          top:0;
          left:0;
          right:50%;
          bottom:0;
          background-image:inherit;
          background-size:cover;
          }

          .a {
          width: 100px;
          height: 200px;
          }

          .b {
          width: 200px;
          height: 100px;
          }

          .c {
          width: 200px;
          height: 200px;
          }

          <div class="a box"> </div>
          <div class="b box"> </div>
          <div class="c box"> </div>








          .box {
          border: solid 2px red;
          background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
          background-size: 0 0;
          display: inline-block;
          margin: 2em;
          position: relative;
          }
          .box:before {
          content:"";
          position:absolute;
          top:0;
          left:0;
          right:50%;
          bottom:0;
          background-image:inherit;
          background-size:cover;
          }

          .a {
          width: 100px;
          height: 200px;
          }

          .b {
          width: 200px;
          height: 100px;
          }

          .c {
          width: 200px;
          height: 200px;
          }

          <div class="a box"> </div>
          <div class="b box"> </div>
          <div class="c box"> </div>





          .box {
          border: solid 2px red;
          background-image: url('https://www.360nobs.com/wp-content/uploads/2016/02/lady-gaga.jpg');
          background-size: 0 0;
          display: inline-block;
          margin: 2em;
          position: relative;
          }
          .box:before {
          content:"";
          position:absolute;
          top:0;
          left:0;
          right:50%;
          bottom:0;
          background-image:inherit;
          background-size:cover;
          }

          .a {
          width: 100px;
          height: 200px;
          }

          .b {
          width: 200px;
          height: 100px;
          }

          .c {
          width: 200px;
          height: 200px;
          }

          <div class="a box"> </div>
          <div class="b box"> </div>
          <div class="c box"> </div>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 14:39









          Temani Afif

          61.4k93572




          61.4k93572
























              up vote
              -2
              down vote













              you can try to add background-attachment:fixed; to your background. hope it works.






              share|improve this answer

























                up vote
                -2
                down vote













                you can try to add background-attachment:fixed; to your background. hope it works.






                share|improve this answer























                  up vote
                  -2
                  down vote










                  up vote
                  -2
                  down vote









                  you can try to add background-attachment:fixed; to your background. hope it works.






                  share|improve this answer












                  you can try to add background-attachment:fixed; to your background. hope it works.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 6:58









                  JL Barcelona

                  334




                  334






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53202737%2fcombine-background-size-percentage-and-background-size-cover%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







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini