Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep div height relevant to aspect ratio

I'm working on building a mobile friendly site of our companies main website. The way it is designed is around 2x for retina. What I'm planning to do is set the main content around a maximum width of 640px, width set at 100%. I have a certain background image that fits nicely do that. But as the width of the div gets smaller, I need the height to adjust as well. Any ideas?

Here's the css:

*{margin:0;padding:0}h1,h2,h3,h4,h5,p,li,a,cite{font-size:14px;font-weight:normal}button,img{border:0}body{font-family:Arial, sans-serif;}

body {
  margin:0;
  background-color:#fff;
}

.top, .body {
  max-width:640px;
  width:100%;
  margin:0 auto;
}

.top {
  background: white url(images/top.jpg) no-repeat;
  background-size:auto;
  overflow:hidden;
  height:124px;
  max-height:124px;
}

.top ul {
  list-style:none;
  height:100%;
}

.top ul li {
  height:100%;
  float:left;
  display:block;
}
like image 674
agmcleod Avatar asked May 28 '12 15:05

agmcleod


People also ask

Does div maintain aspect ratio?

In the HTML, put the player <iframe> in a <div> container. In the CSS for the <div>, add a percentage value for padding-bottom and set the position to relative, this will maintain the aspect ratio of the container. The value of the padding determines the aspect ratio. ie 56.25% = 16:9.

How do you keep the same ratio aspect in CSS?

2022 solution - use the aspect-ratio CSS property. Note: When you only use the width property with 100% as value, this width will be calculated, so that it won't fill the horizontal space. When you want to preserve the 100% width, you have to add both width and height with each 100% set.

Which of the following CSS rule makes a image retain its aspect ratio?

The Simple Solution Using CSS By setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained.


2 Answers

I did find an answer to this. It adds a little bit of unsemantic markup, but works well. Can find it here: http://jsfiddle.net/AdQ3P/

The logic is in the padding-bottom. basically this needs to be (img_height / img_width) * 100.

Edit Here's the code, so not dependent on jsfiddle.

<div class="container">
  <div class="hero"></div>
</div>

.container {
  width:100%;
  max-width:500px;
}

.hero {
  width:100%;
  height:0;
  background-size:100%;
  background:url(http://img97.imageshack.us/img97/3410/photo2ue.jpg) no-repeat;
  padding-bottom:75%;
}

Also that was one messy desk i had lol.

like image 56
agmcleod Avatar answered Oct 06 '22 03:10

agmcleod


You can also use a little jQuery. I believe the advantage is that it is a semantically valid fix, so the next guy who edits your code might have an easier time understanding what's going on.

// Maintain aspect ratio of #my_div

// Set aspect ratio of #my_div
var aspect_ratio = 0.8;

// Store the jQuery object for future reference
var $my_div = jQuery("#my_div");

// Within your document ready function,
// Do an initial resize of #my_div
$(document).ready(function(){
  $my_div.height( $my_div.width() * aspect_ratio );
});

// Resize #my_div on browser resize
jQuery(window).resize(function() {
  $my_div.height( $my_div.width() * aspect_ratio );
});
like image 34
chrisfargen Avatar answered Oct 06 '22 02:10

chrisfargen