How can I make an image fill the container with CSS zoom effect

My website features a full viewport image as the background beneath the Nav Bar.

I am looking to achieve a specific effect on mobile where the image is zoomed in and centered without distortion, allowing some of it to still be visible as a background. The mockup below illustrates what I am trying to convey:

https://i.sstatic.net/HXqn4.png

In this illustration, the red outline represents the original image boundary, while the blue area signifies the mobile viewport. I want the image to appear "cropped" or zoomed in on mobile devices. How can I achieve this using CSS? Thank you

Edit: To clarify my attempts so far, traditional methods like background-size: cover and background-position: center have not yielded the desired result. I am currently working with a React Bootstrap Carousel that requires styling adjustments for the <img> attribute instead. It seems like the background-size/position attributes only affect the <html> tag's style, leading to challenges in implementing suggestions based on my experience.

For instance, I have implemented the following code in my homepage.scss:

.fullImage {
    background-position: center;
    background-size: cover;
}

And then applied it to the image within the Carousel component:

<Carousel.Item interval={5000}>
  <img
    className="fullImage"
    src="/images/home/banner/img1.jpg"
  />
</Carousel.Item>

However, the desired effect on mobile remains elusive, resembling something closer to the middle image in this visual representation:

https://i.sstatic.net/Te3uT.jpg

The ultimate goal is to have a fixed-sized, "zoomed-in" view of the image on smaller screens, without any scrollable elements outside the viewport.

Answer №1

If you absolutely must include the img element (without a background-image), my preference would be to use object-fit: cover. Keep in mind, this method is supported only in modern browsers.

* { margin: 0; } /* resetting styles */
.header { height: 100vh; width: 100vw; overflow: hidden; }
img { width: 100%; height: 100%; object-fit: cover; }
<div class="header">
  <img src="https://picsum.photos/1920/1080"/>
</div>

Alternatively, if possible, I recommend using src-sets to replace the image on mobile devices with a cropped version to prevent unnecessary loading of large images.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to align three divs next to each other in an HTML structure?

I am currently working on a website layout that consists of three sections positioned horizontally. I am aiming for the left division to take up 25% of the width, the middle one to occupy 50% of the width, and the right division to fill 25% of the width so ...

Is there a way to display an image in a React Native app using data from a JSON file?

I am currently working with JSON content that includes the path of an image. I need to display this image in my React Native application. What is the best way to render this image? {"aImages":[{"obra_path":"http:\/\/uploa ...

`Communicating between a ReactJs+NodeJs client application and a Java web service``

My current setup consists of a client-side application built in ReactJs and NodeJS (using an Express server), while the web services are handled by a Java application running on Tomcat. I am seeking advice on the best approach for making web service calls ...

Creating a 3D slider on Owl-carousel 2: A Step-by-Step Guide

I am trying to create a slider using Owl-carousel, but I'm having trouble implementing it. I came across this example https://codepen.io/Webevasion/pen/EPMGQe https://i.stack.imgur.com/KnnaN.jpg However, the code from this example doesn't seem ...

Activate Popover with Hover and simply click anywhere to dismiss

Currently utilizing Bootstrap 4 and intrigued by the popover feature that activates on hover and closes when clicked anywhere. I'm also interested in making links functional within the popover. Any suggestions or tips on how to achieve this? $(doc ...

Problem with structuring a Html5 web page

Recently, I created a web page using HTML5. Check out the code below: <header> <img src="img/logo.png" alt="logo"> <hr class="hr-style"> <h1>The Articles</h1> <nav> <ul> <li><a href="#" ...

Attempting to create a dynamic dropdown menu with animated effects triggered by a key press

I've been attempting to construct a menu that initially appears as a small icon in the corner. Upon pressing a key (currently set to click), the checkbox is activated, initiating the animation. Most of the menu and animation are functional at this po ...

After resolving a promise, what is the process for loading a Next.js App?

Seeking guidance on implementing the code snippet below using Next.js. I suspect there is an issue with Next.js not being able to access the window object without being within a useEffect(() => {}) hook. When switching back to regular React, the code ...

Utilizing node packages with Laravel

I am attempting to integrate the following module: https://www.npmjs.com/package/react-datepicker-component in React within my Laravel Framework. Although I have used npm for installation, I am encountering difficulties beyond that point. Any guidance wo ...

React: CORS policy prevents access to the requested resource

When attempting to retrieve an API endpoint in React, everything runs smoothly on my localhost. However, after building the React project and deploying the build files to Firebase Hosting, I encounter an error when accessing the now publicly hosted URL: T ...

I am having trouble with the CSS and Bootstrap not being applied after printing

Once the submit button is clicked on the initial output page, the CSS styling disappears and only a simple default form page is displayed. This does not meet my requirements. Using inline CSS allows it to work, but external CSS does not. Can someone please ...

tips for effectively retrieving and processing snapshot data from Firestore and Firebase authentication in ReactJS

I find myself in a puzzle when it comes to the flow of firebase auth and firestore snapshot. My understanding is that since the snapshot provides updates for the document, I should start the snapshot after the user logs in or maybe after they refresh their ...

What exactly is the mechanism behind how the text-overflow property's fade value operates?

I am struggling to add a one line description to each flex-child using the fade() value, but it seems to not be working as expected. .flex-container { display: flex; flex-wrap: wrap; justify-content: center; flex-direction: row; } .flex-child { ...

What is the best way to send props to a component that is exported using a Store Provider?

I'm trying to export my react component along with the redux store Provider. In order to achieve this, I've wrapped the component with an exportWithState callback. However, I'm facing an issue where I can't seem to access the props that ...

Encountering a connection error when trying to access a Google spreadsheet within a Next.js application

I am currently exploring Next.js and attempting to utilize Google Sheets as a database for my project. Although my application is functioning correctly, there is still an error message displaying that says "not forgot to setup environment variable". I have ...

Display a list with a set limit and an option to show more by clicking

I am currently struggling to develop a list of items that displays 3 items per row, and when the "show more" button is clicked, another 3 items should be displayed. My issue arises when using map and slice, as this method results in rendering 3 of the same ...

The JQuery command to set the display property to "block" is not functioning as expected

When trying to toggle the visibility of my TextBox based on a selected value in a RadiobuttonList, I initially wrote the following code: $("#<%= rbtnIsPFEnabled.ClientID %>").click(function () { pfno = $("#<%= txtPFNo.ClientID %&g ...

Searching for the closest jQuery value associated with a label input

As a beginner in JQuery, I am looking to locate an inputted value within multiple labels by using a Find button. Below is the HTML snippet. Check out the screenshot here. The JQuery code I've attempted doesn't seem to give me the desired output, ...

Reverting a concealed segment

Can anyone make sense of my unconventional explanation? I've set up a hidden section as shown below: <section class="intro"> <label> <input type="checkbox"> <span class="menu"> <span class="hamburger"></span&g ...

Encountered an issue retrieving information from the API - connection to the server could not be established - encountered an 'Access-Control-Allow-Origin' error

I'm having trouble submitting a form and receiving the data via email. When I try, it shows an error message: http://localhost:4000/contact net::ERR_CONNECTION_REFUSED I've attempted to resolve this issue by trying different cors options. Here a ...