Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I scale a div's height proportionally to its width using CSS?

Tags:

html

css

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

like image 298
spiderman Avatar asked Jan 17 '12 12:01

spiderman


People also ask

How do you set height to proportional to width in CSS?

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.

How do you make width and height responsive?

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.

Which css3 property can be used to resize the width and height of an element by 5 times and 6 times respectively?

The scale() method increases or decreases the size of an element.

How do you scale in CSS?

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.


2 Answers

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

like image 158
Stefan H Singer Avatar answered Sep 20 '22 05:09

Stefan H Singer


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; } 
like image 44
jdavis Avatar answered Sep 20 '22 05:09

jdavis