Can I set any variable in CSS like I want my div height to always be half of width my div scales with the screen size width for div is in percent
<div class="ss"></div>
CSS:
.ss { width:30%; height: half of the width; }
This 30% width scales with screen resolution
It's a good fact to note if you're doing CSS work. For proportional resizing purposes, it makes matters extremely simple: Define the width of an element as a percentage (eg: 100%) of the parent's width, then define the element's padding-top (or -bottom) as a percentage so that the height is the aspect ratio you need.
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 scale() method increases or decreases the size of an element.
scale() The scale() CSS function defines a transformation that resizes an element on the 2D plane. Because the amount of scaling is defined by a vector, it can resize the horizontal and vertical dimensions at different scales. Its result is a <transform-function> data type.
You can do it with the help of padding on a parent item, because relative padding (even height-wise) is based on the width of the parent element.
CSS:
.imageContainer { position: relative; width: 25%; padding-bottom: 25%; float: left; height: 0; } img { width: 100%; height: 100%; position: absolute; left: 0; }
This is based on this article: Proportional scaling of responsive boxes using just CSS
Another great way to accomplish this is to use a transparent image with a set aspect ratio. Then set the width of the image to 100% and the height to auto. That unfortunately will push down the original content of the container. So you need to wrap the original content in another div and position it absolutely to the top of the parent div.
<div class="parent"> <img class="aspect-ratio" src="images/aspect-ratio.png" /> <div class="content">Content</div> </div>
CSS
.parent { position: relative; } .aspect-ratio { width: 100%; height: auto; } .content { position: absolute; width: 100%; top: 0; left: 0; }
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