Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS scale height to match width - possibly with a formfactor

I have implemented a GoogleMapsV3 map in a twitterBootstrap basic responsive design site.

But my question is quite simple: i have:

<div id="map"></map> 

and

#map{ width: 100%; height: 200px } 

I'd like to be able to change the height to a form factor. Like in this "in my dreams CSS"

#map { width: 100%; height: width * 1.72 } 

I have tried leaving out height, setting to auto, and all sorts of persentages - but only to make the div collapse on me always.

I have no problem writing a js-solution, but hope for a simple cleancut CSS solution, possible CSS3

If not possible, what would be the optimal way to js me out of this?? (timers, events...or the like)

like image 542
Steen Avatar asked Jun 28 '12 10:06

Steen


People also ask

How do you scale proportionally in CSS?

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. And that's it!

How do you make width and height responsive?

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. In the demo, the blue box at the top of the page is a single div .

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

Here it is. Pure CSS. You do need one extra 'container' element.

The fiddle

(tinkerbin, actually): http://tinkerbin.com/rQ71nWDT (Tinkerbin is dead.)

The solution.

Note I'm using an 100% throughout the example. You can use whichever percentage you'd like.

Since height percentages are relative to the height of the parent element, we can't rely on it. We must rely on something else. Luckily padding is relative to the width - whether it's horizontal or vertical padding. In padding-xyz: 100%, 100% equals 100% of the box's width.

Unfortunately, padding is just that, padding. The content-box's height is 0. No problem!

Stick an absolutely positioned element, give it 100% width, 100% height and use it as your actual content box. The 100% height works because percentage heights on absolutely positioned elements are relative to the padding-box of the box their relatively positioned to.

HTML:

<div id="map_container">   <div id="map">   </div> </div>    

CSS:

#map_container {   position: relative;   width: 100%;   padding-bottom: 100%; }  #map {   position: absolute;   width: 100%;   height: 100%; } 
like image 136
João Paulo Macedo Avatar answered Oct 16 '22 19:10

João Paulo Macedo


You could try using vw for height. https://developer.mozilla.org/en/docs/Web/CSS/length

Something like

div#map {      width: 100%;      height: 60vw; } 

This would set the width of the div to 60% of the viewport width. You will probably need to use calc to adjust to take padding into account …

like image 36
Pit Avatar answered Oct 16 '22 20:10

Pit