Resize image keeping both sizes on ratio
-
I have a photo blog on the very final stages and this is the last problem I’m struggling with. The layout has fixed menu bar on the right and the content flows on the right. When photo is opened from gallery page to it’s own, it is supposed to be resized so that either side is not over (for example) 70% of the free space. But not just that. It should also stay in horizontal and vertical center of the right content div. At this point everything works except when the image is a portrait it goes over the viewport making the page scrollable.
Is this possible to achieve with only CSS to please the few percent that has JS not activated? If not, well thats not a deal breaker.
The HTML:
<div id="wrapper"> <div id="left_column"> </div> <div id="right_column_post"> <div id="post_container"> <img src="img.jpg" width="750" height="1000"/> </div> </div> </div>
CSS:
#right_column_post{ height: 100%; width:auto; margin-left:240px; position: relative; } #post_container { width:100%; height:100%; display: block; vertical-align: middle; text-align:center; } #post_container:before { content: ''; display: inline-block; height: 100%; vertical-align: middle; margin-right: -0.25em; /* Adjusts for spacing */ } #post_container img{ max-width:70%; height:auto; display: inline-block; vertical-align: middle; margin:auto; position:relative; }
This is close: https://jsfiddle.net/jcAGj/ but it’ll break the layout if implemented as is.
- The topic ‘Resize image keeping both sizes on ratio’ is closed to new replies.