Shifting the course of a Flexbox in HTML CSS

My current setup includes a flexbox with 4 images. However, when I decrease the width, the 4th image disappears. You can see the issue in the image below. https://i.sstatic.net/A7xFi.png

I want the flexbox to change into the layout shown below when the width reaches a certain size.

https://i.sstatic.net/YQ3C5.png

I've been struggling to achieve this transformation. I attempted to use flex-direction: column;, but that made the entire list vertical which is not what I intended.

div{
  display: flex;
  flex-direction: wrap;
  border-style:solid;
  padding-top: 2em;
}

figcaption[certification=yes]::before {
            font-size: 0.75em;
            /*Check mark*/
            content: "\2705";
            display: inline-block;
            color:red;
}

figcaption[certification=yes]::after {
            font-size: 0.75em;
            /*Dispaly li*/
            content: li;
            display: inline-block;
            color:red;
}

figure{
    padding:2.5em;
    margin-left: 3em;
    margin-bottom:2em;
}

@media screen and (max-width: 1185px)
{
    // I want the 4th image to be below the 1st image once the width is 1185px
    div{ 
        flex-direction: column;
    }
    
}
<div>
        <figure>
            <img src="images/ACE.png" height="150em">
            <figcaption data-title="Multimedia and the World Wide Web" certification="yes">Nighthawk</figcaption>
        </figure>

        <figure>
            <img src="images/NSCA.png" height="150em">
            <figcaption data-title="Multimedia and the World Wide Web" certification="yes">Nighthawk</figcaption>
        </figure>

        <figure>
            <img src="images/USA.png" height="150em">
            <figcaption data-title="Multimedia and the World Wide Web" certification="yes">Nighthawk</figcaption>
        </figure>

        <figure>
            <img src="images/Functional-movement.jpg" height="150em">
            <figcaption data-title="Multimedia and the World Wide Web" certification="yes">Nighthawk</figcaption>
        </figure>
    </div>

Answer №1

Make sure to set flex-wrap:wrap;

Consider using vw and vh (viewport units) instead of em, px, or rem. Viewport units are more versatile as they represent percentages of the current screen width and height, adjusting seamlessly with different screen sizes. Unlike percentages, which relate to parent div size, viewport units respond directly to the viewport size.

For more information, check out these resources:


Demo:

div{display:flex;flex-wrap:wrap;border-style:solid;padding-top:2em;}

figcaption[certification=yes]::before{font-size:0.75em;/*Check mark*/content:"\2705";display:inline-block;color:red;}
figcaption[certification=yes]::after{font-size:0.75em;/*Dispaly li*/content:li;display:inline-block;color:red;}
figure{padding:2.5em;margin-left:3em;margin-bottom:2em;}

@media screen and (max-width:1185px)
{// I want the 4th image to below the 1st image once you get to the width of 1185px
    div{flex-wrap:wrap;}
}
<div>
        <figure>
            <img src="images/ACE.png" height="150em">
            <figcaption data-title="Multimedia and the World Wide Web" certification="yes">Nighthawk</figcaption>
        </figure>
  
        <figure>
            <img src="images/NSCA.png" height="150em">
            <figcaption data-title="Multimedia and the World Wide Web" certification="yes">Nighthawk</figcaption>
        </figure>
   
        <figure>
            <img src="images/USA.png" height="150em">
            <figcaption data-title="Multimedia and the World Wide Web" certification="yes">Nighthawk</figcaption>
        </figure> 
  
        <figure>
            <img src="images/Functional-movement.jpg" height="150em">
            <figcaption data-title="Multimedia and the World Wide Web" certification="yes">Nighthawk</figcaption>
        </figure>
    </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

Multer returns an undefined response

I am currently utilizing multer in my Node.js application to handle file uploads, along with AngularJS for the front end. However, when I click on the upload image button, I'm getting an undefined response in the console. Can someone assist me in reso ...

Determine the specific cell involved in an HTML5 drag-and-drop interaction within a table

I've been experimenting with the HTML5 drag and drop functionality in an Angular project. Here's the setup I'm working with: A container containing draggable 'objects' A table where users can drop the dragged elements Following ...

Shift the content downwards within an 'a' snippet located in the sidebar when it reaches the edge of the right border

I'm having an issue with my style.css file. As a beginner in programming, I am trying to position text next to an image attached in the sidebar. However, the text is overflowing past the border. Here's the HTML code: Please see this first to ide ...

Troubleshooting a MySQL query in PHP to insert data into a database

I am currently facing an issue with my code. I have a form on my website with three fields - first name, last name, and email. The data from these fields is supposed to be added to a MySQL database upon submission. However, when I test the form and click ...

Json data integrated dropdown menu

I have successfully retrieved data from a Json array and displayed it in a dropdown list. However, I am facing an issue where I want to only show the name of the city instead of both the name and description. If I remove cities[i]['description'], ...

Bootstrap badge is encountering loading issues

My index.html isn't displaying the badge correctly on any browser. The numbers aren't showing up in a colored circle, it looks like the CSS didn't load properly. I've tried using both local and CDN paths for bootstrap and jquery, but t ...

Exploring off-canvas mobile navigation with a focus on subtle impacts

Seeking a Solution I am in search of a JavaScript (jQuery) library that offers off-canvas navigation specifically designed for mobile devices. The library should provide a native-like user experience, have minimal impact on the existing page structure, an ...

Easily accessible button and span located on the right side

One issue I'm facing is that the button and span are not aligned on the same line. Is there a way to resolve this? <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/c ...

Individual ".htaccess" and ".htpasswd" files are created for every webpage

I have a main page with links to 3 other pages (all within the same folder) named: content1.html, content2.html, and content3.html. <html> <body> <ul> <li><a href="content1.htm">content1</a></li> <li> ...

Binding textarea data in Angular is a powerful feature that allows

I am looking to display the content from a textarea on the page in real time, but I am struggling to get the line breaks to show up. Here is my current code snippet: app.component.html <div class="ui center aligned grid">{{Form.value.address}}< ...

Issue with manipulating background color with JQuery in CSS

Currently, I have a simple navigation menu consisting of five links. The navigation bar is styled with a background image and changes to a darker shade when hovered over. My goal is to implement a jQuery script that dynamically changes the color of the act ...

the script is run only one time

I have developed a unique web component and incorporated it in two distinct sections like this. <div class="sub_container"> <label>Users in Debt</label> <input placeholder="Search by user name" class="input_style ...

Create a nested table with a table-layout set to auto and a width of 100%, allowing for overflow when the window

How can I achieve a table with table-layout: auto and a 100% width that overflows horizontally when the window is too narrow without showing a scrollbar? Consider these examples: WORKING EXAMPLE 1 <div style="width: 100%; overflow-x: auto"> &l ...

The div element is just a tad smaller in size (0.085 smaller as per the CSS calculation)

I have a div with fixed height and width of 24px with a background image, but the browser is cropping off 0.1 pixels... div { width: 24px; height: 24px; background: url(svg); } Upon inspecting the computed styles, I noticed that the height is actua ...

The parent of these inline-blocks does not adjust its width to match its children, even when the parent has overflow-hidden applied

This is the code snippet I have been working with: <!DOCTYPE html> <html> <head> <style> div.ex2 { background-color: lightblue; width: 110px; height: 110px; overflow: ...

Modifying button appearance upon clicking using JavaScript

JavaScript: const buttons = document.getElementsByClassName("togglebtn"); for (let i = 0; i < buttons.length; i++) { buttons[i].onclick = function () { this.classList.toggle("active"); } } html: <md-butt ...

Stop any accidental double clicks on the <input type="submit" /> to avoid duplicate entries

Is it possible to prevent a user from clicking twice on an input tag button with type submit? I attempted using ondblclick="javascript:void(0)", but it did not work. My code is within an Html.BeginForm, not a form element tag. Thank you in advance for al ...

Tips for utilizing data attribute in jQuery programming

How can I modify my jQuery code to select the data attribute "data-price" instead of the value attribute? Sample HTML code: <span id="priceToSwap3"">$238</span> <select id="trapsize"> <option data-price="238">item1</option> ...

QWebEngineView - handling content larger than 2 megabytes

While working with PyQt5's QWebEngineView, I encountered a 2 MB size limitation when using the .setHTML and .setContent methods. After researching potential solutions, I came across two approaches: One option is to use SimpleHTTPServer to serve the f ...

Why isn't the comparison between the data returned by Ajax and the data in the text field functioning properly?

When the Ajax call returns data, it will provide user records in the following format: Array(6) 0:{_id: 1, provider: "abc", time: null, email_id: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4726252407202a262e2b6924282a"&g ...