CSS: Fit image with % w/h in div with % width











up vote
0
down vote

favorite












I have an image that I want to scale to 50% w+h but need it to fit in a div that has a width set to 25%. I keep getting a distorted image with a height set to 50% but the width set to 25%.



enter image description here



I'm using this css:



.pb_benefits_img_wrapper {
height: 100%;
width:25%;
float:left;
text-align:right;
padding-right: 15px;
padding-top: 10px;
border:1px solid red;

}

.pb_benefits_img_wrapper > img {
width: 50%;
min-height: 50%;
}


Any help would be appreciated.










share|improve this question


















  • 4




    You set image to be minimum 50% of height. Since your container is very high, that's where your distortion comes from. Set max-height and max-width instead and make sure you have big enough images to fill at-least 50%
    – Justinas
    Nov 19 at 11:50










  • Does the parent box (img_wrapper) need to be 100%? this is causing the container to be far larger than your intended image.
    – r007ed
    Nov 19 at 11:58















up vote
0
down vote

favorite












I have an image that I want to scale to 50% w+h but need it to fit in a div that has a width set to 25%. I keep getting a distorted image with a height set to 50% but the width set to 25%.



enter image description here



I'm using this css:



.pb_benefits_img_wrapper {
height: 100%;
width:25%;
float:left;
text-align:right;
padding-right: 15px;
padding-top: 10px;
border:1px solid red;

}

.pb_benefits_img_wrapper > img {
width: 50%;
min-height: 50%;
}


Any help would be appreciated.










share|improve this question


















  • 4




    You set image to be minimum 50% of height. Since your container is very high, that's where your distortion comes from. Set max-height and max-width instead and make sure you have big enough images to fill at-least 50%
    – Justinas
    Nov 19 at 11:50










  • Does the parent box (img_wrapper) need to be 100%? this is causing the container to be far larger than your intended image.
    – r007ed
    Nov 19 at 11:58













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an image that I want to scale to 50% w+h but need it to fit in a div that has a width set to 25%. I keep getting a distorted image with a height set to 50% but the width set to 25%.



enter image description here



I'm using this css:



.pb_benefits_img_wrapper {
height: 100%;
width:25%;
float:left;
text-align:right;
padding-right: 15px;
padding-top: 10px;
border:1px solid red;

}

.pb_benefits_img_wrapper > img {
width: 50%;
min-height: 50%;
}


Any help would be appreciated.










share|improve this question













I have an image that I want to scale to 50% w+h but need it to fit in a div that has a width set to 25%. I keep getting a distorted image with a height set to 50% but the width set to 25%.



enter image description here



I'm using this css:



.pb_benefits_img_wrapper {
height: 100%;
width:25%;
float:left;
text-align:right;
padding-right: 15px;
padding-top: 10px;
border:1px solid red;

}

.pb_benefits_img_wrapper > img {
width: 50%;
min-height: 50%;
}


Any help would be appreciated.







css image






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 11:48









PruitIgoe

2,7631250105




2,7631250105








  • 4




    You set image to be minimum 50% of height. Since your container is very high, that's where your distortion comes from. Set max-height and max-width instead and make sure you have big enough images to fill at-least 50%
    – Justinas
    Nov 19 at 11:50










  • Does the parent box (img_wrapper) need to be 100%? this is causing the container to be far larger than your intended image.
    – r007ed
    Nov 19 at 11:58














  • 4




    You set image to be minimum 50% of height. Since your container is very high, that's where your distortion comes from. Set max-height and max-width instead and make sure you have big enough images to fill at-least 50%
    – Justinas
    Nov 19 at 11:50










  • Does the parent box (img_wrapper) need to be 100%? this is causing the container to be far larger than your intended image.
    – r007ed
    Nov 19 at 11:58








4




4




You set image to be minimum 50% of height. Since your container is very high, that's where your distortion comes from. Set max-height and max-width instead and make sure you have big enough images to fill at-least 50%
– Justinas
Nov 19 at 11:50




You set image to be minimum 50% of height. Since your container is very high, that's where your distortion comes from. Set max-height and max-width instead and make sure you have big enough images to fill at-least 50%
– Justinas
Nov 19 at 11:50












Does the parent box (img_wrapper) need to be 100%? this is causing the container to be far larger than your intended image.
– r007ed
Nov 19 at 11:58




Does the parent box (img_wrapper) need to be 100%? this is causing the container to be far larger than your intended image.
– r007ed
Nov 19 at 11:58












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










I would not say it's ideal but hope below code will help you.



<style>
img.image{
visibility:hidden;
width:100%;
height:auto;
}
.imageContainer{
background-image:url("backgroundImage.webp");
background-size:cover;
background-position:50%;
background-repeat:no-repeat;
width:25%;
display:inline-block;
}
</style>

<div class="imageContainer"><img class="image" src="backgroundImage.webp" alt="sample image" /> </div>


If the image's served is a dynamic one then.Slight change will be the way the image is served.






share|improve this answer























  • ha, that's exactly what I did (kind of). Just trying to get it to stop resizing when ti reaches a certain width (original img is 303x303, want it to stop when the image is 1/4 (78px roughly).
    – PruitIgoe
    Nov 19 at 12:05











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%2f53374007%2fcss-fit-image-with-w-h-in-div-with-width%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










I would not say it's ideal but hope below code will help you.



<style>
img.image{
visibility:hidden;
width:100%;
height:auto;
}
.imageContainer{
background-image:url("backgroundImage.webp");
background-size:cover;
background-position:50%;
background-repeat:no-repeat;
width:25%;
display:inline-block;
}
</style>

<div class="imageContainer"><img class="image" src="backgroundImage.webp" alt="sample image" /> </div>


If the image's served is a dynamic one then.Slight change will be the way the image is served.






share|improve this answer























  • ha, that's exactly what I did (kind of). Just trying to get it to stop resizing when ti reaches a certain width (original img is 303x303, want it to stop when the image is 1/4 (78px roughly).
    – PruitIgoe
    Nov 19 at 12:05















up vote
1
down vote



accepted










I would not say it's ideal but hope below code will help you.



<style>
img.image{
visibility:hidden;
width:100%;
height:auto;
}
.imageContainer{
background-image:url("backgroundImage.webp");
background-size:cover;
background-position:50%;
background-repeat:no-repeat;
width:25%;
display:inline-block;
}
</style>

<div class="imageContainer"><img class="image" src="backgroundImage.webp" alt="sample image" /> </div>


If the image's served is a dynamic one then.Slight change will be the way the image is served.






share|improve this answer























  • ha, that's exactly what I did (kind of). Just trying to get it to stop resizing when ti reaches a certain width (original img is 303x303, want it to stop when the image is 1/4 (78px roughly).
    – PruitIgoe
    Nov 19 at 12:05













up vote
1
down vote



accepted







up vote
1
down vote



accepted






I would not say it's ideal but hope below code will help you.



<style>
img.image{
visibility:hidden;
width:100%;
height:auto;
}
.imageContainer{
background-image:url("backgroundImage.webp");
background-size:cover;
background-position:50%;
background-repeat:no-repeat;
width:25%;
display:inline-block;
}
</style>

<div class="imageContainer"><img class="image" src="backgroundImage.webp" alt="sample image" /> </div>


If the image's served is a dynamic one then.Slight change will be the way the image is served.






share|improve this answer














I would not say it's ideal but hope below code will help you.



<style>
img.image{
visibility:hidden;
width:100%;
height:auto;
}
.imageContainer{
background-image:url("backgroundImage.webp");
background-size:cover;
background-position:50%;
background-repeat:no-repeat;
width:25%;
display:inline-block;
}
</style>

<div class="imageContainer"><img class="image" src="backgroundImage.webp" alt="sample image" /> </div>


If the image's served is a dynamic one then.Slight change will be the way the image is served.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 at 12:08

























answered Nov 19 at 12:01









vssadineni

340110




340110












  • ha, that's exactly what I did (kind of). Just trying to get it to stop resizing when ti reaches a certain width (original img is 303x303, want it to stop when the image is 1/4 (78px roughly).
    – PruitIgoe
    Nov 19 at 12:05


















  • ha, that's exactly what I did (kind of). Just trying to get it to stop resizing when ti reaches a certain width (original img is 303x303, want it to stop when the image is 1/4 (78px roughly).
    – PruitIgoe
    Nov 19 at 12:05
















ha, that's exactly what I did (kind of). Just trying to get it to stop resizing when ti reaches a certain width (original img is 303x303, want it to stop when the image is 1/4 (78px roughly).
– PruitIgoe
Nov 19 at 12:05




ha, that's exactly what I did (kind of). Just trying to get it to stop resizing when ti reaches a certain width (original img is 303x303, want it to stop when the image is 1/4 (78px roughly).
– PruitIgoe
Nov 19 at 12:05


















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%2f53374007%2fcss-fit-image-with-w-h-in-div-with-width%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

Ottavio Pratesi

Tricia Helfer

15 giugno