Is it possible to implement the following logic usic HTML and CSS?
width: 100%; height: 30% of width;
What I want to implement: If I decrease the width, the height will also decrease proportionally.
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.
The aspect ratio of an image or video is the proportional relationship of the width to the height. You'll recognize it as two numbers separated by a colon in an x:y format. For instance, a 6×4 inch image has an aspect ratio of 3:2, whereas a 1920×1080 pixel video has an aspect ratio of 16:9.
For the height of a div to be responsive, it must be inside a parent element with a defined height to derive it's relative height from. If you set the height of the container holding the image and text box on the right, you can subsequently set the heights of its two children to be something like 75% and 25%.
This can be achieved by giving the element height:0
and padding-bottom:30%
.
In all browsers, when padding is specified in %, it's calculated relative to the parent element's width. This can be a very useful feature for Responsive Design.
JSFiddle Demo
In the demo, the blue box at the top of the page is a single div
. The height is responsive, and it can contain a variable amount of inline content without increasing the height. It tested fine in IE7/8/9/10, Firefox, Chrome, Safari, and Opera.
.content { width: 100%; height: 0; padding-bottom: 30%; } ... <div class="content"></div>
If there are any problems with using a 0-height element, the demo also has a green box that's implemented using a wrapper element and absolute position. Not sure if there are any advantages to this approach.
.wrapper { position: relative; width: 100%; padding-bottom: 30%; } .wrapper .content { position: absolute; width: 100%; height: 100%; } ... <div class="wrapper"> <div class="content"></div> </div>
This is now possible on modern browsers with vw
and vh
units:
width: 100vw; height: 30vw; /* 30% of width */
Browser support: http://caniuse.com/#feat=viewport-units
Yeah!
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