What is the best way to ensure your background image appears proportional?

I'm having an issue with the background image on my login page - it doesn't look like the original photo:

Take a look at the screenshot to see the problem with the width of the image: https://i.sstatic.net/qb9u0.jpg

Here's the HTML/jsx code for the page:

 <div className="login-page">

   <picture>

     <source srcSet={mobileImg} media="(min-width: 400px) and (max-width: 700px)" />

     <source srcSet={tabletImg} media="(min-width: 700px) and (max-width: 900px)" />

     <img srcSet={desktopImg} className="login-background" alt="background image" />

   </picture>

   <div className="login-wrapper">{children}</div>

 </div>

And here's the css part:

.login-page {
    display: flex;
    justify-content: center;
    height: 100%;
    width: 100%;
    position: relative;
    background-size: cover;
}

.login-wrapper {
    align-self: center;
    position: absolute;
}

.login-background {
    height: 100vh;
    width: 100vw;
    background-size: cover;
    background-position: center center;
}

Any suggestions on how to make the image responsive?

Answer №1

When displaying images with <img> tags, it's important to note that they are not affected by CSS attributes like background-*. To control how the image is displayed, consider using object-fit: cover instead.

Answer №2

It seems like the issue you're encountering is that the image isn't maintaining its aspect ratio. Unfortunately, background properties don't apply to img tags. Therefore, the code you had previously won't be effective.

/* Won't work for img */
background-size: cover;
background-position: center center;

Instead, try using the following code, which is similar to the background properties you were trying to apply:

img {
  object-fit: none;
  object-position: 50% 50%; 
  /* OR */
  object-position: center;
}

For more information, you can refer to this link: https://css-tricks.com/almanac/properties/o/object-position/

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

Is it necessary to save the user's sign-in status in Local Storage in order to render the component (using React, Next.js, and Firebase Authentication)?

(Implementing Google Signin with Firebase and React(Next)) When a user signs in, the state is set to true. The home component and login component are rendered in index.js based on the state value. Initially, I stored this state in local storage, but I rea ...

Guide to connecting two separate components from distinct pages in React/MUI

My setup involves two pages: Appbar.js, where I have a top appbar and a Navigation Menu Icon Button that triggers the display of my Drawer (Material UI) in TempDrawer.js. Initially, everything worked fine when all the code was placed in the Appbar.js file ...

What could be causing my CSS navigation toggle button to malfunction?

My attempt at creating a toggle button for tablets and phones seems to be failing. Despite the javascript class being triggered when I click the toggle button, it is not functioning as expected... https://i.stack.imgur.com/j5BN8.png https://i.stack.imgur. ...

In Next JS and React, child components have the ability to dictate to their parent components what to

I'm currently exploring NextJS and React, delving into dynamic routing. Within my project, I have a parent page that renders a child component containing navigation options. The parent component comprises a layout with a header and right sidebar that ...

Guide to creating an asynchronous function

Starting my first react website and needing assistance with writing an asynchronous JavaScript function. I am currently working on uploading user input files to firebase storage and then making a post request to the API in order to store the data in the da ...

Having trouble receiving a response when making a request to an API

I encountered an issue while trying to fetch an API. Initially, I received an error message stating that the message port was closed before a response was received. After removing all extensions, the error disappeared but now I am still unable to receive a ...

Div expanding beyond intended size when media reaches maximum 468 pixels

When zoomimg is set to width: 145px; height: 145px; it ends up pushing the text that should be inside its parent element out of the parent. If you want to see the Zoomimg ProjectKort divs, check out the code below: .projectkort{ margin: 10px 0px 10px 0 ...

What is the best approach to retrieve all user information using React with TypeScript and the Redux Toolkit?

I'm currently using React with TypeScript and Redux Toolkit, but I've hit a roadblock trying to retrieve user information. Below is my userSlice.ts file: export const userSlice = createSlice({ name: "user", initialState: { user: null, } ...

Troubleshooting Issues with Bootstrap Carousel Functionality

I'm facing some difficulties with the Bootstrap carousel on my website. I have followed all the instructions carefully and researched for solutions, but unfortunately, my carousel is not functioning properly. It seems like everything is set up correct ...

Tips for preventing Uncaught SecurityError: Blocking a frame with origin in phonegap

I'm in the process of developing a phonegap application that serves as a miniature browser for a client's website. This app will allow the client's customers to access their favorite pages with ease. Once a user selects a favorite, it will b ...

Tips on sending references from child to parent components in ReactJS

Upon rendering my child component, I aim to focus on the input box right away. To achieve this, I am attempting to pass the ref of the input box up to its parent component, which contains a modal. My goal is to focus on the input field upon entering the mo ...

Customized placement of form fields on an HTML grid determined by the user

My goal is to organize input elements on a grid based on user preferences. After researching, I stumbled upon CSS grids, which seem promising. I am considering creating a CSS grid with r rows and c columns, then using JavaScript to assign input elements t ...

Enhancing websites with CSS3 and VueJS for fluid and captivating background gradients transitions

After implementing a logic in the application, the gradient is now changing dynamically. <template> <div :style="`background-image: ${backgroundImage}`" class="background"> <snackbar /> <nuxt /> <default-footer /&g ...

Navigating through pages without using Javascript

While working on creating page numbers, I stumbled upon this intriguing CodePen: https://codepen.io/p-ziegler/pen/bmdaaN .b, input[type="radio"] { display: inline-block; width: 16px; height: 16px; vertical-align: middle; } .b { back ...

The CSS menu appears more compact when viewed on a different page

Recently, I've been working on a website for practice and decided to include a menu. It functions perfectly on the first webpage, but appears slightly smaller on the second one. Here's the code snippet I used for the menu on the initial page: &l ...

PHP code that removes a table row from a database

Can anyone help me troubleshoot my code? My PHP seems to be functioning, but for some reason the delete button is not working! if(isset($_POST['delete'])) { $ID = $_POST['value']; $delete = "DELETE FROM tbl_document WHERE ID = ...

Prevent keypress from being detected while the confirm box is displayed

My website heavily relies on key events, and for certain actions, it triggers a bootbox confirm box. This confirm box appears over a transparent layer, blocking all mouse click actions unless the user interacts with the confirm box. Now, I also want to dis ...

Material UI Typography in ReactJS is a powerful combination for creating

Is there a way to make the cursor appear like a button when hovering over typography? Also, how can I incorporate browser router routing to enable clicking? <Grid item lg={12} md={12} sm={12} xs={12} > <Typography noWrap className={c ...

Material-UI's simplification of 10px comprehension

I'm a bit confused about why we have to do this for the 10px simplification: html { font-size: 62.5%; /* 62.5% of 16px = 10px */ } Shouldn't the following code handle everything? const theme = createMuiTheme({ typography: { // Set the ...

What is the reason for the malfunction of input :: - webkit-slider-thumb and input :: - moz-slider-thumb?

Designing a compact slider for enhancing user experience not limited to webkit browsers requires utilizing a scss template such as input { width: 100%; -webkit-appearance: none; -moz-appearance: none; appearance: none; height: 1vm ...