Is it possible for me to use an image as a grid item?

I have a grid with 3 elements as shown below:

Now, I am looking to replace the blue elements with images (they can be just placeholders).

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: minmax(30em, auto);
  justify-items: stretch;
  align-items: stretch;
}

.grid-item-1 {
  background: url("https://placeholder.com/image.jpg");
  background-size: cover;
  overflow: hidden;
}

.grid-item-2 {}

.grid-item-3 {
  grid-column: span 2;
  grid-row: span 1;
}

.grid-item {
  position: relative;
  font-size: 25px;
  padding: 20px;
  padding-top: 50px;
  background-color: #379AD6;
  color: #222;
  border: 1px solid white;
}
<div class="grid-container">
  <div class="grid-item grid-item-1">
  </div>
  <div class="grid-item grid-item-2">
  </div>
  <div class="grid-item grid-item-3"></div>
</div>

Answer №1

Through my experience of the last 7 years, it has become evident that images tend to glitch as grid or flex items.

This issue can typically be fixed by placing the image inside a container and making the container the grid or flex item instead.

While images are technically valid as grid or flex items, I recommend using a container for now until browsers make further advancements in resolving this problem.

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

The alignment of image and text next to each other is not functioning as intended

I've been attempting to display an image and text next to each other, but I'm encountering some issues. The current output looks like the figure below: https://i.stack.imgur.com/WxxrS.jpg The desired layout is as shown in the following link: ht ...

Tips for personalizing PNG images within an SVG element

Looking to update the color of a PNG image within an SVG tag. The PNG images I have are transparent, and I want to customize their colors based on user selections. How can this be achieved? Is there anyone out there who can assist me? <HoodieSvg ...

Looking for assistance with transferring a data attribute to a form redirection

I'm seeking assistance with a coding dilemma I've encountered. To provide some background, I have a list of items on my website, each featuring a 'Book Now' button that redirects users to different pages. Recently, I incorporated a mod ...

Switch the visibility of an HTML input styled as a "spinner"

I have managed to create a CSS-based method for toggling the visibility of span or input elements depending on whether a radio button is checked. I got inspired by this insightful question on Stack Overflow. However, I encountered an issue where this togg ...

Element remains hidden when position set to relative

I have created a panel inside a container class. The panel's CSS is as follows: .panel { width: 100%; background: url(img/launch1.png); height: 80%; background-size: cover; background-position: center; position: relative; ...

PHP: Dynamically update div content upon submission

I am attempting to update the "refresh" div after clicking the Submit button and also at regular intervals of 5 seconds. Despite looking through various resources, I have not been able to find a solution that meets my requirements. <script src="h ...

Efficiently input text box values into a canvas in real-time using HTML canvas and keypress

There are three text boxes labeled textbox1, textbox2, and textbox3. I am looking to transfer the values entered into these text boxes directly onto a canvas (cnv) whenever a user types in them, and remove the value from the canvas when it is deleted fro ...

What is the most effective method for dynamically showcasing buttons in AngularJS?

Hello all, I'm currently learning AngularJS and I was wondering if anyone could recommend the simplest and most effective method for dynamically displaying links from AngularJS to HTML. I am looking to display a variable number of buttons in my HTML ...

Search bar placeholder text appears off-center in Firefox browser

I have implemented a universal search bar on our website and it is working perfectly in Chrome and Safari. However, I am facing an issue with Firefox where the placeholder text is aligned to the bottom of the bar instead of the middle. Usually, placeholder ...

The Foolish Mistake: Undetermined Dollar Sign

Can someone please help me with this issue? I've searched everywhere but I can't seem to fix the "Uncaught ReferenceError: $ is not defined" error. I have rearranged my script, tried putting it at the bottom, but nothing seems to work. Any sugges ...

I am attempting to format an HTML form with proper indentation, but it seems to be eluding me

Struggling to design a custom registration form template, I'm facing an issue where paragraph spaces or indents are not being recognized by the program, even in the preview pane. Seeking assistance with this problem. I am attempting to position this ...

Are none of the page links clickable?

Currently, I am in the process of creating a portfolio website. This is my first attempt at coding HTML and CSS from scratch without the aid of a layout template. I've encountered an issue that has me stumped - the links within the container are not ...

Regular expression to identify items containing `href=""` within a specific set of tags

After using a Regex pattern to check for every href="" in my document, I now want it to specifically target all hrefs located between <a and </a> tags while still allowing other parameters within. Do not include in the match: <base href="http ...

When the page first loads, the slider is responsive, but it does not adjust

I installed the Moving Boxes plugin and customized it to create a full-width image slider. While it works well when the page loads, I'm facing challenges with making it responsive on resize. It appears that the JS script sets fixed widths upon load, w ...

Retrieving the following element

I am trying to retrieve the next sibling element of one that contains the text "Yes". Unfortunately, I can only use the text "Yes" and part of the id attribute, as the number (e.g.. 106) is not available. Due to this limitation, I am unable to directly acc ...

Placing the input-group-addon above the text-input for better positioning

Feeling overwhelmed trying to finalize a simple form due to CSS issues? Check out this image for a visual of the problem: https://i.stack.imgur.com/PgCmN.jpg The label is slightly off to the left, and the button appears larger than the input field. The i ...

Creating a bootstrap form field that spans 100% in width:

I am encountering an issue with two column divs in a row, each containing two text fields. When resizing on mobile, the width of the textbox is not expanding to 100% and looks unattractive. Below is my Bootstrap code, but strangely, the width looks fine f ...

What is the best way to incorporate tooltips in SVG?

My teacher wants me to display a tooltip on an SVG circle with links and information when we hover over it. They suggested using jQuery UI, but I've done extensive research and nothing has been able to assist me so far. ...

What is the best way to increase padding in a Vuetify view element?

I have been attempting to increase the padding of my view by utilizing the "px-5" class on the container within the view. However, I am struggling to achieve the desired amount of padding. What I am aiming for is padding similar to what is shown in this sc ...

A tutorial on dynamically adding fields with a dropdown list (populated from a database) and a text box using PHP and JQuery

I have multiple form components that I need to add dynamically, allowing users to add more than one. Firstly, there is a dropdown list with values populated from MySQL, followed by a text box for inquiries entry. The dropdown list displays a list of users, ...