We cannot stress enough the importance of multimedia such as images, infographics, and videos in development. However, the size of images affects the overall performance of an application and its usability.
HTML (hypertext markup language) is the most used language to design and create the structure of web pages. As a developer, you can add product pictures, logos, and other graphics to your HTML document to improve user experience and visual appeal.
However, if you need the application to function optimally, you must resize and optimize your images. In this article, I will explain the importance of resizing images, the different approaches to resizing images in HTML, and the best practices.
What is the importance of resizing images in HTML?
Resizing an image is scaling up or down the size of an image. Typical images are stored in different forms, and you can get them from your photographer, events, or designer or download them from online sources.
You may be tempted to use images in their original formats. However, in most cases, this is not desirable. These are some of the reasons why you should resize images in your HTML document:
Page load speed: Although images are known to improve user experience, having large images may negatively affect the loading speed. Optimizing such images will make your web pages load fast.
Make your website responsive: A typical web/application receives visits from people using varying devices. Resizing images makes a web page responsive to mobile devices, desktops, and tablets.
Bandwidth usage: Most website hosts limit bandwidth when selling hosting packages to website owners. When you optimize images, you get more space for other types of content on your website.
SEO purposes: Page loading speed is one of the ranking factors that search engines consider. Resizing your images improves the loading speed, which eventually boosts your SEO.
Maximize storage: The size of storage space is one of the factors to consider when selecting a web host. Uploading large and high-resolution images may consume a lot of storage space on the servers.
Improve user experience: Website users don’t have to wait ages before images load on a web page. You can optimize your images to appear simultaneously with the rest of the content.
These are the different ways to resize images in HTML
Using HTML attributes
We can create a simple application to demonstrate how to resize images. I will be using images from Pixabay in my web application. I will use this image. You can download it and move it to your project for easy demonstration.
You can now see the image is visible and optimized.
Using CSS Styles
We can create a separate style sheet instead of the inline styling we applied in the above step. I will create a style sheet and name it styles.css. We will place a <link> tag within our HTML document’s <head> section to link the stylesheet with the HTML document.
We can add styling to our image through the styles.css sheet. This is our code:
The rendered image will appear as follows:
We have used the <img> selector to locate our image on the HTML document. We have given our image a width of 300px and a height of 200px.
In the previous example, we have used pixels (px) to resize our image. We can also use percentages to optimize our image. For instance, we can set the width and height of our image to 65%.
We will use our previous HTML code. However, delete what is in the styles.css file and add this code:
When you render the image, you will get the following:
Resizing Images for Responsive Design
A good image should be responsive to different screen sizes. You can also optimize your image to respond to different screen sizes. Using the same HTML code, we can add this to our styles.css file:
The max-width: 100% property ensures that the image will never exceed the width of its container. As such, the image will always scale down proportionally on smaller screens.
The height: auto property ensures that the image maintains its aspect ratio as it scales up and down.
Maintaining the Aspect Ratio
In web/app design, the aspect ratio is the proportional relationship between height and width. Maintaining the aspect ratio while resizing or optimizing your images ensures that they maintain visual consistency; the images are not distorted and are responsive to different screen sizes.
We will use the same HTML code to demonstrate how to maintain the aspect ratio while resizing our images.
Add this code to your styles.css file:
I have used the ‘img’ selector to locate our image in the HTML document. We have set our width as 300px. The height: auto property allows different browsers to automatically calculate the height of the image while maintaining the aspect ratio. This ensures the images aren’t stretched while rendered on different screen sizes.
Resizing Background Images
You can insert images on elements to serve as background images. Resizing a background image ensures that it will fit inside its container. This is an example of an HTML document with a paragraph and some text:
A background image can only be added using CSS. I have created a container with centered text and a background image of a cat.
Using Image Resizing Tools
You can use different tools to resize and optimize your images. Some of these tools only offer you a resizing feature. However, some will also allow you to store images and serve to your HTML file using a URL.
Cloudinary is a good example of a tool that resizes the image on the server before it delivers it to the users. This tool is designed to automatically resize images without impacting site performance.
Best Practices for Resizing Images in HTML
Although we have discussed several approaches for resizing images in HTML, you should always stick to these best practices for optimal results:
Avoid inline styling: You may be tempted to quickly style images from your HTML documents and move to the next part. However, this approach may negatively affect the web page loading speed. Use a separate CSS styling sheet and make your code easy to read and maintain.
Use ‘width’ and ‘height’ attributes: Ensure you specify your images’ width and height. This approach directs the browser to automatically allocate the image space as the rest of the content loads.
Use the correct image formats: Images come in different formats, such as JPEG, PNG, and SVG. For instance, PNG is a perfect format for a transparent image, while SVG suits logos.
Make your images responsive: You may never know the nature of your website visitors’ devices to access your content. Always optimize the images for different screen sizes and ensure uniformity in display.
Optimize the file sizes: Raw high-quality images might come in big files. However, such images might take up a lot of space on your server and affect page loading times. You can reduce the size of such images using various tools without lowering their quality.
Resizing your images in HTML improves user experience, makes your web page responsive, and saves on server space. We have presented you with several approaches to resizing images in HTML.
Even though they are all effective, I love using CSS properties in a separate CSS file to resize the images. However, you can sometimes add inline CSS if your application is small and you have a few images.
You can also read our article on CSS Resources and get a deeper understanding of this styling language.
When a user opens a website, one of the first things they notice is the header. A website header is the top section of a webpage, which contains elements such as a site’s logo, navigation menu, and functionalities such as searching and logging in.
Financial charting libraries help you to add stock market and digital asset market movement charts in any app. You will find both HTML5 charting libraries and JS libraries for app development projects.