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
This answer is much the same as others except I prefer not to use as many class names. But that's just personal preference. You could argue that using class names on each div is more transparent as declares up front the purpose of the nested divs.
<div id="MyDiv" class="proportional">
<div>
<div>
<p>Content</p>
</div>
</div>
</div>
Here's the generic CSS:
.proportional { position:relative; }
.proportional > div > div { position:absolute; top:0px; bottom:0px; left:0px; right:0px; }
Then target the first inner div to set width and height (padding-top):
#MyDiv > div { width:200px; padding-top:50%; }
You could assign both the width and height of the element using either vw
or vh
. For example:
#anyElement {
width: 20vh;
height: 20vh;
}
This would set both the width and height to 20% of the browser's current height, retaining the aspect ratio. However, this only works if you want to scale proportionate to the viewport dimensions.
If you want vertical sizing proportional to a width set in pixels on an enclosing div, I believe you need an extra element, like so:
#html
<div class="ptest">
<div class="ptest-wrap">
<div class="ptest-inner">
Put content here
</div>
</div>
</div>
#css
.ptest {
width: 200px;
position: relative;
}
.ptest-wrap {
padding-top: 60%;
}
.ptest-inner {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #333;
}
Here's the 2 div solution that doesn't work. Note the 60% vertical padding is proportional to the window width, not the div.ptest
width:
http://jsfiddle.net/d85FM/
Here's the example with the code above, which does work:
http://jsfiddle.net/mmq29/
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;
}
You can use View Width for the "width" and again half of the View Width for the "height". In this way you're guaranteed the correct ratio regardless of the viewport size.
<div class="ss"></div>
.ss
{
width: 30vw;
height: 15vw;
}
Fiddle
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