Set the image to display at 100% width while maintaining its original height, avoiding the need for scrolling from top to bottom in CSS

img
{
width:100%;
height:auto;
}

I am looking to adjust the height of an image based on the device screen size without the need for scrolling. For example, I want the image width to be 2000px and height to be 1500px.

Answer №1

When dealing with CSS only, one approach is to utilize media queries with predetermined values for viewport widths. There are numerous resources available online detailing typical device widths that you can incorporate as needed. For instance:

img { height: 1500px; width: 2000px; }
@media (max-height:520px) {
    img { width: 693px; height: 520px; }
}
@media (max-height:515px) {
    img { width: 686px; height: 515px; }
}
@media (max-height:510px) {
    img { width: 680px; height: 510px; }
}
@media (max-height:505px) {
    img { width: 673px; height: 505px; }
}
@media (max-height:500px) {
    img { width: 666px; height: 500px; }
}
/* ... and so forth ... */

This method proves effective when relying solely on CSS, albeit necessitating manual adjustment of device width thresholds.

If considering the use of Javascript or jQuery, image dimensions can be dynamically adjusted based on window size changes, including resizing events.

Below is a functional demonstration of JavaScript/jQuery code that modifies image dimensions in response to window size variations:

$(document).ready(function(){

window.onresize = function(event) {

if($(window).height() < $(window).width()){
// adjust based on device height
    var $windowheight = $(window).height();
    var $origheight = parseInt($('img').css('height'));
    var $diff = $windowheight/$origheight;
    var $origwidth = parseInt($('img').css('width'));
    var $newwidth = $origwidth * $diff;
    console.log("h: "+$windowheight+" w: "+$newwidth);
    $('img').css('height',$windowheight);
    $('img').css('width',$newwidth);
}else{
// adjust based on device width
    var $windowwidth = $(window).width();
    var $origwidth = parseInt($('img').css('width'));
    var $diff = $windowwidth/$origwidth;
    var $origheight = parseInt($('img').css('height'));
    var $newheight = $origheight * $diff;
    console.log("h: "+$newheight+" w: "+$windowwidth);
    $('img').css('height',$newheight);
    $('img').css('width',$windowwidth);
}

}

});

The example provided includes somewhat messy code due to its impromptu creation, but it effectively achieves the desired outcome. Certainly, room for improvement exists.

Observe the implementation in action

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

Bootstrap select box gracefully fits within a dynamically adjusting height div

Can someone help me troubleshoot the issue with displaying/overflowing the .drop-down outside of the .item box in this demo? I need to keep the height of the .item as auto but for some reason, I can't enable overflow visible on it. .item{ backg ...

What is the best way to align an HTML element with animated effects in place?

I'm having trouble with CSS animations on an HTML element, specifically when it comes to positioning the element freely on the page. How can I achieve this? I've attempted to use the following code snippet in order to position the HTML element a ...

Changing the size of text using radio buttons in JQuery MobileIs there a way to modify

I am currently working on adjusting the text size for both header and paragraph information in a listview. Below is the JQuery code that I have implemented: $('#rc1').prop('checked', 'true', function(){ $(".feedContainer ...

Tips for saving the chosen dropdown value into a div

I would like to save all the input values from a form into separate divs as they are entered. For text input boxes, I have successfully implemented it using the following code: <input type="text" class="form-control" onkeyUp="document.getElementById(& ...

Tips for adjusting image size in Bootstrap carousel

Is it possible to change the width of the image in this carousel? How can I resize the image without affecting the red box? I've tried numerous solutions on various forums, but none seem to work. The image just won't shrink the way I want it to ...

Error with hyperlinks preventing redirection when clicking on the menu

$(document).ready(function () { $('.mobile-nav-button').on('click', function() { $( ".mobile-nav-button .mobile-nav-button__line:nth-of-type(1)" ).toggleClass( "mobile-nav-button__line--1"); $( ".mobile-nav ...

To interact with a dynamic div that is generated without any specific ID or class, which is contained within a fixed div, you can

Is there a way to trigger a click function using JQUERY on the div that includes the text "Save as JPEG"? The div with the ID "graph1" remains static while all other nested divs are dynamic. Please note that the dynamic div containing the text does not h ...

What is the method for adding a shadow solely on the right side while concealing the bottom border?

After creating a navigation bar with bootstrap, I wanted to make the active tab stand out above the others. Here's what I achieved: https://i.sstatic.net/lmPz8.png Once I managed to connect the tabs with the content using a "before" class, everythin ...

The issue of Bootstrap 4 fixed position not working with inherited width

Exploring content and sidebar: .border { border: 1px solid black; } <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device- ...

Ensuring the sort icon is constantly displayed in MudDataGrid

Is there a way to keep the sort icon visible at all times in mudgrid without any default sorting using mudblazor? I have implemented the advanced data grid from the following link:- I have attempted using sortable=true on both the column and datagrid but ...

Having trouble sending the information to Parse.com using the website

I am a beginner with the Parse database and I am currently working on implementing a code that allows users to sign up, with their information stored in the Parse database. However, I am encountering an issue where the values are not uploading as expected. ...

A guide on how to use Bootstrap to align elements at the center of a div container

My attempt to center an image in the div didn't quite work out as planned. Here's the snippet of my html and css: .col-xs-4 { background-color: lightgrey; width: 300px; height: 200px; border: 6px solid green; padding: 25px; margin: ...

Certain JavaScript code might fail to load on WordPress

I've been struggling to troubleshoot an issue with a JavaScript accordion menu on my WordPress site. When I try to click the accordion button, it doesn't reveal the hidden content beneath it. I've attempted various solutions, but none have s ...

Building Nested Div Tags

I'm struggling with creating nested divs similar to the layout shown in the linked image. Image Could someone please assist me in achieving this? .demo-container { padding: 30px; border: 1px solid #e2e4e7; background-color: #f5f ...

Combining jQuery within a single container/segment/div tag

Currently, I am experimenting with adding jQuery to my Cargo Collective website: I have been working on a mouse-tracking jQuery effect that I really like, but I am facing an issue where the script does not stay contained in a specific section of the site. ...

Modify button text when hovered over

Is it possible to modify the text color of "Contacte-nos" on hover? .popmake-contacte-nos { background-color: #fffff7; /* Green */ border: none; color: black; font-weight: bold; padding: 15px 32px; text-align: center; text-decoration: n ...

Animating shifting of an overflowing div using jQuery

Struggling to articulate this, so here is a little jsfiddle demo: http://jsfiddle.net/UwEe2/ The concept I am aiming for is very similar to the one in the demo, however, I require the image to be perfectly centered. In other words, I need the center of t ...

Is it possible to overlay a div onto a Google Map?

Does anyone know of a plugin or method that can achieve this? I have a website at this link, and at the bottom there is a map. I'm looking to add a small overlay box on top of the map with contact information inside. Edit: The box should be position ...

Having trouble with a basic HTML and JavaScript login page, unable to retrieve the email and password fields when the button is clicked

<!doctype html> </html> <head> <title> </title> <link href="../assets/css/bootstrap.css" rel="stylesheet"> <link href="sigin-style.css" rel="stylesheet"> &l ...

Is there a way to set the image result retrieved from the API call as the background image for my app?

In my current project, the user is able to input a city and the background of the page changes to display an image of that specific city. This image is obtained through an API call to Pixabay API, with the result stored in a variable named "background". Ho ...