Skip to main content
Known Participant
July 8, 2022
Answered

Side By Side Images Responsive - 3 Columns Desktop to 1 Column on Mobile

  • July 8, 2022
  • 2 replies
  • 6036 views

I need html and css for a simple page that will have 3 images with text centered underneath each image, side by side on desktop, that goes to 1 column on mobile devices. So 3 columns to 1 column. Can anyone help? Has to be .php file to work with our site if that matters. I will use the existing header footer code for our site, and put this html code into the body. I looked at templates in Dreamweaver, and they were not helpful to me.

Thanks! 

    This topic has been closed for replies.
    Correct answer Nancy OShea

    I'm going to cut to the chase and use CSS GRIDS because it does exactly what you want without media queries.  And it will work with any number of items.

     

    <!doctype html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>CSS Grid Evenly Distributed Layout</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    body {
    width: 95%;
    margin: 0 auto;
    padding: 2%;
    }
    .grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    grid-auto-rows: minmax(150px, auto);
    grid-gap: 1em;
    border: 1px dotted silver;
    }
    .module {
    background: #eaeaea;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    }
    </style>
    </head>
    
    <body>
    <h3>Welcome to CSS Grids</h3>
    <div class="grid">
    <div class="module">1</div>
    <div class="module">2</div>
    <div class="module">3</div>
    <div class="module">4</div>
    <div class="module">5</div>
    <div class="module">6</div>
    <div class="module">7</div>
    <div class="module">8</div>
    <div class="module">9</div>
    <div class="module">10</div>
    <div class="module">11</div>
    <div class="module">12</div>
    <div class="module">13</div>
    <div class="module">14</div>
    <div class="module">15</div>
    </div>
    </body>
    </html>
    

     

    2 replies

    Nancy OShea
    Community Expert
    Community Expert
    July 8, 2022
    Nancy O'Shea— Product User & Community Expert
    Known Participant
    July 13, 2022

    Ok, here is my testing page - this goes from 3 columns to 1 column the way I want it to, but I want it to go to 2 columns before it goes to 1 (so I need to add an inbetween). I think it will go right above this code but not quite sure what to add, can you please help?

     

    /* Responsive layout - makes a one column-layout instead of two-column layout */
    @media (max-width: 800px) {
    .flex-container {
    flex-direction: column;
    }
    }

    Known Participant
    July 20, 2022
    quote

    so you would make the squares 350x350 instead of 175x175?

     


    By @acobbcarthagemills

     

    Yes.

     

    quote

     

    Wouldn't that only allow 2 columns on desktop rather than 4?


    By @acobbcarthagemills

     

     

    No, just set the images to the maximum width of their containers:

     

     

    .module img {
    max-width: 100%;

    height: auto;
    }

     

    Oh and remove the inline width from all the images, it is not needed:

    width="175"


    what do you think now?

    https://carthagemills.com/Geosynthetic-products-test2.php# 

    Nancy OShea
    Community Expert
    Community Expert
    July 8, 2022

    You have 3 options:

     

    Hope that helps.

     

    Nancy O'Shea— Product User & Community Expert