Igor's Blog

As I work away on my travelblog.ws project I encounter some interesting problems and creating a constant ratio DIV has been one of them. What I wanted to do was have photos that I uploaded, no matter whether they are landscape or portrait oriented to be displayed in a uniform manner, all with the same sizes, exactly how Facebook does it. This is my solution to that.

To demonstrate my code I have uploaded 3 photos, two portrait and one landscape...
constratiodiv_2.png


When displayed, the results look like this...
constratiodiv_1.jpg


Each of the photos is placed into a DIV with a structure like this...
 HTML
<div class="photo-4x3">
<div class="photo_clip">
<img src="photo.jpg" class="photo_img" width="320" height="240">
</div>
</div>


Note in this case I am using a 4:3 ratio div to clip the photo. In addition the photo is both vertically and horizontally centred within the clipping DIV.



Applying a bit of CSS gets the desired result...
 CSS
.photo-4x3 {
display: inline-block;
position: relative;
width: 320px;
}
.photo-4x3:after {
padding-top: 75%; /* 4:3 aspect */
display: block;
content: '';
}
.photo_clip {
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
background-color: #999;
overflow-x: hidden;
overflow-y: hidden;
text-align: center;
}
.photo_img {
width:100%;
height:auto;
top:50%;
transform: translateY(-50%);
-ms-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
left:0;
position:absolute;
}


The size of the resulting DIV is controlled by setting the width on the photo-4x3 class. The ratio is set by the padding-top value on the photo-4x3:after selector. For example 75% means 4:3 ratio (3 ÷ 4 × 100). Using the same calculation a 16:9 aspect ratio is a 56.25% value.

I've put a full example HTML file here.

-i

, , ,
About — I'm an enthusiastic software engineer and consultant interested in many fields including J2EE, programming, electronics, 3D printing, video games, wood working and gardening.
See my Resume for more information.
The views expressed in this blog are my own and not those of my employer.
comments powered by Disqus
My other posts you may like...
Programming, DIY, Games, Hacks, Tech and more.
Follow me on...
Current and Past Projects
See my Resume

Subscribe


RSS Feed

My Other Web Sites

Igor and Elise's Travels
Riverside Expressway Cam
300 George St Blogumentary

My Online Tools

UUID to OID Converter
Guru JSON-RPC Tester
Extrudifier Object Designer
Travel ┬ÁBlog

Recent Blog Posts

Showing whitespace characters in vi

Using Jackson to automatically convert Java POJOs to JSON as output of a Jersey service

Enable quick access to the keyboard and emoji viewer in macOS

How to tell if your site is served via CloudFlare

Googe Cloud Platform doesn't support CloudFlare's free edge certificates

Fixing the invalid private key error in the Google Cloud Platform App Engine app Settings page

Cleaning the sticky PlayStation controller analogue thumb sticks

Invoking interactive shell scripts non-interactively

Using variables as column numbers with Bash and Awk when processing text files

Debugging Google DFP ad delivery via the publisher console

Recent Galleries

Space Food - Chocolate Ice Cream with Chocolate Chips

Legeod Star Wars AT-DP kit

DIY spare parts computer build with a RAIDMAX Anura case

Fake 'Lepin' brand Lego packaging

Hardwood garden bench with clear resin void filler

Fixing a 3D printer extruder that stopped heating up

Easily increase disk space in a Lenovo Ideapad 100S 14" laptop with an M.2 SSD

Making a multi-piece 3D printed solder spool holder stand

DIY indoor apartment grow light wiring

Good Friday Electronics fun Easter Bunny LED PCB Kit IBEABU-01.0

Top Categories

Blogs I follow

Matt Moores Blog
Georgi's FlatPress Guide
Perplexing Permutations
The Security Sleuth

Friends

RAWS Parts Online
Alpha Dimensions Hosting
Kristensen Photography
Ilia Rogatchevski
Travelling Fairy

Blog Activity

Blog Activity
Follow me on... 
     
...or subscribe for updates!

Don't show this again