What causes images to spill over in CSS Grid layouts?

For the layout, the first image should take up 3/4 or 2 columns of available space, while the remaining two images would be stacked vertically in the last column.

<div class="wrapper1">
<div class="one">
  <img  width="1200px"height="500" src="https://images.unsplash.com/photo-1459666644539-a9755287d6b0?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=279b60ab483a2610d1e7260dc213898c&auto=format&fit=crop&w=828&q=80" alt="pic1">
  </div>


<div class="two">
  <img height="300px" width="600px" src="https://images.unsplash.com/photo-1508182314998-3bd49473002f?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=af394daae4bdbc4a95dc2204984b790d&auto=format&fit=crop&w=1350&q=80" alt="pic2">
  </div>
<div class="three">
  <img  height="300px" width="600px"src="https://images.unsplash.com/photo-1516703914899-e8303d01329a?ixlib=rb-0.3.5&ixid=eyJhcH.../* this is the CSS styles */</p>

<pre><code>.wrapper1{
          display:grid;
          grid-column-gap:1px;
          grid-template-columns:repeat(3,1fr);
          grid-template-rows: 200px; 

        }

        .one{
         grid-column:1/3;

        grid-row: 1;

        }

        .two{
          grid-column: 3;
          grid-row:1;   

        }
        .three{
         grid-column:3;
          grid-row:2;
        }
        .wrapper2{
          display: grid;

        }

Answer №1

Your layout has a few issues to consider:

  1. The columns are set with repeat(3, 1fr), but the image widths are defined in the HTML, limiting the distribution of free space for the fr unit on most screens. This may cause images to overflow their grid item parents.

  2. You intend for the last two images to be stacked vertically, but the grid container only has one declared row (grid-template-rows: 200px), creating additional rows with default auto height.

  3. Although grid-column-gap: 1px is functioning, the issue arises as images exceed their parent grid items and cover the gap.

  4. To address this, you might need to override the defaults of min-width: auto / min-height: auto on grid items, preventing them from resizing smaller than their content size (images). For further information, refer to: Prevent content from expanding grid items

For more targeted advice, additional context about your goals would be helpful.

Visualize the code in action with this jsFiddle demo (borders added to show overflows).

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

Inquiring about browserify or webpack on a website using node - a few queries to consider

Seeking assistance with a website project I am currently working on. The project consists of 7 HTML documents, 3 stylesheets, 8 JavaScript files (including jQuery.min.js and some additional jQuery plugins), and various images. I am looking to bundle and mi ...

Initiating external libraries at the right time in Angular 4

I am currently experimenting with a UI kit (you can check it out here) that comes with multiple JavaScript files, jQuery, Bootstrap, and its own components. I have included them in my index.html file and everything works perfectly as long as the checkbox ...

Steps for achieving a seamless delay fade-in effect

Can anyone provide a solution to my issue? I'm a CSS beginner and need some help. My landing page has a background Youtube video with buttons that appear after 8 seconds. I want these buttons to smoothly fade in, but I can't figure out how to do ...

SCSS - Hide div1 when div2 is hovered over

In an attempt to make a div disappear when hovering over another div using only CSS, I have set up two divs: <div class="" id="target">I will disappear</div> <div class="hover_box">Hover me</div> Here is my SCSS code: #target { ...

Storing a Vue/JS element reference in a constant using Typescript

In my template, I have one form element and one button element: <button type="submit" id="ms_sign_in_submit" ref="submitButton" class="btn btn-lg btn-primary w-100 mb-5"> </button> Wi ...

Stopping link navigation with JavaScript

Can someone help with my link element issue? <a href="javascript:;" onclick="check_show('http://www.test.com')">Test</a> <script> function check_show(link){ if($('#testelement).css('display') == 'block& ...

What are the techniques for implementing an if statement in CSS or resolving it through JavaScript?

Demo available at the bottom of the page Within my code, there is a div called #myDiv that defaults to an opacity of 0. Upon hovering over #myDiv, the opacity changes to 1. See the CSS snippet below: #myDiv { opacity: 0; } #myDiv:hover { opacity: 1 ...

Simple method for swapping out an HTML string with image tags solely using JavaScript

In our Ionic 2 App, we are displaying content from a backend API in the form of Questions and Answers. The Answers are generated using summernote, allowing us to render raw HTML in the app. However, we need to replace all instances of img tags in the answe ...

What is the best way to align the button correctly beside the cluster of checkboxes within bootstrap framework?

Here is a snippet of HTML code that I am working with: <div class="container"> <form name ="queryForm"> <div class="form-group"> <p class="checkbox-inline"> <input type="checkbox" name="optionsRadios" id="checkOne" ...

Using JavaScript to manipulate CSS Keyframes

Can I control these animations with JS/JQuery instead of CSS hover action? .button { width: 350px; height: 300px; margin: 100px auto; position: relative; border: solid 2px #cbd4d9; -webkit-border-radius: 5px; -moz-border-radius: 5px; bor ...

Adjust the color as you scroll

It appears that the example provided changes the color from red to blue from the bottom, but I am looking for a solution that will change the color from top as I scroll. Can anyone help me achieve this effect using JavaScript? Here is another great referen ...

Issue with positioning Bootstrap tooltip on the right side causing it to not display correctly

Currently, the tooltip I have is functioning properly: However, my main requirement is to align it to the right. So, when I use .pull-right, it appears like this: This is the HTML code snippet: <div class="wrapper">Login <span rel= ...

The email width is exceeding 600px in all Outlook email clients

Fellow developers, I need your expertise! I've spent hours testing this email and researching why it's not conforming to a width of 600. Below is my code snippet and screenshots from Email on Acid - everything looks great except in Outlooks (exce ...

Align text columns to the right within flexible rows

I'm currently constructing a grid using flexbox, but I've encountered some border and padding issues leading me to devise the following HTML structure: https://jsfiddle.net/rqmdxcjo/ <div class="flex"> <div class="flex ...

The datepicker on mobile devices fails to display the default text of dd/mm/yyyy

This JSP code uses Bootstrap5: <form action="" method="GET" class="row g-1 mb-3 "> <div class="col-lg-2 col-md-4 col-6 mb-2"> <input type="date" class="form-control" name=" ...

Automatically press a button that appears on the webpage

I am looking to automate the clicking of a button that appears on a website. How can I accomplish this using Python? I have no experience in JavaScript and am fairly new to programming. Here is the outer HTML code for the button: <button type="button" ...

Implementing Background Video in Bootstrap 5 for a Stunning Hero Section

I have a unique template design that I am working on: https://i.sstatic.net/lLleK.png My goal is to replace the current background image with a video. Here is my attempt by adding a <video> tag: <!-- ======= Hero Section ======= --> <sec ...

Bootstrap Model Footer Button Alignment

click hereI'm facing an issue while adding buttons to the model footer. The buttons are not aligning in line, instead they move to the next line. <div class="modal-footer"> <form method="post" action="admin_view"> ...

Do the last block in the table occupy the remaining space in the row?

Trying to create an HTML email calendar and struggling with getting the last td to fill the remaining space without affecting the width of other rows. Is there a way to increase the width of just the 4th block on the last row to fill the empty space? Here ...

What is the correct placement of DESC in a MySQL query when using PHP?

Here's the issue I'm facing: $query = "SELECT * FROM table ORDER BY 'timestamp' LIMIT $startAt, $perPage"; I've been attempting to sort the results in descending order (latest on top) by adding DESC to the query. However, every t ...