How to make Image responsive using Aspect ratio? No media queries are needed

In this blog, you will learn how to make an image responsive without any media queries. We will use the Aspect ratio to make our image responsive.

Video tutorial

I have already made a video about it on my youtube channel. Check that out.

Please like and subscribe to Cules Coding. It motivates me to create more content like this.

Live Preview

The basic idea here is to set the image width in a way that will change when the browser screen size will changes. And when the width will change, the height will also change based on the width.

Now there are two types of aspect ratios you can use.

  • Original ratio of Image
  • Custom aspect ratio

I will show you both. I will also show you how to use the aspect ratio with the nextjs image component.

Original aspect ratio

<style>
    * {
        padding: 0;
        margin: 0;
        box-sizing: border-box;
    }

    .container {
        width: 100%;
        margin: 0 auto;
    }

    img {
        width: 100%;
        height: auto;
    }
</style>
<body>
    <div class="container">
        <img src="ts.jpg" alt="" />
    </div>
</body>

Explanation:

  • The container will take 80% width and will be in the center horizontally.
  • The image is taking the full width of the container.
  • The image will take as much height it needs. It will change when the width will change.

Custom Aspect ratio

Now there are two ways you can use a custom aspect ratio in CSS.

  • aspect-ratio property
  • padding-top property

Using padding-top property

The process is very simple but a little bit tricky. You just need to create extra containers.

<head>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .container {
            width: 100%;
            position: relative;
            padding-top: 56.25%;
        }

        .image {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
    </style>
</head>
<body>
    <div class="container">
        <img class="image" src="./ts.jpg" alt="" srcset="" />
    </div>
</body>

Explanation:

  • The container will now take full width and position relative.
  • The image will take full space of its container. But it will be positioned absolute and aligned with its container. If you don't know about CSS positions, you can check out this video.

  • We need to give height to the container using padding-top. The value will depend on the aspect ratio.

To get the aspect ratio, do the simple math. Do this on the calculator. Just divide the height by width. Then multiply by 100 to get the percentage value.

1920 X 1080 resolution

1080 / 1920 = 0.5625 --> The ratio

0.5625 * 100 = 56.25%
<style>
    .container {
        width: 100%;
        position: relative;
        padding-top: 56.25%;
    }
</style>

Sometimes the image might get too small on small screens. Then you can use a media query and make the image square.

<style>
    @media screen and (max-width: 400px) {
        .container {
            padding-top: 100%;
        }
    }
</style>

Explanation:

  • padding-top will be 100% because in a square height and width is the same. If you divide them you will get 1. And you know the rest.

Use Aspect ratio with Nextjs Image Component.

import React from 'react'
import Image from 'next/image'

const Responsive = () => {
    return (
        <div>
            <Image
                src='/ts.jpg'
                height={9}
                width={16}
                layout='responsive'
                objectFit='cover'
            />
        </div>
    )
}

export default Responsive

Explanation:

  • The idea here is the same. Make layout responsive.
  • Give height and width based on the ratio.

I have made two landing page clone videos with vanilla HTML, CSS, and JavaScript.

  • SpaceX
  • Tesla


You will learn about:

  • Javascript intersection observer to add cool effects
  • DOM manipulation
  • Aligning elements with CSS positions.
  • How to make responsive websites.

These will be great projects to brush up on your front end skills.

If you are interested you can check the videos.

You can also demo the application form

Please like and subscribe to Cules Coding. It motivates me to create more content like this.

That's it for this blog. I have tried to explain things simply. If you get stuck, you can ask me questions.

By the way, I am looking for a new opportunity in a company where I can provide great value with my skills. If you are a recruiter, looking for someone skilled in full stack web development and passionate about revolutionizing the world, feel free to contact me. Also, I am open to talking about any freelance project.

About me

Why do I do what I do?

The Internet has revolutionized our life. I want to make the internet more beautiful and useful.

What do I do?

I ended up being a full-stack software engineer.

What can I do?

I can develop complex full-stack web applications like social media applications or e-commerce sites. See more of my work from here

What have I done?

I have developed a social media application called Confession. The goal of this application is to help people overcome their imposter syndrome by sharing our failure stories.

Screenshot

I also love to share my knowledge. So, I run a youtube channel called Cules Coding where I teach people full-stack web development, data structure algorithms, and many more. So, Subscribe to Cules Coding so that you don't miss the cool stuff.

Want to work with me?

I am looking for a team where I can show my ambition and passion and produce great value for them.
Contact me through my email or any social media as @thatanjan. I would be happy to have a touch with you.

Contacts

Blogs you might want to read:

Videos might you might want to watch:





14