Show image in entire browser window without resizing unless image is oversized

I am trying to achieve the display of an image centered within the entire browser window with a few conditions in place. If the image can fit within the browser's client area, it should be displayed at its original size. If the image is too tall or too wide for the browser window, then it needs to be scaled down accordingly. Additionally, if the user resizes the browser window, the image should adjust accordingly by either scaling down (if too large) or maintaining no larger than its original size.

While I currently have a solution using jQuery, I am curious to know if this functionality can also be implemented using CSS alone?

EDIT:

The closest attempt I have made so far is shown here:

https://jsfiddle.net/Deepview/o5vgo6du/

html

<div class="outerCont">   <img src="http://www.wonderslist.com/wp-content/uploads/2015/10/Doutzen-Kroes-Most-Beautiful-Dutch-Woman.jpg" /> </div>

css:

.outerCont {
  position: relative;
}

img {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
   max-width: 100%;
    height: auto;
    width: auto;
}

However, the issue I am facing is that the image does not center vertically as desired.

Answer №2

Are you looking to achieve this? Simply apply background-size:cover and it will consistently fill the entire div.

*{
margin:0;
padding:0;
}
.image{
background:url('https://www.aussiespecialist.com/content/asp/en_gb/sales-resources/image-and-video-galleries/jcr:content/mainParsys/hero/image.adapt.1663.medium.jpg') no-repeat;
background-size:cover;
width:100vw;
height:100vh;
}
<div class="image"></div>

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

Image tinting color picker tool (using HTML, CSS, and web technologies)

I'm currently exploring ways to apply a filter or tint on top of an image using CSS/HTML or some lightweight solution. For example, Ideally, my goal is to only have one image displayed on the page and overlay it with a transparent filter based on a ...

Storing JavaScript functions in a MySQL database

Is there a way to save the output from a JavaScript function to MySQL? var macs = { getMacAddress : function() { document.macaddressapplet.setSep( "-" ); return (document.macaddressapplet.getMacAddress()); } } ...

Using a CSS button to activate a JavaScript function: A step-by-step guide

My current project involves writing a script to change the color of text when a specific button is clicked. The idea is that clicking the "Change color1" button triggers the text color change using the following code snippet: <button onclick="myFunction ...

How can you organize an array into rows and columns for easier viewing?

Hopefully everything is clear. I am open to making changes if needed to address any important points. In one of my components, the array items are displayed within cards as shown below: <b-card-group deck class="mb-3"> <b-card border-variant ...

Challenges in rendering textures

Hello there, I'm encountering an issue with the way a texture is being rendered on an imported OBJ model. Below is an image showing how the texture currently appears: And here is how the texture should actually look when mapped to the object: Here ...

Check if a Vue 3 component has been loaded and if not, load a general component

Currently in the process of migrating a project from Vue 2 to Vue 3, I find myself searching for a way to determine if a component is loaded, and if not, how to load a GenericComponent as a placeholder. In Vue 2, I was able to accomplish this task using ...

Problems with jQuery UI datepicker and current date

My goal is to configure the jQuery UI datepicker so that the minimum date allowed is today, and the maximum date is at least 18 years from now. If a user chooses a date less than 18 years ago, an error message should be displayed: $('#dateofbirth&apo ...

Having trouble displaying a local JSON file in React?

I've been troubleshooting this issue with a local JSON file. The console.log shows that all values are being returned correctly, confirming that the path is accurate. However, when attempting to extract the 'name' input value, I encounter th ...

Prevent child element from being rotated along with parent element

I am facing an issue with two div elements: a parent element that is rotated and a child element that I want to remain unaffected by the rotation of its parent. To tackle this problem, I tried rotating the child element in the opposite direction of the pa ...

The Angular multi select tree feature seems to be malfunctioning

I recently incorporated a plugin called angular-multi-select-tree from GitHub into my project. Here is how I added it: First, I installed it via bower using the command bower install angular-multi-select-tree --save. This updated the bower.json file to i ...

Token Fields malfunctioning

I attempted to use a sample from bootstrap, but I am not having any luck with it. Is there anyone who can assist me in resolving this issue and understanding it better? Code Snippet: <input type="text" class="form-control" id="tokenfield" value="red, ...

Sending POST Requests with Next.js Version 14

How can I send a POST request using axios in Next.js 14, I prefer axios but fetch is also okay. I have been getting an AxiosError: Network Error when I try to use axios and TypeError: fetch failed when using fetch. However, it works fine with Postman. I ...

The scroll event is triggered only when scrolling upwards

I am encountering an issue with my scroll function. Currently, it only alerts when scrolling to the top instead of the bottom. How can I correct this so that it triggers the alert when reaching the bottom of the page? $(window).scroll(function() { if ...

Steps to Remove the Displayed Image upon Click

I have a collection of images such as {A:[img1,img2], B:[img1]}. My goal is to remove the array values that correspond to previewed images upon clicking the delete button. Each image is equipped with its own delete button for this purpose. This snippet ...

What is the best way to adjust the size of this text using CSS?

I'm encountering an issue with font size in CSS. In the code below, you can see that I have a .post class containing < pre > tags nested inside it. Shouldn't the CSS styling for the pre tags work as intended? Despite setting the font size t ...

Using EJS to Render a Function Expression?

Has anyone been able to successfully render a function call in express using EJS? Here's what I've tried so far: res.render("page", { test: test() }); Can someone confirm if this is possible, or provide guidance on how to call a function fr ...

Retrieve an image located outside of a container

I have multiple SVGs inside separate div elements. <div id="divA"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <rect x="10" y="10" height="130" width="500" style="fill: #000000"/> ...

Unable to set focus on a disabled button programmatically, issue persists

$(function () { $('#save').click(function() { $('#save').attr('disabled', 'disabled'); console.log($(document.activeElement).attr('id')); }); $('#test').focus(); co ...

Try triggering transitions on all elements by hovering over any CSS element

I am currently in the process of developing a website, and for the header section, I have decided to use a table format where each column represents a different section. The top row will feature a picture link, while the bottom row will display the name of ...

Implement a Bootstrap accordion onto a webpage seamlessly alongside an outdated jQuery version to avoid any conflicts

My bootstrap accordion is not functioning properly, the animation seems to be broken. How can I fix it? I suspect that it might be due to using an outdated version of jQuery or because the theme developers implemented a custom solution. The end result is ...