Struggling to make a row of images resize smoothly and responsively when the viewport size changes

Struggling with making a row of images resize responsively when the viewport changes? Are your images wrapping awkwardly or leaving white space as the screen narrows, requiring users to scroll horizontally to view them? I want my images to resize without wrapping and fill the entire width of the screen border-to-border. Check out this wireframe for reference

New to web design/development and seeking assistance on Stack Overflow for the first time.

Code Used

html, body, .row {
  height: 100%;
  max-height: 100%;
  width: 100%;
}
body {
  line-height: 1.75rem !important;
}
.container-fluid {
  padding: 0 !important;
  margin: 0 !important;
}
.about-imgs-item {
  height: 240px; 
  width: 245px; 
  display: flex;
  object-fit: cover;
  justify-content: start;
  flex-wrap: nowrap;
}
@media (max-width: 575px) {
  .about-imgs-item {
      display: none;
  }
}
<div class="container-fluid dev">
    <div class="row about-imgs">
        <div class="about-imgs-item">
            <img id="about-imgs-1" src="image1.jpg">
        </div>
        <div class="about-imgs-item">
            <img id="about-imgs-2" src="image2.jpg">
        </div>
        <div class="about-imgs-item">
            <img id="about-imgs-3" src="image3.jpg">
        </div>
        <div class="about-imgs-item">
            <img id="about-imgs-4" src="image4.jpg">
        </div>
        <div class="about-imgs-item">
            <img id="about-imgs-5" src="image5.jpg">
        </div>
    </div>
</div>

GitHub Link to code

Attempted solutions:

  • Using flex-wrap: nowrap;
  • Switching from px to rem for image dimensions
  • Implementing media queries for specific screen sizes
  • Removing the container

Still learning, so may not have applied these concepts correctly. Appreciate any guidance!

Answer №1

To eliminate the horizontal scroll, simply include display:flex; and flex-wrap:wrap; in the item container

<div class="image-container">

.image-container {
  display: flex;
  flex-wrap: wrap;
}

Answer №2

One issue that arises is when image sizes are hard-coded.

An improved method involves splitting the container element into 5 equal columns:

.about-imgs { 
  display: grid;
  grid-template-columns: repeat(5, 1fr);
}
.about-imgs img {
  /* prevents gaps when the images 
     are too small to fill the row width
     Might produce pixelation if the images are enlarged too much!
   */
  width: 100%;
}

and simplifying the markup to:

<div class="row">
  <div class="col about-imgs">
    <img src="image1.jpg">
    <img src="image2.jpg">
    <img src="image3.jpg">
    <img src="image4.jpg">
    <img src="image5.jpg">
  </div>
</div>

Check it out here.

Note: I enclosed the row contents in .col to offset the negative margins applied by .row on specific screen sizes.

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 Angular be used for live search to provide precise search results even when the server does not return data in the expected sequence?

Currently, I am in the process of constructing a website for one of my clients. The search page on the site is designed to update search results as you type in the search bar - with each keystroke triggering a new search request. However, there's an i ...

What is the best method for closing the open portion of an accordion menu?

I'm experimenting with creating a collapsible accordion feature, but I'm facing a challenge in figuring out how to close the currently open accordion pane. The accordion functions smoothly when expanding sections, but I'm stuck on how to col ...

Using httpRequest to handle binary data in JavaScript

Having trouble deciphering the response of an http request that is a binary datastream representing a jpeg image, despite numerous attempts. Edit: Including the full code snippet below: xmlhttp = false; /*@cc_on@*/ /*@if (@_jscript_versio ...

Ways to verify browser rollover support (as some mobile devices lack a cursor)

I am currently exploring options to determine if rollover events are supported for navigation. If not, I plan to switch to click events. I have reviewed jQuery.Support and Modernizr but haven't come across anything that directly addresses my issue. T ...

The inclusion of Bootstrap 4 in the header seems to be causing issues with mouse events not functioning correctly

Exploring the functionalities of Bootstrap 4 and jQuery has been an interesting journey. I recently worked on creating a Navbar with 4 links, where the 4th link has a submenu that opens upon hover. However, I encountered an issue where this functionality o ...

The value returned by $_GET is always 1

I'm struggling with passing a GET variable to another PHP page. My issue involves deleting rows from a table of content using AJAX. The Ajax function works flawlessly on a simple example with a form. Here is the code snippet: while($row=mysql_fe ...

Is there a way to search for text and highlight it within an HTML string using Ionic

Welcome everyone! I am currently utilizing Ionic to load an article from a local HTML string. <ion-content padding> <p [innerHTML]="url"></p> </ion-content> The 'url' variable contains the local HTML string. My goal ...

Activating the first item in a custom menu in WordPress

I'm trying to make the first custom menu item active by adding an "active" class. Can anyone help me achieve this? Here is the HTML code: <nav class="secondmenu"> <div class="menu-captain-crew-container"> <ul id="menu-capta ...

Different ways to prevent invalid entries in an input field with type datetime-local

<input type="datetime-local" step="1"> Is there a way to prevent invalid date input? For example, entering a date like "11:11:1111" in the format "mm-dd-yyyy". How can this be addressed using Moment.js? ...

Arrange the table in alphabetical order and categorize it

Hello there! I am currently working on creating a well-organized table list where names are sorted into specific categories based on their starting letter. For example, names beginning with 'A' will be placed in the 'A category', while ...

The Layout of Bootstrap4 Form is Displaying Incorrectly

I'm attempting to create a basic email form similar to the one shown in the Bootstrap 4 documentation. However, I am facing an issue where the formatting is not displaying correctly. Here is what the example should look like: https://i.sstatic.net/qx ...

Apply a Fade In transition to the ul li elements following a JavaScript toggle

I'm currently experimenting with implementing a Fade in from the right animation for the links displayed in the menu after toggling the menu body in the browser. Despite my efforts, I am unable to determine why the animation is not functioning as expe ...

Reorganizing content for smaller screens using Bootstrap 4

This is an example of the structure of my webpage: <div class="container new-container"> <div class="row"> <div class="col-md-4 info"> .... </div> <div class="col-md-8 contact-form"> .... </div> ...

How can we filter the input type file browse window to display only image files?

I'm trying to figure out how to filter the window popup so that it only shows image files when I click on the input type file. <input type="file" /> Any help would be greatly appreciated as I have been struggling to find a solution for this. ...

I am experiencing difficulties updating my website

Is there a way to automatically refresh my webpage in PHP whenever I press the enter key? ...

Press the smiley icon and drag it into the designated input box

Is there a way to select and copy a smiley/emoji from a list and paste it into an input field? Although the Inspect Element Q (console log) shows that the emoji is being clicked, I am having trouble transferring it to the input field. Here is the HTML cod ...

What is the best way to make an element "jump to the top" when the parent element has reached its maximum height?

I have a parent div containing 6 child divs. The height of the internal divs changes dynamically based on the data. The outer div has a fixed height set. I want the internal divs to move to a new column inside the parent div when they no longer fit in term ...

Display a modal popup and simultaneously launch a link in a new tab

I need help setting up my modal popup. I would like to customize it so that a specific link opens in a new window instead of within an iframe. Currently, this is the code I am using: <div class="modal-content" style="max-width:767px;"> <span c ...

Is there a way to target a sibling element of another element by using its identifier in Cypress?

My current task involves clicking a checkbox within a list of table rows. The only way I can think of reaching this level is by targeting the span tag along with its corresponding name. cy.get('tr > td > span').contains('newCypressTes ...