Dynamic picture layout with a set height, showing one image on top and three below

I'm facing a challenge with displaying a grid of images on my site. The layout I have shows one image on top and three images below it, but I'm struggling to make it responsive. At smaller screen widths, everything looks fine, with the images aligned properly. However, when the screen size increases, the first image either gets distorted or there's a gap on the sides. I've included snippets and jsfiddles below along with my comments for reference.

Attempt One

/* Latest compiled and minified CSS included as External Resource*/

.container{
  max-height:200px!important;
}

.row{
  padding-bottom:10px;
}

/* Optional theme */
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>



<div class="container">
    <div class="row imagetiles">
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <img style="display: block; margin-left: auto; margin-right: auto;" src="https://scontent-sea1-1.cdninstagram.com/vp/f2630ab0809f8f1f942e76620914885e/5C885E3A/t51.2885-15/e35/c0.61.1080.1080/s480x480/44191803_684612728592256_2201772746335609632_n.jpg?ig_cache_key=MTg5MTYwNzQwNTMxODQyMTY0MA%3D%3D.2.c" class="img-responsive">
        </div>
    </div>
    <div class="row imagetiles">
        <div class="col-lg-3 col-md-4 col-sm-4 col-xs-4">
            <img src="https://scontent-sea1-1.cdninstagram.com/vp/f2630ab0809f8f1f942e76620914885e/5C885E3A/t51.2885-15/e35/c0.61.1080.1080/s480x480/44191803_684612728592256_2201772746335609632_n.jpg?ig_cache_key=MTg5MTYwNzQwNTMxODQyMTY0MA%3D%3D.2.c" class="img-responsive">
        </div>
        <div class="col-lg-3 col-md-4 col-sm-4 col-xs-4">
            <img src="https://scontent-sea1-1.cdninstagram.com/vp/f2630ab0809f8f1f942e76620914885e/5C885E3A/t51.2885-15/e35/c0.61.1080.1080/s480x480/44191803_684612728592256_2201772746335609632_n.jpg?ig_cache_key=MTg5MTYwNzQwNTMxODQyMTY0MA%3D%3D.2.c" class="img-responsive">
        </div>
        <div class="col-lg-3 col-md-4 col-sm-3 col-xs-4">
            <img src="https://scontent-sea1-1.cdninstagram.com/vp/f2630ab0809f8f1f942e76620914885e/5C885E3A/t51.2885-15/e35/c0.61.1080.1080/s480x480/44191803_684612728592256_2201772746335609632_n.jpg?ig_cache_key=MTg5MTYwNzQwNTMxODQyMTY0MA%3D%3D.2.c" class="img-responsive">
        </div>
    </div>
</div>

Attempt Two

/*scares away bad guys*/
* {padding:0;margin:0;}

#dashboard {
  display:flex;
/*height: 1024px; as specified in your original pen.
  width: 1024px;
*/
  min-width:100%;
  width: 100vmin;
  min-height:100%;
  min-height:98vmin; /* (-) offset by 2x AMOUNT*/
  height: 100%;
  height: 98vmin; /* (-) offset by 2X AMOUNT*/
  background-color:purple;/*visual dev*/
}

/*These two rules govern offset needed for whichever construct you choose to use first. In this example, only the .row is active, but if you chose to build the structure using cols (items 1,4,5 in col a, etc.) then the second rule would handle the same offset. Offset is 1x AMOUNT*/
#dashboard >.row{ padding:1vmin 0; }
#dashboard>.col { padding:0 1vmin; }

.row, .col{display:flex;}
.row{
  flex-direction: row;
  background-color:black;
}
.col{
  flex-direction:column;
  background-color:green;
}

.f1{flex:1;}
.f2{flex:2;}

.child{
  display: flex;
  flex:1;
  margin: 3px; /* <--- AMOUNT*/
  width:auto;
  height: 100%;

  /*CONTENT: TEXT*/
  font-weight: bold;
  font-size: 2em;
  text-align: center;
  color: white;
  
  background-color: grey;
  
  /*FOR DISPLAYING CONTENT CENTERED*/
  display: flex;
  justify-content: center;
  flex-direction: column;
  text-align: center;
}

/*UNCOMMENT THIS TO BEAUTIFY COLORS*/
body,#activecontainer,#dashboard,.row,.col{
/*   background-color:#293C4A; */
}

img{
  max-width: 100%;
  max-height:300px;
}

#dashboard{
  max-height:300px;
}
<div id="dashboard" class="col">
  <div class="row f2">
 
    <div class="col f2">
      <div class="child"><img src="https://scontent-sea1-1.cdninstagram.com/vp/c67d489394820ab5cf40af652a62e63c/5C5F6209/t51.2885-15/e35/c135.0.810.810/s480x480/43064777_242628609743355_5025062255919095941_n.jpg?ig_cache_key=MTg5MTU2Nzg3NzIzMzE5ODA3OA%3D%3D.2.c"></div>
    </div>
  </div>
</div>  
  <div class="row f1">
    <div class="child"><img src="https://scontent-sea1-1.cdninstagram.com/vp/c67d489394820ab5cf40af652a62e63c/5C5F6209/t51.2885-15/e35/c135.0.810.810/s480x480/43064777_242628609743355_5025062255919095941_n.jpg?ig_cache_key=MTg5MTU2Nzg3NzIzMzE5ODA3OA%3D%3D.2.c"></div>
    <div class="child"><img src="https://scontent-sea1-1.cdninstagram.com/vp/c67d489394820ab5cf40af652a62e63c/5C5F6209/t51.2885-15/e35/c135.0.810.810/s480x480/43064777_242628609743355_5025062255919095941_n.jpg?ig_cache_key=MTg5MTU2Nzg3NzIzMzE5ODA3OA%3D%3D.2.c"></div>
    <div class="child"><img src="https://scontent-sea1-1.cdninstagram.com/vp/c67d489394820ab5cf40af652a62e63c/5C5F6209/t51.2885-15/e35/c135.0.810.810/s480x480/43064777_242628609743355_5025062255919095941_n.jpg?ig_cache_key=MTg5MTU2Nzg3NzIzMzE5ODA3OA%3D%3D.2.c"></div>
  </div>

Jsfiddle

Check out a possible solution here

An ideal fix would involve maintaining the images' ratios and ensuring there's no margin or padding on the sides. Adjusting the spacing between the three images can also enhance the overall look of the layout.

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

Answer №1

Choice 1: To prevent images from stretching, use the cover property.

If you include these two lines in your upper img {} CSS section, everything should appear correctly:

.f2 img {
  object-fit: cover;
  object-position: 0 50%;
}

An issue with the object-fit property is its lack of support on older browsers (https://caniuse.com/#search=object-fit). Therefore, if you can replace <img> tags with <div> tags and set images as background images within these divs using background-size: cover;, it achieves the same effect but is fully supported.

Choice 2:

To maintain a square upper image that spans the full width, simply remove "display: flex" from the first .example-collection-grid class, and apply only width: 100% to that specific image. I tested this method in your fiddle, and it worked fine.

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

SNIPPET:

*{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box}html{height:100%}body{margin:0;min-height:100%;background-color:#fff;font-family:Arial,sans-serif;color:#323b40;font-size:16px;line-height:1.4em}img{max-width:100%;vertical-align:middle;display:inline-block}.example-body{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;margin-bottom:64px;-webkit-box-orient:vertical;-webkit-box-direction:normal;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-box-pack:start;-webkit-justify-content:flex-start;-ms-flex-pack:start;justify-content:flex-start;-webkit-flex-wrap:nowrap;-ms-flex-wrap:nowrap;flex-wrap:nowrap;-webkit-box-align:stretch;-webkit-align-items:stretch;-ms-flex-align:stretch;align-items:stretch;border-radius:6px;box-shadow:0 1px 3px 0 rgba(0,0,0,.1),0 6px 12px 0 rgba(0,0,0,.1)}.example-body.pink{background-color:#ec1561;color:#ec1561}.example-collection{min-height:560px;padding:40px 48px;-webkit-box-orient:vertical;-webkit-box-direction:normal;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-box-pack:start;-webkit-justify-content:flex-start;-ms-flex-pack:start;justify-content:flex-start;-webkit-flex-wrap:nowrap;-ms-flex-wrap:nowrap;flex-wrap:nowrap;-webkit-box-align:stretch;-webkit-align-items:stretch;-ms-flex-align:stretch;align-items:stretch;-webkit-align-content:center;-ms-flex-line-pack:center;align-content:center;color:#fff}.example-collection-grid,.example-collection-grid-first{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;margin-right:-12px;margin-left:-12px;-webkit-box-orient:horizontal;-webkit-box-direction:normal}.example-collection-grid-first{-webkit-flex-direction:row;-ms-flex-direction:row;flex-direction:row;-webkit-flex-wrap:wrap;-ms-flex-wrap:wrap;flex-wrap:wrap;-webkit-box-align:stretch;-webkit-align-items:stretch;-ms-flex-align:stretch;align-items:stretch;-webkit-box-flex:1;-webkit-flex:1 0 auto;-ms-flex:1 0 auto;flex:1 0 auto}.example-collection-grid-first img{width:100%}.example-collection-grid{display:flex;-webkit-flex-direction:row;-ms-flex-direction:row;flex-direction:row;-webkit-flex-wrap:wrap;-ms-flex-wrap:wrap;flex-wrap:wrap;-webkit-box-align:stretch;-webkit-align-items:stretch;-ms-flex-align:stretch;align-items:stretch;-webkit-box-flex:1;-webkit-flex:1 0 auto;-ms-flex:1 0 auto;flex:1 0 auto}.example-collection-grid.nested{margin-top:-12px;margin-bottom:-12px}.example-collection-cell{display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;padding:12px;-webkit-box-orient:vertical;-webkit-box-direction:normal;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;-webkit-box-pack:start;-webkit-justify-content:flex-start;-ms-flex-pack:start;justify-content:flex-start;-webkit-box-align:stretch;-webkit-align-items:stretch;-ms-flex-align:stretch;align-items:stretch;-webkit-box-flex:1;-webkit-flex:1;-ms-flex:1;flex:1}@media (max-width:991px){.example-collection{min-height:480px;font-size:14px;line-height:1.5em}}@media (max-width:767px){body{font-size:14px;line-height:1.4em}.example-body{margin-bottom:32px}.example-collection{min-height:320px;padding:18px 24px;font-size:12px;line-height:16px}.example-collection-grid{margin-right:-8px;margin-left:-8px}.example-collection-grid.nested{margin-top:-8px;margin-bottom:-8px}.example-collection-cell{padding:8px}}@media (max-width:479px){.example-body{margin-bottom:16px;border-radius:4px}.example-collection{min-height:240px;padding:12px;font-size:8px}.example-collection-grid{margin-right:-4px;margin-left:-4px}.example-collection-grid.nested{margin:-4px}.example-collection-cell{padding:4px}}
<div class="example-body pink">

                     <div class="col-xs-10">
                        <div>
                          Instagram
                        </div>
                        <div>
                          21/10/2018 - 31/10/2018
                        </div>

    <div class="example-collection">
        <div class="example-collection-grid-first">
            <div class="example-collection-cell">
                <img src="https://scontent-sea1-1.cdninstagram.com/vp/f2630ab0809f8f1f942e76620914885e/5C885E3A/t51.2885-15/e35/c0.61.1080.1080/s480x480/44191803_684612728592256_2201772746335609632_n.jpg?ig_cache_key=MTg5MTYwNzQwNTMxODQyMTY0MA%3D%3D.2.c">
            </div>
        </div>
        <div class="example-collection-grid">
            <div class="example-collection-cell">
                <div class="example-collection-grid nested">
                    <div class="example-collection-cell _1of3">
                        <img src="https://scontent-sea1-1.cdninstagram.com/vp/4af2e08b533e4d61f8316743e16f31b3/5C5E719E/t51.2885-15/e35/s480x480/42756110_553225941790401_6762481182286684009_n.jpg?ig_cache_key=MTg5MjMxODcxMDg2NjY3MTQ2OA%3D%3D.2">
                    </div>
                    <div class="example-collection-cell _1of3">
                        <img src="https://scontent-sea1-1.cdninstagram.com/vp/73a6d62a8839981de4eb4576a70ee364/5C5D459E/t51.2885-15/e35/c0.135.1080.1080/s480x480/43083237_636952050032613_3914252954595670906_n.jpg?ig_cache_key=MTg5MTY3MDA3NzQ5ODQ1NTI5OA%3D%3D.2.c">
                    </div>
                    <div class="example-collection-cell _1of3">
                        <img src="https://scontent-sea1-1.cdninstagram.com/vp/c67d489394820ab5cf40af652a62e63c/5C5F6209/t51.2885-15/e35/c135.0.810.810/s480x480/43064777_242628609743355_5025062255919095941_n.jpg?ig_cache_key=MTg5MTU2Nzg3NzIzMzE5ODA3OA%3D%3D.2.c">
                    </div>
                </div>
            </div>
        </div>
    </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

`Finding Solutions for jQuery and CSS Problems`

My jQuery slideDown/slideUp animation works smoothly in IE9 and Firefox, but is choppy in Chrome. When I remove the css file, the issue disappears, leading me to believe it is a Chrome and CSS interaction problem. Are there any tools available to help ide ...

Challenges with CSS dimensions

Hey everyone, I have a question that I've been struggling to find an answer to. Imagine you have something like this: <div className='container'> <div className='wrapper' style={{ width: 40, height: 40 ...

Handling duplicated cell numbers in the footerCallback

I am trying to implement footerCallback with a conditional based on another cell's value. I came across this question and replied to it: DataTables footerCallback - conditional on another cell. In my response, I mentioned that there is an issue with ...

What is the best way to display a logo on top of an image with absolute positioning?

My goal is to position my site's logo 50% from the top and 8% from the left. Take a look at what I have accomplished so far here. Any suggestions on how I can achieve this? <!DOCTYPE html> <html lang="en"> <head> <title>Boot ...

Issues with clickable links within a table cell

I am facing an issue with a table where rows <tr> are generated dynamically by a PHP loop. I have transformed the entire row into a link, but the problem arises when there is an <input type="checkbox"> within the row. Whenever I check the check ...

Is it possible to use a JavaScript string as a selector in jQuery?

So, my issue is with the following JavaScript code snippet: for ( i=0; i < parseInt(ids); i++){ var vst = '#'+String(img_arr[i]); var dst = '#'+String(div_arr[i]); } I'm wondering how I can proceed in jQuery to handle ...

Using rtl direction in Firefox does not function properly

Is there a way to create a select menu that opens from right to left? When I use Google Chrome, it appears correctly. https://i.sstatic.net/tgVvM.png However, when I use Firefox, it displays an error. https://i.sstatic.net/xys5v.png Below is the code ...

Passing a variable using a button's value attribute in jQuery

My goal is to develop a form in my application where users can input their name and address. Once the user enters the information, I need to validate it and add it to an array before submitting it to the PHP function. Additionally, I want to provide users ...

The text-center alignment in Bootstrap doesn't seem to be applying to buttons

After spending a considerable amount of time trying to center two buttons in Bootstrap, I came across the "text-center" class offered by Bootstrap. However, no matter where I include this class, it doesn't seem to have any effect on the alignment of t ...

Manipulate the value of the <input> element when focused through JavaScript

After I focus on the input field, I was expecting to see Bond-Patterson, but instead, I am only getting Bond. What could be causing this discrepancy and how can it be fixed? $('input[name="surname"]').attr("onfocus", "this.placeholder='Bo ...

ASP.NET's Div Element: A Fundamental Building Block

Is it possible to include a div element within the td element of a table? I have tried doing this but it does not appear in design mode on asp.net VWD. The syntax I used is: <td width="33%" id="tdselected" runat="server"> <div id="divSelect ...

Changing Scroll-bar Colors in JavaFX TableView Using Java

I modified the style of my TableViews programmatically with the following code- tableView.setStyle("-fx-base : #333333; -fx-background-color : gray"); Now I am looking to change the color of the scroll bar using the same method. I prefer not to add a ...

What is the process for altering the color of an HTML output depending on its values?

I created a simple HTML code to showcase some outcomes. The possible results are SUCCESS, Failure, and Still Failing. I want these results to be displayed with corresponding colors, such as green for SUCCESS, and red for both Failure and Still Failing. I ...

Is it possible to customize the appearance of ordered list numbers using javascript?

Is it possible to change the color of a specific line number in an ordered list using JavaScript? I am familiar with CSS styling such as li::marker, but curious about altering individual list item markers dynamically through JavaScript. How can this be ach ...

Simple method to open a website on Android using a shortcut icon

Our website provides e-mail services and we are looking to develop a mobile app for it. However, our budget does not allow for this at the moment. In the meantime, we are interested in creating an icon on users' Android phones that can direct them to ...

Excessive content and the upper limit of height

I'm in the process of creating a scrolling table with a maximum height, and here's what I've done so far: <table> <tbody style="height: 300px; overflow:auto;"> //php for loop to populate table with <tr>s/<td>s </t ...

What is the reason for index always being not equal to 0?

<script> var index = 0; $.getJSON("./data/data.json", function(json) { $.each(json, function(data) { var html = ""; if(index == 0) { console.log(index + " fir ...

Display a webpage with updated information transmitted from Angular to Flask

I am currently working on updating my data on a page when the date changes. I am using an AngularJS date picker and Flask on the backend to achieve this functionality. However, after passing the changed date from Angular to Flask, I am able to generate the ...

Deactivate dropdown menu selection depending on choice made in another list

I am currently working with the following code: $('select').on('change', function(){ $('select option').prop("disabled", false); $("select").not(this).find("option[value="+ $(this).val() + "]").prop('disabled ...

HTTrack displays an error message indicating that the file cannot be located

After downloading a website using HTTrack with the command below: /usr/local/bin/httrack https://www.website.com -O /Users/mainuser/Desktop/website -n -j I located the index.html file in the website folder and attempted to run it. However, Chrome display ...