
Change the aspect ratio of an image using CSS
24/09/2021In this post I'll show you how to change the aspect ratio of an image or element using CSS, and explain why this is important.
Whatever theme I’m building, whether for WordPress, Drupal or bespoke, I’ve almost always needed to create lightboxes. When I use third party resources I like them to be lightweight, customisable and fast, so when I’m using jQuery I use Featherlight.
Featherlight is relatively simple to use but offers many great features. I’m going to show you how to make image and modal lightboxes.
Note: make sure to include the featherlight.js and featherlight.css on your page.
To create an image lightbox it is as simple as wrapping it within an <a> tag and using the data-featherlight attribute to point at the location of the image file.
<a href="#" data-featherlight="path/to/image.png"><img src="path/to/image.png" alt="an image to lightbox"></a>
Click the logo above to checkout the lightbox. All styling is Featherlight default and unmodified.
With Featherlight you aren’t just limited to image lightboxes. Because Featherlight acts on all elements using the data-featherlight attribute we can also use it to create modals.
<a href="#" data-featherlight="#modal-example">Show me!</a>
<div id="modal-example" class="modal">Now I can fill my div with lots of cool modal stuff</div>
Featherlight will look for the <div> using the id passed through data-featherlight. Show me!
Now I can fill my div with lots of cool modal stuff
To get this to work properly you will need to add a modal style to your css stylesheet. This can’t be set inline.
.modal {
display: none;
}
Featherlight is created as an object. By targetting the constructor $.featherlight we can change and extend the behaviour of Featherlight. I like to use this to add captions to my image lightbox.
In this example we want to modify .afterContent. To achieve this we use the following.
$.featherlight.prototype.afterContent = function () {
}
Next we are going to pass our captions. We are going to take this from the $currentTarget and, in this example, we will use the title attribute.
var caption = this.$currentTarget.find('img').attr('title');
Now that we have our caption we are going to make sure that there isn’t a caption currently set and then add a new one.
this.$instance.find('.caption').remove();
$('<div class="caption">').text(caption).appendTo(this.$instance.find('.featherlight-content'));
Your final code should look something like below.
$.featherlight.prototype.afterContent = function () {
var caption = this.$currentTarget.find('img').attr('title');
this.$instance.find('.caption').remove();
$('<div class="caption">').text(caption).appendTo(this.$instance.find('.featherlight-content'));
}
Click the image above to see an image lightbox with captions. To get the caption to display all you have to do is populate the title attribute of the image.
Note: for more Featherlight tricks, please read the usage docs.
In this post I'll show you how to change the aspect ratio of an image or element using CSS, and explain why this is important.
Here is how you can fix node incompatibility error, using "Node Sass does not yet support your current environment" as an example.
Need to make a directory in PHP? No Problem! mkdir has you covered... Need to recursively make directories in PHP? mkdir has you covered, too!
Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.
Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.
Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!
I love featherlight for the same reasons you said in your post. Simple but extendable and very lightweight (less than 15k for the minfied JS and CSS combined).
Thanks for the very useful tutorial on how to extend the prototype to add captions. It was really easy to follow – I had to tweak slightly because I wanted the caption in a data attribute instead of title attribute, but this was easy to do.