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;
    }
    }

    Legend
    July 20, 2022
    quote

     

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

    width="175"


    By @osgood_

     

    According to MozillaWhile developer best practices from the last decade may have recommended omitting the width and height attributes of an image on an HTML <img>, due to aspect ratio mapping, including these two attributes is considered a developer best practice.


    These days best practices are largely irrelevant and can be taken with a grain of salt. Not so long ago it was considered bad practice to use more code than is necessary, what happened to that one may question. Everyone just makes it up according to what they feel is best practices these days and the workflow they choose to use. 

     

    According to you one should use a heading tag in a section tag but that's not specifically what the spec says. It says where possible, so what appears to be good practice to one developers is not always shared by another, that's the ugly side of web development.

    Nancy OShea
    Community Expert
    Community Expert
    July 8, 2022

    You have 3 options:

     

    Hope that helps.

     

    Nancy O'Shea— Product User & Community Expert