Image Stretching in responsive design - html

I am having an image in different size. All having huge size images. I need to show the image without any stretch inside a container. That container had some height. So I have show the image to fit inside the container without any stretch. I need to use image in html not as background image. Here is my code
.image-container {
height: 420px;
width: 100%;
}
#media (max-width: 480px) {
height: 310px;
}
#media (max-width: 375px) {
height: 275px;
}
#media (max-width: 320px) {
height: 240px;
}
.image-container img {
max-height: 100%;
object-fit: contain;
max-width: 100%;
display: block;
margin: 0 auto;
}
<div class="image-container">
<img src="http://media.gettyimages.com/photos/under-the-tree-picture-id480585809" />
</div>
When I use above code image is stretching. Is there any way to fix this? Please suggest any solution for this.
Thanks in Advance.

Always reset the margins and paddings of all elements first.
* {
margin: 0;
padding: 0;
}
If height or width is auto it will not stretch / mutate the original image.
.img-container {
width: 420px; // your width
}
.img-container img {
width: 100%;
height: auto;
display: block;
}
Remove the media queries. Your syntax is incorrect the correct syntax for using them is :
#media (max-width: 480px) {
.img-container {
// some changes in the class props
}
.another-class-change-for-480px-max-width-of-screen {
}
}

use max-width instead of width in your css to avoid stretching
.img-container img{ max-width:100%; height:auto;}

Latest solution for this is using object-fit for IMG.
Note: Not supported in IE11. Check support here.
Both images are same. Second image has object-fit: cover
.fitImage {
width: 200px;
height: 200px;
object-fit: cover;
}
<img src="https://i.picsum.photos/id/292/200/300.jpg" alt="">
<img src="https://i.picsum.photos/id/292/200/300.jpg" alt="" class="fitImage">
Using jQuery
$(document).ready(function() {
$(".fitImage").each(function(index) {
var imageUrl = $(this).find('img').prop("src");
$(this).css('background-image', "url(" + imageUrl + ")")
});
});
.fitImage {
height: 200px;
width: 200px;
background-size: cover;
background-position: center;
}
.fitImage img {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="fitImage">
<img src="https://i.picsum.photos/id/292/200/300.jpg" alt="">
</div>

Related

How to make container take full width in phone devices using Bootstrap

No matter how I try in my CSS file, the container can't take full width in phone device.
my CSS file:
#media (max-device-width: 1024px) {
.col-sm-2{
width: 100%;
}
.container {
margin: 0 auto;
width: 100%;
}
.col-sm-10 {
width: 100%;
}
}
HTML:
<div class="container">
<h1 >Profile</h1>
</div>
Any suggestion?
You should use container-fluid class instead of container, or if would prefer you can use the sm, lg and so on suffixes , like container-sm
See the docs
Try container fluid
<div class="container-fluid">
<h1 >Profile</h1>
</div>
or add !important to #media like this
#media (max-device-width: 1024px) {
.col-sm-2{
width: 100%;
}
.container {
margin: 0 auto;
width: 100% !important;
padding: 0 !important;
}
.col-sm-10 {
width: 100% !important;
}
}

Remove Extra Whitespace from Overflow Image CSS

I am trying to make the overflow of an image appear to be hidden upon a certain screen size, but I cannot find the solution of why this extra spacing appears. Here is the html an css I have so far. I am trying to do this because the image would appear too small.
Bootstrap solutions are also welcome too.
.aboutsplash img{
width: 100%;
height: auto;
margin: 0;
}
#media (max-width: 769px){
.aboutsplash img{
overflow: hidden;
width: auto;
max-height: 150px;
}
}
#media (max-width: 500px){
.aboutsplash img{
position: left;
margin: 0 0 0 -25%;
}
}
<div class="aboutsplash">
<img src="images\sunsetcrop2.jpg" alt="lbl sunset">
</div>
#media (max-width: 769px){
.aboutsplash {
overflow: hidden;
}
}
try this.

CSS: Make img fit completely in div

Working on a news site and once I get to mobile, I'd like it where the image fits inside the entire width of the div....whatever that might be. The images are originally 240px in width so we are talking about bumping them up. This is what I'm currently using:
.thumb { float: left; clear: both; width: 100%; height: auto; margin: 0; }
.thumb img { max-width: 100%; max-height: 100%; }
So on my iPhone, the pic it a little bit too small since the div is 320px and the img is 240. Of course, this would change for say, landscape at 480px for example. So it needs to be flexible. Any suggestions?
You might try the following, set the image width to 100%.
.thumb {
float: left;
clear: both;
width: 100%;
height: auto;
margin: 0;
}
.thumb img {
width: 100%;
}
<div class="thumb">
<img src="http://placehold.it/200x100">
</div>
#media screen and (max-width: 480px) //place whatever the resolution you are working here{
/*enter your code here for this resolution*/
}
to learn more about
media queries
This is very simple as like this.
img {
width:100%;
}
div{
float: left; clear: both; width: 100%; height: auto; margin: 0;
}
<div>
<img src="http://www.keenthemes.com/preview/conquer/assets/plugins/jcrop/demos/demo_files/image2.jpg" />
</div>

Trouble with positioning images evenly inside container

One of my questions about applying float to resizable images was answered by Gasim (thankyou) but I still have one more problem to fix.
I have two images which have this css code applied to them...
.image-container {
display: inline-block;
width: 100%;
background-size: cover;
background-position: center;
background-image: url(Images/Filler2.png);
height: 250px;
margin-right: auto;
margin-left: auto;
}
#media only screen and (min-width : 1200px) {
.image-container {
max-width: 500px;
}
}
.image-container2 {
display: inline-block;
width: 100%;
background-size: cover;
background-position: center;
background-image: url(Images/Filler3.png);
height: 250px;
margin-right: auto;
margin-left: auto;
}
#media only screen and (min-width : 1200px) {
.image-container2 {
max-width: 500px;
}
}
So now they're resizing correctly and aligned next to one another, I am unable to position them horizontally like my example below. I have tried the only way I know how to do this (margin-left: auto , margin-right:auto) but this didn't work. Also padding isn't working? Is there some part of this code which prevents these both from working and if so what do I need to fix it?
Any input if helpful thanks :)
Maybe this is not the best solution for responsive images, but I generally use media queries with a mobile first approach and it has been working out great. Basically, you set the boxes' widths to 100% by default and on larger screens you set the width that you want:
.image-container {
float: left;
width: 100%;
background-size: cover;
background-position: center center;
background-color: red; /* for testing */
height: 250px;
}
#media only screen and (min-width : 1224px) {
.image-container {
max-width: 500px;
}
}
Here is the result: http://codepen.io/gasim/pen/xbYQdd
Try to make the window size smaller and you will see the difference. It is much simpler to test, debug, and modify this code.
EDIT: Forgot to mention. If you can also use display: inline-block instead of floats.
EDIT 2: To add padding between containers you need to add margin to a specific value, not auto. For example:
.image-container {
/* rest of the values */
margin: 5px;
}
This will add 5px margin to left, right, top, and bottom of the image container. If you want to have center the image containers also, I would add them in another container:
<div class="container">
<div class="image-container" style="background-image: url(URL_HERE);"></div>
<div class="image-container" style="background-image: url(URL_HERE);"></div>
</div>
and add the styles:
#media only screen and (min-width: 1224px) {
.container {
width: 80%;
margin: 0 auto;
}
}
Also, please check out the way I added the background images, using styles. It is much cleaner than writing CSS for each background image. That way you can have one class image-container and just add as many background-images without relying on CSS.

How to size images in #media screen property?

I want to know how to size my images in #media screen and max-width:640.
My style.css:
#header-wrapper
{
position: relative;
padding: 7em 0 0;
background:url(images/fox-illustration.jpg) no-repeat center;
background-position:center;
width:auto;
height:768px;
}
My mobile.css:
#media screen and (max-width: 640px) {
header-wrapper {
width: 600px;
}
So the width is only applied to to the header-wrapper..
I also tried to give the header-wrapper in mobile.css another image
(which I sized in photoshop to mobile size).
But this didn't work either. I think because the image of the header-wrapper overwrite it in style.css?
background:url(images/fox-illustration.jpg);
background-size:600px 400px;
background-repeat:no-repeat;
Do this
First make sure your mobile.css is active.
If the second part of your code is at it is set now, you are probably missing an id tag in the css.
Try changing your
header-wrapper {
width: 600px;
}
to
#header-wrapper {
width: 600px;
}
Other than using fixed background size, you can use cover and follow the width of the #header-wrapper
#header-wrapper {
position: relative;
padding: 7em 0 0;
background:url(images/fox-illustration.jpg) no-repeat center;
background-size:cover;
width:auto;
height:768px;
}
#media screen and (max-width: 640px) {
#header-wrapper {
width: 600px;
}
}