I have a simple website / html-page that should show one image and some buttons. But the image size should follow the following rules:
the shorter side of the images should not be shown as more than 600 pixel
the image height should be max 80% of the window to allow space for UI without need to scroll
the image width should be up to 100% of the window
the image should not be shown in an increased size
the image's aspect ratio should not change
Regarding the rule with the shorter size: With shorter size, I mean either the height or the width of the image in pixel, depending which is smaller in number. So if an image is 2000x1200 it should be shown in 1000x600, so that the shorter size is 600. But also an image with 1200x88888 would be shown to 600x44444 pixel.
Here is what I tried. I resized all the images, so that their shorter size is 600 pixel or shorter. But the images are still diverse, an image could be 3000x600 but also 100x3000 or 100x100.
And in the code I tried:
<div style="max-height: 80%">
<img style="margin: 0px auto; display: block; height: 100%;" src="http://www.dummyimage.com/800x200/000/fff&text=image">
</div>
but this does not work for me, as the image is increased in size if the 80% window height is larger than the image.
Here a fiddle with 30%, instead of 80% to make it better visible:http://jsfiddle.net/ErNeT/2723/
It is probably impossible to determine if an image is portrait or landscape without JavaScript (to determine shorter side). So:
$(window).on("load", function() {
$("img").each(function() {
if (this.naturalWidth >= this.naturalHeight) {
$(this).addClass("landscape");
} else {
$(this).addClass("portrait");
}
});
});
body {
margin: 0;
}
.demo {
position: relative;
height: 100vh;
background-color: peachpuff;
}
.demo:nth-of-type(even) {
background-color: papayawhip;
}
.demo::before {
position: absolute;
content: attr(title);
}
/* style used on page load */
img {
max-width: 100vw;
max-height: 80vh;
}
/* classes added by JavaScript */
img.landscape {
max-width: 100vw;
max-height: 600px;
}
img.portrait {
max-width: 600px;
max-height: 80vh;
}
/* 600px is greater than 80vh for screen shorter then 750px so... */
#media screen and (max-height: 750px) {
img.landscape {
max-height: 80vh;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="demo" title="Shorter side test (landscape)">
<img src="http://www.dummyimage.com/1000x700/ccc/000"></div>
<div class="demo" title="Shorter side test (portrait)">
<img src="http://www.dummyimage.com/700x1000/ccc/000"></div>
<div class="demo" title="Viewport width and height test">
<img src="http://www.dummyimage.com/2400x2400/ccc/000"></div>
<div class="demo" title="No stretch test">
<img src="http://www.dummyimage.com/200x200/ccc/000"></div>
take look here https://www.w3schools.com/howto/howto_css_image_responsive.asp for a responsive image with just css or use an picture meta tag, in that way you can see this too https://css-tricks.com/responsive-images-css/
<picture>
<source srcset="extralarge.jpg" media="(min-width: 1000px)">
<source srcset="large.jpg" media="(min-width: 800px)">
<img srcset="medium.jpg" alt="…">
</picture>
Related
I've been looking up online for hours on how to make the image responsive on mobile..This is what it looks like on computer:
When I try 'inspect element' and view the images, it comes out like this:
As shown, the images are big squished(?) where width is being decreased.
I'm using media query to customize the width and height.
HTML:
<div id = "projects">
<div id = "we-cycle">
<img id = "we-cycle-image" src = "./photos/we-cycle.png" width = "100%" height = "auto">
</div>
CSS:
#projects {
width: 80%;
margin: 300px auto;
margin-left: -30px;
padding: 20px;
}
#we-cycle-image {
position: relative;
max-width: 80%;
height: auto;
}
#media screen and (max-width: 500px) {
#we-cycle-image {
width: 100%;
height: auto;
}
}
yeah so the images are being squished because their aspect ratio is 1:1 or square but in small screens theres not enough space for them to be in a square form so the browser is changing the aspect ratio to something that would fit even though it doesn't look good
quick fix: assuming you already have the images in a div with a display of flex, add the following style:
flex-wrap: wrap;
So I searched up how to make a video responsive in html with css but it doesn't work. please help
this is my HTML
<div class="container">
<video width="540px" height="320px" controls src="Videos/Video.mp4"></video>
</div>
and this is css
.container {
width: 100%;
}
video {
height: auto !important;
width: 100% !important;
}
I would erase the width and height attributes from the video tag and (if the video isn't larger than specified by those attribute values) introduce a max-width setting in the CSS rule:
<div class="container">
<video controls src="Videos/Video.mp4"></video>
</div>
.container {
width: 100%;
}
video {
height: auto;
width: 100%;
max-width: 540px;
}
This will make the video either 540px wide if the container is >= than 540px, and make it full width when the container is less than 540px wide. Height will be auto according to the proportions in both cases.
In any case, if your video is larger than specified, you should use the original size width as the value for the max-width in the CSS. That way the CSS will allow a size up to original size (but not larger, which would cause bad quality)
I'm developping a page where the user can upload some images in my website.
The images the user can upload should have a specific size let's say : 800 x 600 px.
When the user uploads a different size for example : 900 x 500. This image should be rescaled (not proportionally) to 800 x 600.
Now I need the page to be responsive. Meaning that when the whole page is resized, the image region is should be resized proportionally this time.
For example, if the page is 50% the original size, the image region should be 400 x 300;
I have the following html
<div class="ImageRegion">
<img class="Image" src="whatever"/>
</div>
Can this be achieved using CSS only or should I use Javascript please ?
Cheers,
If I understood correctly, you probably can achieve want you want with something like that:
<img src="yourimage.png" class="whatever" />
And in CSS:
img.whatever {
display: inline-block;
max-width: 800px;
width: 100%
}
/* For width smaller than 400px: */
.ImageRegion {
background-repeat: no-repeat;
background-image: url('http://placehold.it/400x300');
width: 400px;
height: 300px;
}
/* For width 400px and larger: */
#media only screen and (min-width: 400px) {
.ImageRegion {
background-repeat: no-repeat;
background-image: url('http://placehold.it/800x600');
width: 800px;
height: 600px;
}
}
<h3>Resize the browser width and the background image will change at <mark>400px</mark>.</h3>
<div class="ImageRegion"></div>
I am using the same image as a background image for a div on one page and as an image element on another page. They take up the same space on both pages, same width and height, but the image is not positioned the same. This is the background image html:
<div class="frontpage-bg-image-wrapper">
<div class="header-bg-image frontpage-header-hero"></div>
<div class="bg-overlay overlay"></div>
</div>
And this is its css:
.frontpage-header-image-div {
height: 100%;
.frontpage-bg-image-wrapper {
position: relative;
width: 100%;
min-height: 635px;
background-size: cover;
background: url('/wp-content/themes/sfk/assets/images/sfk-bg.png') no-repeat;
}
}
And on the other page I have a an image element:
<div class="hero-image-wrapper">
<img src="<?php echo get_template_directory_uri(); ?>/assets/images/sfk-bg.png">
<div class="overlay"></div>
</div>
And its css:
.hero-image-wrapper img{
width: auto;
height: 100%;
}
But there is the difference in the positioning of the image, I have tried with object-fit: cover, but it didn't help. This is the background:
And this is the image element:
How can I fix that?
I think your picture is smaller than container (specially in 'height');
background-size:cover will cut external picture to fit the container, however the 'img' tag will not.
First way: set the container size as same ratio as the picture ([container width] : [container height] = [image width] : [image height])
Second way : try the img tag's css with: 100% , height:auto
From what I can tell cover plus min-height is cropping your image a little bit, probably when it's stretched beyond the native resolution of the image.
I would do what they do to create responsive video, add padding-bottom as a percentage that is equal to the video's aspect ratio. If your image is 400x300 then you'd add 75% padding,
300 / 400 = 0.75 * 100 = 75%
Doing this allows the element to fill the width of it's container with the same proportions as your image.
img {
display: block;
max-width: 100%;
height: auto;
}
.container {
margin: 3rem auto;
width: 70%;
}
.bg {
position: relative;
background-image: url( 'http://lorempixel.com/800/400/city/4/' );
background-size: cover;
padding-bottom: 50%;
}
#media ( min-width: 1120px ) {
.bg {
max-width: 800px;
padding-bottom: 400px;
}
}
<div class="container">
<img src="http://lorempixel.com/800/400/city/4/">
<div class="bg"></div>
</div>
For the img element we've made it responsive. Most CSS frameworks use the three properties used on img for their responsive image class. Note that the image will not resize beyond it's native resolution.
For the div to replicate the responsive styles of the img we needed to use a media query to prevent the image from expanding beyond it's native resolution along with updating the bottom padding. If we don't change the padding bottom when we limit the images width then you end up with a div that takes up a lot more space than the background image does (creating a lot of white space below it).
Yo could try setting background-size to 'contain'
I have an image that's 1920 pixel wide. I want it to display centered and cropped in the browser window when it's width is greater than 1024 pixels. When the browser width is less than 1024, the image should be centered and cropped to 1024 pixels and then resized to the browser width.
I can't figure out how to do this, and haven't found any solutions on the internet yet.
Can anyone tell me how to do this, or point me at examples?
I am not exactly sure what are you trying to do, BUT I think what are you looking for is:
max-width
http://www.w3schools.com/cssref/pr_dim_max-width.asp
You can use object-fit property:
.image-wrapper {
width: 100%;
height: 100%;
overflow: hidden;
> img {
width: 100%;
height: 100%;
object-fit: cover;
object-position: 50% 50%;
}
}
HTML
<div class="image-wrapper">
<img src="" />
</div>
What you are looking for can be achieved using media queries. Following is just an example. Hope you can proceed with that.
#media screen and (min-width: 480px) {
body {
background-color: lightgreen;
}
}
Here background color will be changed when you resize the browser and browser width is less than 480px.