What is the best way to adjust image sizes on a web page for various screen sizes?

Is there a way to adjust image sizes on my website based on different screen resolutions? Specifically, I need to reduce the width of images from 28% to 20% for screens with a resolution of 2560x1440. This change is necessary as it will enhance the overall look and feel of the website design. However, this adjustment should only apply to that particular screen resolution, while maintaining the original size for other resolutions like 1280x720.

To better illustrate this, here is an example image at 28% width: https://i.sstatic.net/ZoHyw.jpg

And this is how I want the image to appear, with 20% width specifically for that screen resolution: https://i.sstatic.net/Xj5fk.jpg

I have attempted to achieve this using CSS, but so far without success:

@media (max-width: 2000px){ 
  .amenajari_interioare_css{
    width: 20%;
  }
}

Below is the HTML and CSS code I am currently working with:

HTML:

<section id="showcase">
    <div class="container">
        <h1>LOCUINȚE</h1>
        <div class="amenajari_interioare_css">
            <img src="../img/locuinta1.jpg" alt="" />
        </div>
        <div class="amenajari_interioare_css">
            <img src="../img/locuinta2.jpg" alt="" />
        </div>
        <div class="amenajari_interioare_css">
            <img src="../img/locuinta3.jpg" alt="" />
        </div>
        <div class="amenajari_interioare_css">
            <img src="../img/locuinta4.jpg" alt="" />
        </div>
        <div class="amenajari_interioare_css">
            <img src="../img/locuinta5.jpg" alt="" />
        </div>
        <div class="amenajari_interioare_css">
            <img src="../img/locuinta6.jpg" alt="" />
        </div>
</section>

CSS:

.amenajari_interioare_css{
    min-width: 350px;
    width: 28%;
    height: 300px;
    display: inline-block;
    margin: 8px;
    position: relative;
}

.amenajari_interioare_css img{
    margin-top: 5px;
    margin-bottom: 1px;
    width: 100%;
    height: 100%;
    border: 1px solid black;
    border-radius: 16px;
}

Answer №1

Your CSS has a few errors, particularly in the media query section. In order to make changes for screen sizes larger than 2000px, you should use min-width instead of max-width.

@media (min-width: 2000px){

.amenajari_interioare_css {
    width: 20%;
    }
}

Another important adjustment to make is:

.amenajari_interioare_css img{
    margin-top: 5px;
    margin-bottom: 1px;
    width: 100%;
    height: auto;
    border: 1px solid black;
    border-radius: 16px;
}

Make sure to set the height to auto, especially if your image isn't perfectly square, as otherwise it may appear stretched.

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

How can I import XML files containing image links for embedding in a 2D JavaScript Array?

For a few months now, I have been dedicating my free time to working on a personal project - an open-source version of SimTower with updated graphics that I have created myself. In the initial stages, I developed a webpage that displayed a collection of i ...

Achieving True Nested Divs: Making Children Truly Inside

I want to create nested divs for positioning children with top and left properties, allowing them to overlap each other: https://jsfiddle.net/e0cpuarv/ .boo { position: absolute; left: 10px; top: 10px; width: 100px ...

Toggle class on element based on presence of class on another element

If I have 4 boxes and the user is required to choose one. I aim to assign an active class to the box that the user clicks on. However, if the user selects another box, I want to remove the class from the first clicked box and apply it to the newly clicked ...

Using PHP code to dynamically update the CSS styles on a

I am looking to adjust the css based on section_name. For example, if the section is labeled Featured, the css should include right:0. On the other hand, if the section_name is Latest, the css should include left:0. Is there a way to incorporate some PHP c ...

Issue with preventing default behavior of a link when clicking using Jquery?

Is there a way to prevent accidental link clicks on mobile devices when scrolling? I am having trouble implementing this functionality and would appreciate some guidance. Currently, I am testing the code on a desktop environment. My button structure is as ...

What is the best way to insert a JavaScript variable into a filter while using ng-repeat?

I'm currently working on a situation where I have the following code: <div ng-repeat="i in MediaFeedItems | filter: { category: 'userSelect' }" class="mediafeed--item"> This specific code snippet is responsible for iterating through ...

What is the best way to deactivate the onclick event after it has been triggered?

There was an image that triggered the add_cart() JavaScript function when clicked using onclick() <img src="images/add.png" onclick="add_cart()"> The goal is to limit the user to clicking the image only once. Upon the first click, the add_cart func ...

Generate a series of 5 columns that repeat in Bootstrap 4

I need to figure out how to create a 5 column grid in Bootstrap 4 to display over 100 images. The code I have currently only works if I have exactly 5 items, but what should I do if I have more than that? Additionally, the grid needs to switch to a 2 colu ...

The iFrame is set to a standard width of 300 pixels, with no specific styling to dictate the size

My current challenge involves utilizing the iframe-resizer package to adjust the size of an iframe dynamically based on its content. However, even before attempting any dynamic resizing, I encounter a fundamental issue with the basic iframe: it stubbornly ...

Transform the *.svg image source into inline SVG for easier CSS customization

My goal is to include <img src="some.svg"> in my HTML, as inline SVG can be cumbersome and affect page load times. However, I would like to use CSS to style elements like fill:#fff. PS.: The webpage will be hosted on a nodeJS server and the co ...

Adding a transition effect to a div in CSS when switching from col-12 to col-9

I am facing an issue with the CSS provided below. It seems to only work when I close my div with the class "onclick-div". However, the transition effect is not visible when I click on the button to reduce the width of the div with the class "set-transiti ...

Choose the data attributes you want to include and attach them to input elements using plain JavaScript

Creating a User Information form with a select element containing four options, each with data attributes. Under the select are input types to populate the data attributes when selected. Looking for help in achieving this using plain JS and onchange event. ...

Is it possible to invoke fancybox using a class selector?

I'm having trouble using fancybox on an element with this code: $(this).parent().parent().parent().find('.comments').fancybox(); Is it not feasible? Could it be because it's a class and not an id? I have multiple rows of items, so as ...

PHP Debate: Static vs Non-Static - Which is the Superior Choice?

Static and non-static functions as well as class properties really had me puzzled when I attempted to launch my website. Could someone clarify whether it is preferable to have the entire website coded using static functions rather than relying on non-stat ...

Eliminate any unnecessary space below a list item and ensure it is justified

I need help with some extra indentation that is appearing under an ordered list I have created. I want to eliminate the extra spacing to achieve a cleaner look. This is how I want it to appear: https://i.sstatic.net/O7cyG.png However, it currently looks ...

When padding is added on an MVC 5 page, the Bootstrap 4.1.1 class option for rounded-circle becomes distorted

Incorporating VS 2017 and bootstrap 4.1.1 into an MVC 5 page, I am facing a challenge in adding right-side padding to an image without distorting the circular shape of the image, as shown below. When I apply the padding, the circle becomes warped, but remo ...

In a remarkable design with an array of buttons and an individual div assigned to each, a fascinating functionality unfolds. Whenever a

https://i.stack.imgur.com/emhsT.png When I click on the first "Respond" button, I want the adjacent text box to disappear. Currently, if I click on the first "Respond" button, both text boxes will disappear instead of just the first one. $('.comment ...

Adjusting Opentok React video to utilize the complete height of the encompassing container

I'm currently utilizing https://www.npmjs.com/package/opentok-react within my application and setting up a publisher and subscriber in a specific div. This is the CSS I have implemented: #videos { position: fixed; right: 0; bottom: 0; ...

"Utilizing Bootstrap 4: Understanding the Optimal Times to Implement the 'Row

After studying the fundamental guidelines for Bootstrap 4, a question has arisen in my mind: Under what circumstances should we utilize the .row class in our layout? I came across this example where the .row class was not used in the code. However, the ba ...

jQuery animation for expanding accordion headers

I want to create a cool animation for my accordion headers that mimics a ribbon being dragged onto the wrapper when hovered over, and then dragged out when the hover ends. Everything was working fine in this initial jsFiddle, but as soon as I tried to ani ...