Creating a dynamic lineup of images that adjust seamlessly to different screen sizes

Trying to figure out how to create a row of images that adjust responsively to the width of the window. Here's what I have so far:

<div class="image-slider">
  <div><img src="/img1.jpg"></div>
  <div><img src="/img2.jpg"></div>
  <div><img src="/img3.jpg"></div>
  <div><img src="/img4.jpg"></div>
  <div><img src="/img5.jpg"></div>
</div>

and...

.image-slider {
  display: flex;
  overflow-y: scroll;
}

.image-slider div {
  flex: 1;
  margin: 5px
}

.image-slider div img {
  width: 100%;
  height: auto;
  min-width: 125px;
}

Works well, but now I want the fifth image to disappear when the window is less than 880px and have the remaining four images resize to take up the remaining space.

I attempted adding a wide-only class to the fifth div tag:

<div class="wide-only"><img src="/img5.jpg"></div>

Then added some media rules as shown below, but it's not quite functioning as expected:

.image-slider div img.wide-only {
  display:none;
}

@media(min-width:880px) {
  .image-slider div img.wide-only {
    flex: 1 /* Unsure what this should be - also tried display: flex */
  }
}

Answer №1

You made an adjustment by adding display:none, so in the media query, you should include display:block. Additionally, I made corrections to some CSS selectors as they were not targeting the correct tag:

.image-slider .wide-only {
  display:none;
}

@media(min-width:880px) {
  .image-slider .wide-only {
    display:block;
    flex: 1 /* uncertain about this value - attempted display: flex too */
  }
}

Here's the full updated code:

.image-slider {
  display: flex;
  overflow-y: scroll;
}

.image-slider div {
  flex: 1;
  margin: 5px
}

.image-slider div img {
  width: 100%;
  height: auto;
  min-width: 125px;
}

.image-slider  .wide-only {
  display: none;
}

@media(min-width:880px) {
  .image-slider .wide-only {
    display: block;
    flex: 1/* unsure about this value - tried display: flex too */
  }
}
<div class="image-slider">
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div class="wide-only"><img src="https://lorempixel.com/400/400/"></div>
</div>

Answer №2

For those looking to keep their HTML markup clean without adding extra classes, using the nth-child pseudo selector can achieve this goal while also ensuring maintainability.

The code snippet

.image-slider div:nth-child(n+5);
will target the 5th and subsequent child divs within the image slider.

Consider two sets of 'sliders'; one with 5 elements and another with 9. When the screen width is below 880px, only 4 elements will be displayed. Once the screen width exceeds that threshold, all elements show up.

This approach offers scalability as additional media queries can easily be added with higher nth-child values. For example, displaying 6 at 1024px or 8 at 1280px requires simple adjustments in CSS without having to modify both the HTML and the CSS files.

I've opted to use a max-width comparison in the media query for a mobile-first strategy, starting from smaller screens and progressively enhancing the layout on larger devices.

(Don't forget to view the snippet in full-screen mode)

.image-slider {
  display: flex;
  margin-bottom: 1rem;
}

.image-slider div {
  flex: 1;
  margin: 3px;
}

.image-slider div img {
  width: auto;
  max-width: 100%;
}

@media(max-width:879px) {
  .image-slider div:nth-child(n+5) {
    display: none;
  }
}
<div class="image-slider">
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
</div>


<div class="image-slider">
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
  <div><img src="https://lorempixel.com/400/400/"></div>
</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

Is there a way for me to automatically close other menus when the current menu is open using slideToggle?

Seeking assistance with enhancing the navigation functionality. How can I ensure that when a user clicks on a menu item, any other open menus are closed and the active class is removed from them? The current navigation setup meets my requirements except ...

Obtaining data with jQuery.Ajax technology

I am attempting to retrieve real-time data from a different URL and display it in a text field every second without refreshing the entire page. The content of the URL is constantly changing, so I want the field to update accordingly. However, despite my ef ...

The buttons mysteriously vanish when hovered over in Internet Explorer 11

I've encountered a strange issue with my website www.webfounded.com when viewed in Internet Explorer - all of my bootstrap buttons disappear on hover! Despite adding CSS classes for multiple browser compatibility and even attempting to remove the hove ...

Unable to update the <object/> element while transitioning from application/pdf to text/html

I am attempting to refresh a specific element within the DOM tree. Essentially, the TypeScript code is responsible for updating both the data and type attributes of an existing HTMLObjectElement. Here is a simplified version of the code: const textCanvas: ...

Utilizing jQuery for real-time operations

I am working with two input text fields and I am looking to utilize jQuery to perform an operation between them. Here are the input text fields: <p> <label>Title 1</label> <span class="field"> <input type="text" ...

Apply styling on hover with an additional class

I am attempting to apply a border to an input element when its pseudo class :hover is activated, while also adding the same border to the button next to it. I have read that using the selector class:hover + class should achieve this effect. However, in my ...

Create an HTML button with dual functionality

I currently have a button in my HTML that triggers the opening of the sidebar on mobile devices. <a href="#header" class="button scrolly">Contact Me</a> There is also a label element that, when clicked, selects the name field in the contact f ...

Accepting user input in a PHP for loop

Almost there, but I'm having trouble with a loop in my PHP code. The issue is that the code doesn't wait for user input and continues on. I've included my code below. The Excel spreadsheet starts at row 4 with multiple choices. Once a choice ...

Is there a way to hide my jQuery menu?

Can anyone help me figure out how to make it close when I click on a link? <nav class="navigation"> <a href="" class="menuIcon"><i class="fa fa-bars"></i></a> <ul class="nav"> <li class="clearfix"&g ...

Adjust the height of the embedded YouTube video iframe

I am trying to adjust the height of a YouTube video that is embedded in my webpage. After going through various threads, I found that adding an additional div outside the iframe element can solve this issue. However, the iframe element is already enclosed ...

Is the Spring MVC ModelAndView object being returned?

After clicking a link on a jsp page, I have a GET method that is instantiated as: HTML: <div class="panel accordion clearfix" id="dispdir"> <script type="text/javascript"> window.onload = function() { //showDirectorySe ...

Passing PHP values to JQuery is a common practice in web

I have a PHP file named userAuthentication.php containing a function that returns either "true" for successful authentication or "false" for a failure. In addition, I have a login view called login.html which includes fields for inputting username and pas ...

Can someone help me extract the text from the line directly beneath the <span> tag in HTML code, but only if it has a certain class and specific content

I am currently developing a script to extract product specifications from an e-commerce website. I have a list of URLs that lead to different products, and my goal is to scrape the specific specs required for each item. Although I have been using ParseHub ...

The title tag appears to be malfunctioning as it is being shown as regular paragraph text on the live

After a year of programming websites, I encountered a strange issue for the first time. The text from my title tag is appearing live on my website, allowing me to click on it and highlight it. Here is the markup I used: <!doctype html> <html> ...

Unable to link href attribute in project

My issue is that clicking the updates button does not load its page. Here's the relevant part of the code: <div class="st-container"> <input type="radio" name="radio-set" checked="checked" id="st-control-1"/> <a href="home.html ...

Creating aesthetically pleasing class names using SASS and CSS modules

I'm in the midst of working on a Next.js project and experimenting with SCSS/SASS for my styling needs. I'm currently grappling with how to streamline the process of applying class names. Here's the issue at hand: Within one of my componen ...

Hiding and displaying elements using jQuery for printed output

Is it possible to customize the show and hide properties of JQuery for different media types (e.g., screen and print)? For instance, I have two divs and a button that toggles between showing and hiding one div while displaying the other. However, when it ...

using generated divs to target a pseudoclass

I have designed a webpage that utilizes the CSS :target selector to determine which tab of information should be displayed. These tabs are generated dynamically through AJAX-loaded data when the page is initially loaded. Below is a snippet of code showcasi ...

Adjust the product percentage discount color in WooCommerce

I am struggling to switch the box color of the percentage discount for each product from green to red. I tried modifying the function I found on Reddit, but I couldn't get it right and it caused a fatal error, crashing the entire website. Here is the ...

Is there a way to set up HTML5 Video.js in Boonex Dolphin?

My current project involves utilizing the Video.js Source Code to update the video player on my website that currently uses Boonex Dolphin. I am looking to transition from the flash player used by Dolphin to HTML5. Dolphin's modules contain specific f ...