Automatically adjust image size to match viewport dimensions without cutting off edges

I am facing a CSS challenge. The issue is with an image that has dimensions of 1024x500 pixels. When the browser window size decreases below the width of the image (1024px), the image starts to get cropped on the sides. I have set the container width to 100% for viewport sizes below 1024px, which does resize proportionally, but the edges of the image are becoming more and more cut off as the browser window gets smaller.

Is there anyone who can help me dynamically resize my image pixel by pixel without any loss of the original picture or cut offs?

You can view my webpage and adjust the browser window size to observe the issue with the image cropping on the sides...

HTML: Original image size is 1024x500

 <div class="ei-slider">
 <ul class="ei-slider-large">
   <li>
   <img src="http://lamininbeauty.co.za/images/large/makeup.jpg" alt="Vertical Sunbed TanCan"/>
   </li>
 </ul>
 </div>

CSS:

CSS code for large screens:

.ei-slider{
    position: relative;
    width: 1024px;
    height: 500px;
    margin: 0 auto;
}
.ei-slider-large{
    height: 100%;
    width: 100%;
    position:relative;
    overflow: hidden;
}
.ei-slider-large li{
    position: absolute;
    top: 0px;
    left: 0px;
    overflow: hidden;
    height: 100%;
    width: 100%;
}
.ei-slider-large li img{
    width: 100%;
}

Media query for when browser window is smaller than image width (1024px):

@media screen and (max-width : 1023px){
    .ei-slider{
        width: 100%;
    }   
}

CSS adjustments for smaller screens where images are cropped: Note that the original image size is 1024x500

@media screen and (max-width: 930px) and (min-width : 831px){
    .ei-slider{
        width: 100%;
    }   
    .ei-slider-thumbs li a{
        font-size: 11px;
    }
.ei-slider-large li{
    position: absolute;
    top: 0px;
    left: 0px;
    overflow: visible;
    height: 100%;
    width: 100%;
    }
    .ei-slider-large li img{   
        width: 930px;
        height: 454px;
    }
}

Thank you!

Answer №1

Here is how you can achieve the desired effect:

max-width: 100%;
height: auto;
width: auto; /* specifically for ie9 */

By using these css properties, your element will dynamically resize to fit its container based on the max-width set at 100%. Feel free to adjust the max width value to customize the resizing behavior according to your requirements.

Answer №2

If you're looking for a straightforward fix, consider specifying the width using viewport percentage.

Usage :

width: <Percentage>vw;

For instance :

<img src="Images/Photo.jpg" alt="" style="width: 75vw; height: 120px">

In this case, the image's height remains constant while its width adjusts to 75% of the viewport size, regardless of the screen dimensions.

Trust this solution serves your needs :)

Answer №3

I encountered a similar issue while using the jquery plugin ie-slider. Upon investigation, I discovered that the image was receiving additional inline styles from the Javascript code, causing it to appear shifted left rather than cropped. The script dynamically calculates values based on the image dimensions during loading in a specific viewport width, as implemented by the author in the .js file.

var $img = $(this);
imgDim = _self._getImageDim( $img.attr('src')); // retrieves image dimensions

The author then applies a left margin to the image like this:

$img.css({marginLeft: imgDim.left}); // sets a new margin-left value, overriding any existing css declarations due to being inline

As the viewport size decreases, this value becomes negative. A simple fix is to reset the margin-left property:

$img.css({marginLeft: 0});

After implementing this adjustment, my issue was resolved without any subsequent problems arising. Best of luck with your implementation!

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

Exploring the theme color options in Angular Material

Despite extensive searching on SO and GitHub, I am seeking clarification: Is it impossible to access a theme color (or any other theme feature) in order to set a mat-card color? There is no way to retrieve a variable in scss. You cannot assign a class to ...

Can someone assist me in completing a Vertical Dots Navigation feature that activates on scroll?

I'm struggling to create a vertical dots navigation that dynamically adds the "active" class based on the section currently in view while scrolling. For example, if you are on the first section, the first dot should be white and the rest transparent. ...

Having trouble implementing a nested grid system in Bootstrap

Struggling with creating a grid layout that looks like the one in this image. I initially thought of setting up a row with 2 divided columns, then adding another row+column within each of those 2 columns to place a card inside. This is an excerpt of my c ...

I'm having trouble getting my dropdown content to align properly with my centered search bar. Any suggestions on how to fix this issue?

How can I align the dropdown content to the center in its td element? I haven't included all of my code here, so feel free to ask if you need more information. The search bar is supposed to be at the center of this table navbar. This is the HTML: &l ...

Ignoring custom elements does not occur when the child is set to display as inline-block

My code includes custom elements that exhibit strange behavior when a child element has the "display: inline-block" style. Let's examine these two div elements: <div style="padding: 4px;"> <randomcustomelement style="background-color: y ...

Creating a script to open multiple URLs in HTML and JavaScript

Currently, I am working on creating a multiple URL opener using HTML and JavaScript. However, I have encountered an issue where HTTP links are opening fine but HTTPS links are not. Can someone provide assistance with this problem? Below is the code snippet ...

Another element concealing an element underneath

I am currently working on a website and could really use some assistance. Being new to web design, I find myself puzzled by this particular issue. The problem I'm facing is with a sawtooth pattern that should be displayed over a header, similar to the ...

Footer division misbehaving

My goal is to add a footer to my website that includes a text field, but for some reason I'm encountering an issue with two of my divs not cooperating. You can observe this problem in this JSFiddle. Here is the CSS code for the footer: .footer { b ...

Is it possible to switch the orientation of the input fields from vertical to horizontal?

My custom form is displayed below: <form novalidate class="form-group" ng-hide="tab==1"> Reviews Min: <input type="number" ng-init="revNum=0" class="form-control" min="0" step="10" ng-model="revNum" /> Min Price: <input type="numbe ...

Keeping content in the middle of the page

When working with a div tag, I encountered a challenge. <div id="length"></div> My goal is to center the contents of the div while also decreasing its width to hold a number. #length{ background:black; color:white; border:3px solid gr ...

Top jquery CSS selector for locating a table's colspan in Internet Explorer

Does anyone know a more efficient way to find the colspan of table cells in IE, specifically when it's other than 1? The current selector I'm using feels clunky. Any suggestions on how to improve this? if (isIE) { selector = "> tbody > ...

Can a local image be incorporated into an HTML or CSS project through the use of Javascript or alternative methods?

I've been trying, but all I can manage is: <img width='220' height='280' src='chrome-extension://okjaohhbffepkcfacapapdhkmnebgiba/johnny.jpg' class="me"/> Unfortunately, this code only works in Chrome. Is there a ...

Chrome users may encounter difficulty grabbing the horizontal textarea scrollbar if a border-radius has been applied

Check out this JSFiddle for more information <textarea wrap="off" rows="5" style="border-radius: 4px">aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaa aaaaaaaaaaabbbbbbbbbaaaaaaaaaaabbbbbbbbbaaaaaaaaaaabbbbbbbbbaaaaaaaaaaabbbbbbbbbaaaaaaaaaaabbbbbbbbb aa ...

Navigate effortlessly with our vertical multi-level menu - simply hover over an option to reveal the next level of

This particular image demonstrates a vertical menu that is expanded. I am looking to display only the parent tag initially and upon hovering, show the next level and so forth. function display_children($parent, $level, $categoryModel) { $result = $ca ...

The card-img-overlay in Bootstrap 5 seamlessly integrates into the following div container

Currently, I am in the process of creating a painting website layout for my CS50 homepage. However, there seems to be an issue with the background-color: rgba(0,0,0,0.7); not aligning correctly with the images within the card-group. <div class="car ...

Combine the jQuery selectors :has() and :contains() for effective element targeting!

I am trying to select a list item element that has a label element inside it. My goal is to use the :has() selector to target the list item and then match text within the label using the :contains() selector. Can I achieve this in a single line of jQuery ...

Center 3 elements horizontally using Flexbox, ensuring the center element is aligned properly

Is it possible to center 3 elements using flexbox on a page? The middle element should be centered on the page, while the left and right elements can vary in width. Please refer to the image below: https://i.sstatic.net/IVdz8.png I am not certain if this ...

Is it possible for webkit CSS resize to not function properly when applied to a canvas element that is

Consider this HTML and CSS code snippet: #box { width: 100px; height: 100px; overflow: hidden; resize: both; border: 1px solid black; } #content { width: 100%; height: 100%; } <div id="box"> <canvas id="content"> ...

Create another div for the remaining vertical space occupied by the sibling div?

I currently have 3 different divs nested within each other: <div class="a"> <div class="b"> </div> <div class="c"> </div> </div> Here is the current CSS styling applied to these divs: .a { hei ...

How can we use withStyles to customize the styling of a selected ListItem in ReactJS and Material UI?

I am seeking assistance in changing the style of a specific ListItem, similar to what is demonstrated in this Codesandbox. In the Codesandbox example, a global CSS stylesheet is utilized, but I prefer to implement the changes using the withStyle techniqu ...