Set height as a ratio of width with only css - html

I want to achieve the following for an <img> element in HTML, using only CSS:
width: calc(100% - 20px)
height: calc(width * 0.5625) /*16:9 aspect ratio*/
There are similair examples all over the internet regarding <div> elements and their background. But in the case of <img> elements, changing the padding does not work
Similair example: Maintain the aspect ratio of a div with CSS
Edit, using jQuery one can achieve the above with:
$(".myImage/s").outerHeight($(".myImage/s").outerWidth() * 0.5625);

CSS has a built-in property called aspect-ratio just assign it to the element after height or width has been defined. CSS-tricks has an example and I made a code snippet below.
div{
width:50vw;
border: 2px solid black;
border-radius: 15px;
margin:5px;
aspect-ratio: 16/9;
}
<div>
</div>

Use viewport-width (vw) for defining width in the height property:
width: calc(100% - 20px)
height: calc((100vw - 20px) * 0.5625) /*16:9 aspect ratio*/
The viewport is the visible area of the web page.
Its full size is 100vw * 100vh, where vw and wh are the viewports size units.
Thus one "vw" is equal to 1% of the web page's currently visible width.
More can be found at: Viewport units: vw, vh, vmin, vmax

the proposed solutions so far use vw which only work if you want the image to fill the entire page.
but there is a much cleaner solution that keep the image aspect ratio 9/16 in all size containers.
HTML:
...
<div class="image image-9-16"> <!-- replace image and image-9-16 with any name you like -->
<img src="..." />
</div>
...
CSS:
.image {
position: relative;
display: block;
width: calc(100% - 20px);
max-width: calc(100% - 20px);
height: auto;
max-height: 100%;
padding: 0;
margin: 0;
overflow: hidden;
}
.image::before {
display: block;
content: "";
}
.image, .image img {
position: absolute;
top: 0;
bottom: 0;
left: 0;
width: 100%;
height: 100%;
border: 0;
}
.image-9-16::before {
padding-top: 56.25%;
}
and that'll work no matter the width of the container holding the image with no need to place the image as a background-image... and now you can even add more aspect ratios if you want...
.image-1-1::before {
padding-top: 100%;
}
.image-3-4::before {
padding-top: 75%;
}
.image-9-21::before {
padding-top: 42.857143%;
}
...

you can use vw (viewport width) to do that:
width: calc(100vw - 20px);
height: calc((100vw - 20px) * 0.5625); /*16:9 aspect ratio*/
You can also use the padding-bottom method if you place the image as a
background for the div.
https://jsfiddle.net/m11L9kjb/1/

This worked for me using the actual ratio… then I needed a max dimension, so make sure you set them before setting the height:
position: relative;
margin: 0 auto;
width: 100vw;
max-width: 1080px;
max-height: 1920px;
height: calc(100vw * (16/9));

Related

how to allow image width to exceed the width of the div that it is in

I have a div that is 1140px wide and am image sitting within it that I need to fit the screen size, Is it possible to make the image width size larger than the div width that it is sitting in?
<div className="row" style="width: 1140px; margin-left: auto; margin-right: auto">
<img className="aimg" src="~/images/background.gif" style="height: 325px" />
</div>
CSS as follows:
.aimg {
padding: 0;
display: block;
margin: 0 auto;
height: auto;
width: 100%;
}
As it stands, the div width is limiting the image from fitting the screen width
set position: absolute; width: 100vw; height: 100vh; to you img styles and it should work :)
"vw" stands for viewWidth and "vh", accordingly, for viewHeight.
https://www.w3schools.com/cssref/css_units.asp
Below codepen is based on:
https://css-tricks.com/full-width-containers-limited-width-parents/
Codepen itself:
https://codepen.io/anon/pen/vzawqv
you need css classes for container and image, the following example suppose to the div width is fixed width and set with 480px.
.div_image {
max-height:480px;
height: 480px;
}
.div_image img{
height: 100%;
width: 100%;
}

How to use CSS to set height of image the same as width

In my project, there are some images. My code looks like:
<div className="col-sm-6">
<img src="xxx">
</div>
<div className="col-sm-6">
<img src="xxx">
</div>
The width of img is 100% of div, which is 50% of the whole screen. If I resize the browser, the width of image is changed. In this case, how to keep the height is still the same as width?
It depends on the aspect ratio of the image.
If you want to stretch the image to a square: since the container <div> is 50% of the whole screen. You could've written it as width: 50vw; (50% of the viewport width). The same for your image: width: 50vw; and to keep height the same as the width.:
.col-sm-6 img {
width: 50vw;
height: 50vw;
}
If the image is a square, just adjust width. Height will automatically adapt. Since you must be using Bootstrap (guessing from the class name col-sm-6).
If the image is always panoramic:
.container {
width: 50vw;
height: 50vw;
border: 1px solid lime;
overflow: hidden;
}
.container img {
height: 100%;
min-width: 100%;
}
<div class="container">
<img src="http://www.w3schools.com/css/img_lights.jpg">
</div>
Same logic above for always vertical images.
You can just set the width of the wrapper to equal width and height for this and 100% width and height for the img.
Another Option
You can also use the fact that padding is always calculated based on the width in CSS-
Position the img absolutely relative to its wrapper
Give the same value for padding-top and width (50vw each) and set height to zero for the wrapper.
Give width: 100% for the img
See demo below:
body {
margin: 0;
}
.wrapper {
width: 50vw;
height:0;
padding-top: 50vw;
position: relative;
}
.wrapper img {
width:100%;
height: 100%;
top:0;
left: 0;
position: absolute;
vertical-align:top;
}
<div class="wrapper">
<img src="http://placehold.it/250x250">
</div>
Note that your images may stretch out if it is not a square image - you can opt according to your design to drop either of width: 100% or height: 100% so that the stretching won't happen. (or opt to use a square image of course!)
See demo below:
body {
margin: 0;
}
.wrapper {
width: 50vw;
height:0;
padding-top: 50vw;
position: relative;
}
.wrapper img {
width:100%;
height: 100%;
top:0;
left: 0;
position: absolute;
vertical-align: top;
}
<div class="wrapper">
<img src="http://placehold.it/250x100">
</div>
Usually if the width auto then height fix hardware and vice versa, if you want auto height to auto-height of the current div

Ionic - Multiple elements fill 100% height of parent <div>

I am building an app in Ionic where I am trying to locate four row elements and a button inside a div. The div has an height: 100% that covers the whole screen, and the content has to stretch to its full-height.
I have tried the following solution, but apparently it is not working:
ion-content {
position: relative;
height: auto;
width: 100%;
}
.boxes-container {
position: absolute;
height: 100%;
width: 100%;
}
This is the complete code. Do you have an idea of what is a possible way to solve it?
Thanks in advance for your replies!
You can use a combination of the calc function and viewport units to achieve this layout.
height: 100vh will give an element a height equal to that of the viewport.
You have a header element that is 44px in height. Each row has a vertical margin. You can deduct these from 100vh using the calc function:
.boxes-container {
height: calc(100vh - 44px - 50px);
}
This will give your element a height equal to that of the viewport minus the height of the header element.
You then need to give your four rows and the button a height of 20% so that they occupy all of the available vertical space in the container.
.row {
...
height: 20%;
}
.button {
height: 20%;
}
Updated live demo
calc() is available in all major browsers and IE>8 (caniuse.com)
An alternative would be to give both .scroll and .boxes-container a height of 100% and .row a height of 20%:
.scroll {
height: 100%;
}
.boxes-container {
height: 100%;
}
.row {
...
height: 20%;
}
Updated live demo

Responsive video iframes (keeping aspect ratio) with only CSS?

I usually use a similar solution to this one. Something like:
.wrapper {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%;
}
.wrapper iframe {
position:absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
}
But this time I have no access to the HTML or JavaScript code so I can't use a wrapper to prevent the height:0.
Is there a way to make an iframe responsive (and to keep the ratio) with only CSS?
Tried this (works with the iframe but not with its content):
iframe {
width: 100%;
background: red;
height: 0;
padding-bottom: 33%;
}
fiddle
Any thoughts? No need to support old browsers so even a CSS3 solution would be great.
Here is a Fiddle for a solution, that is based on a CSS2 secret: https://jsfiddle.net/59f9uc5e/2/
<div class="aspect-ratio">
<iframe src="" width="550" height="275" frameborder="0"></iframe>
</div>
<style>
/* This element defines the size the iframe will take.
In this example we want to have a ratio of 25:14 */
.aspect-ratio {
position: relative;
width: 100%;
height: 0;
padding-bottom: 56.25%; /* The height of the item will now be 56.25% of the width. */
}
/* Adjust the iframe so it's rendered in the outer-width and outer-height of it's parent */
.aspect-ratio iframe {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
</style>
It is explained by how percentage-values for padding are handled:
The percentage is calculated with respect to the width of the generated box's containing block, even for 'padding-top' and 'padding-bottom'.
https://www.w3.org/TR/CSS2/box.html#padding-properties
Use the new CSS viewport units vw and vh (viewport width / viewport height)
FIDDLE
iframe {
width: 100vw;
height: 56.25vw; /* 100/56.25 = 560/315 = 1.778 */
background:red;
}
Browser support is also good: IE9+ (caniuse)
Calc function makes it much more readable:
.iframe-video {
width: 755px;
height: 424px;
max-width: 100%;
max-height: calc((100vw - 40px) / (16/9));
}
width and height is size for desktop and also fallback to ancient browsers
40px is margin (20 px between iframe border and viewport border on both sides)
16/9 is ratio of video (if you have edge-to-edge player)
With css aspect-ratio it's easy.
iframe {
height: auto;
width: 100%;
aspect-ratio: 16 / 9;
}
<div>
<div style="position:relative;padding-top:56.25%;">
<iframe src="https://www.youtube.com/embed/nckseQJ1Nlg" frameborder="0" allowfullscreen
style="position:absolute;top:0;left:0;width:100%;height:100%;"></iframe>
</div>
</div>
Please add this styling to the web url content you are trying to load. It will keep the 16:9 aspect ratio.
This is kind of hackish however you can use images to preserve the aspect ratio of a video. For example I went to paint and saved an image of 1280 x 720 resolution to use for a 16:9 aspect ratio (in here I will just grab a blank image off the internet somewhere).
This works because if you change the width of an image while leaving height auto, vise-versa the image will automatically scale - keeping proportions.
img {
display: block;
height: auto;
width: 100%;
}
iframe {
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
.wrapper {
float: left;
position: relative;
}
<div class="wrapper">
<img src="http://www.solidbackgrounds.com/images/1280x720/1280x720-ghost-white-solid-color-background.jpg" alt=""/>
<iframe width="560" height="315" src="https://www.youtube.com/embed/HkMNOlYcpHg" frameborder="0" allowfullscreen></iframe>
</div>
I had the same issue and this worked for me:
.wrapper iframe{
width: 100%; /* This Forces video to 100% of parent's width */
height:unset; /* This Overwrites the height attribute of Youtube's embed code */
aspect-ratio: 16 / 9; /* This adjusts video height to keep the desired aspect ratio*/
}

CSS Image Resize by Height

I am aware of the image resizing technic of changing image proportions based on width:
img{
max-width: 100%;
height: auto;
}
I need to do the same thing only based on the height of the parent, not the width. I have tried the following with no effect:
img{
width: auto;
max-height: 100%;
}
I hope I explaining this well enough.
Please help :)
max-height will only restrict the height to be less than the given value.
If you want it to be the same as its parent.. give it as height: 100%
hence this should work:
CSS:
img{
height: 100%; // changed max-height to height here
width: auto; // this is optional
}
You cannot effectively base it on the height using standard css techniques, but you can make the height relate directly to the width of the image for a more flexible layout. Try this:
img {
position: relative;
width: 50%; /* desired width */
}
img:before{
content: "";
display: block;
padding-top: 100%; /* initial ratio of 1:1*/
}
i have tried this and it worked .
<div class="parent">
<img src="http://searchengineland.com/figz/wp-content/seloads/2014/07/google-logo-sign-1920-600x337.jpg" alt="..." />
</div>
.parent {
width: 100%;
height: 180px;
overflow: hidden;
}
see it here
https://jsfiddle.net/shuhad/vaduvrno/