So I want an img
to be displayed
Also, the image's original size is not known.
I've tried quite a few different options for this, including a flexbox one (to get the vertical center), but nothing seems to tick all the boxes.
Ideally I'd like this to be an all CSS solution of course, but I have been looking into some JS as well.
Thanks
To make an image responsive, you need to give a new value to its width property. Then the height of the image will adjust itself automatically. The important thing to know is that you should always use relative units for the width property like percentage, rather than absolute ones like pixels.
In the HTML, put the player <iframe> in a <div> container. In the CSS for the <div>, add a percentage value for padding-bottom and set the position to relative, this will maintain the aspect ratio of the container. The value of the padding determines the aspect ratio. ie 56.25% = 16:9.
Make The Container Div Full Screen This is a two-step process. Add a couple of CSS properties to the HTML and body selectors. Setting the margin to 0 will get rid of any white space around the browser window and set the height to 100%.
To center it, you can use the technique shown here: Absolute centering.
To make it as big as possible, give it max-width
and max-height
of 100%
.
To maintain the aspect ratio (even when the width is specifically set like in the snippet below), use object-fit
as explained here.
.className {
max-width: 100%;
max-height: 100%;
bottom: 0;
left: 0;
margin: auto;
overflow: auto;
position: fixed;
right: 0;
top: 0;
-o-object-fit: contain;
object-fit: contain;
}
<img src="https://i.imgur.com/HmezgW6.png" class="className" />
<!-- Slider to control the image width, only to make demo clearer !-->
<input type="range" min="10" max="2000" value="276" step="10" oninput="document.querySelector('img').style.width = (this.value +'px')" style="width: 90%; position: absolute; z-index: 2;" >
You could use a div
with a background image instead and this CSS3 property:
background-size: contain
You can check out an example on:
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Scaling_background_images#contain
To quote Mozilla:
The contain value specifies that regardless of the size of the containing box, the background image should be scaled so that each side is as large as possible while not exceeding the length of the corresponding side of the container.
However, keep in mind that your image will be upscaled if the div
is larger than your original image.
I have come to point out the answer nobody seems to see here. You can fullfill all requests you have made with pure CSS and it's very simple. Just use Media Queries. Media queries can check the orientation of the user's screen, or viewport. Then you can style your images depending on the orientation.
Just set your default CSS on your images like so:
img {
width:auto;
height:auto;
max-width:100%;
max-height:100%;
}
Then use some media queries to check your orientation and that's it!
@media (orientation: landscape) { img { height:100%; } }
@media (orientation: portrait) { img { width:100%; } }
You will always get an image that scales to fit the screen, never loses aspect ratio, never scales larger than the screen, never clips or overflows.
To learn more about these media queries, you can read MDN's specs.
To center your image horizontally and vertically, just use the flex box model. Use a parent div
set to 100% width and height, like so:
div.parent {
display:flex;
position:fixed;
left:0px;
top:0px;
width:100%;
height:100%;
justify-content:center;
align-items:center;
}
With the parent div's display
set to flex
, the element is now ready to use the flex box model. The justify-content
property sets the horizontal alignment of the flex items. The align-items
property sets the vertical alignment of the flex items.
I too had wanted these exact requirements and had scoured the web for a pure CSS solution. Since none of the answers here fulfilled all of your requirements, either with workarounds or settling upon sacrificing a requirement or two, this solution really is the most straightforward for your goals; as it fulfills all of your requirements with pure CSS.
EDIT: The accepted answer will only appear to work if your images are large. Try using small images and you will see that they can never be larger than their original size.
After a lot of trial and error I found this solved my problems. This is used to display photos on TVs via a browser.
The only thing to watch for are really wide images. They do stretch to fill, but not by much, standard camera photos are not altered.
Give it a try :)
*only tested in chrome so far
HTML:
<div class="frame">
<img src="image.jpg"/>
</div>
CSS:
.frame {
border: 1px solid red;
min-height: 98%;
max-height: 98%;
min-width: 99%;
max-width: 99%;
text-align: center;
margin: auto;
position: absolute;
}
img {
border: 1px solid blue;
min-height: 98%;
max-width: 99%;
max-height: 98%;
width: auto;
height: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With