What is the best way to add a div to the bottom of a grid layout?

Here is the code I am working with:

.my-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  grid-gap: 20px;
}

.item {
  background-color: #ddd;
  height: 300px;
}
<div class="my-grid">
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item"></div>
</div>
<div class="new-item">hello!</div>

This is how it appears in my testing environment:

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

You may notice that the hello text is displaying on a new row.

I am wondering if there is a way to insert the "hello" item after the existing grid instead of on a separate row. Basically, I want it to be the last item in the grid and aligned with the others.

The obvious solution would be to make the new-item div a child of my-grid, but unfortunately, I cannot do this because my-grid is an external component from the internet and I cannot modify its structure.

Both elements share a common parent, which could be any other element.

Answer №1

If you have elements that share the same container, you may want to consider using display: contents:

/* transfer all the styles to container 
   in the near future you can do .container:has(.my-grid) {}
*/
.container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  grid-gap: 20px;
}

.item {
  background-color: #ddd;
  height: 300px;
}

/* we remove "my-grid" div*/
.my-grid {
  display: contents; 
}
<div class="container">
  <div class="my-grid">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
  </div>
  <div class="new-item">hello!</div>
</div>

Answer №2

To achieve a specific position for "new-item", ensure the placement of an absolute position while having a div container positioned above "my-grid". Remember to set "position: relative" on this container to adjust the left and top positioning of "new-item" in relation to the main div container. Keep in mind that this may cause issues with varying screen resolutions, so utilize media queries to address any inconsistencies.

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

Exploring the implementation of Javascript, HTML, PHP, and Ajax in programming

<div class="navigation"> <a class="prev" href="index.php?month=__prev_month__" onclick="$('#calendar').load('index.php?month=__prev_month__&_r=' + Math.random()); return false;"></a> <!-- <div class="title" & ...

What is the best method for freezing an HTML element in relation to a container using either CSS or JavaScript?

I've been diligently researching this topic up until the final day, but unfortunately, I have yet to find a solution. I am in dire need of assistance with my project. Could someone please help me out? One of my related searches led me to Can I positi ...

The HTML button failed to toggle on in Div's previous attempt

Check out this JSFiddle link I'm trying to hide a div when the page loads and then show it when a button is clicked, but for some reason it's not working properly. Any suggestions on how to fix this issue? Here's the HTML code I'm usi ...

Is there a way to convert HTML into a structured DOM tree while considering its original source location?

I am currently developing a user script that is designed to operate on https://example.net. This script executes fetch requests for HTML documents from https://example.com, with the intention of parsing them into HTML DOM trees. The challenge I face arise ...

Several HTML dropdown menus that do not expand

On my website, I have several select elements but the first one related to math is giving me trouble. It doesn't expand properly - sometimes it works on the first click but stops working afterwards. I've tested this issue on both Google Chrome a ...

Enlarging and cutting video footage

I have developed an app that features a code enabling users to capture photos using their computer-connected camera. How it Works: A webcam is utilized to display a video element with the camera as its source: function onSuccess (stream) { this.video.s ...

Issue with displaying the glyphicon-chevron on the carousel control

Currently, I'm in the process of incorporating a straightforward modal into my gallery design. I have a slide carousel positioned at the top of the page, while at the bottom, there are thumbnails of the gallery. While the slide carousel control butto ...

The CSS classes from Bootstrap are failing to function properly despite my best efforts in

I have been experiencing issues with the row and col classes in Bootstrap. <!DOCTYPE html> <html lang="en> <head> <meta charset="utf-8> <meta http-equiv="X-UA-Compatible" content="IE-edge> <meta name="viewport" content=" ...

Problem: The variable "$" is not defined in angular datatables causing a ReferenceError

Trying to implement Paging and sorting in my table, but encountered an error even after following all the steps mentioned here: . Tried troubleshooting the issue with no success. Ensured that all dependencies were installed properly. Below is the compo ...

What is the best way to conceal part of a chart or a div in Angular?

I have a large chart that I need to showcase on my website, but due to its size it would take up too much vertical space and potentially overwhelm the user upon their first visit. My goal is to provide a preview of the chart, similar to an excerpt of text ...

Missing CSS in dynamically loaded content on IE

I have been searching everywhere, but I just can't seem to find a satisfactory answer to this particular question. Whenever I load a few items on the page, they are displayed correctly at first. However, if a user decides to change the language, the ...

Executing a Python script within an HTML page using Django and extracting a variable from an input form

I have created a sentiment analysis Python script, but now I want to integrate it with an HTML page. The goal is to take input from the HTML form, process it using the Python script, and then display the results back on the HTML page. Currently, I am usin ...

Knitting: exporting to HTML file while retaining formatting

Recently discovered the amazing knitr library in R, which looks great when viewed in the viewer. But unfortunately, the style gets lost when saved to an html file. Code library(knitr) library(kableExtra) some.table <- data.frame ( x = rep(1 ...

Adding a <tr> tag to an HTML table using JQuery and AJAX in the context of Django framework step by step

I am currently navigating the world of Javascript, Jquery, and Ajax requests and I'm encountering a challenge with how my scripts are executing. My homepage contains a lengthy list of items (over 1200) that need to be displayed. Previously, I loaded ...

Setting the selected option in a Razor HTML Select/Option after a postback

Currently, I have a HTML select element with 3 options. My goal is to specify which option should be selected after a post back on my form. The technologies I am using include Razor (C#), HTML, jQuery, and CSS. If possible, I would like to achieve this ...

Using JavaScript to store CSS style properties in an array

In the midst of building a React-datagrid project, I am streamlining CSS properties for an array. However, there seems to be redundant CSS properties that I would like to consolidate into an array format. let _columns = []; let commonStyle = {"width":"20 ...

Utilize JavaScript to eliminate tags surrounding a text node

If my HTML code looks something like this: <div id="text"> This is some text that has an area <span class="highlight">highlighted with different formatting</span> and then some more text. </div> I am trying to figure ...

Display excerpts of code within an Angular application within the HTML segment

I'm currently developing a tutorial page using Angular where I intend to display code snippets in various programming languages such as 'Java', 'TypeScript', 'C#', and 'Html'. Although there is a snippet called ...

If there is no content present, the html element with contenteditable will have an <br>

I'm encountering an issue with a <div contenteditable="true"></div>. Whenever I enter content into it and then delete that content, the browser seems to automatically insert a <br> element into the div. Has anyone else experienced th ...

JavaScript tip: Improve the way you highlight the current navigation page while scrolling by finding alternative methods to using "scrollY > x"

Currently, my webpage layout is divided into 4 sections - HOME, ABOUT, SKILLS, and CONTACT. Below is the JavaScript code I am using to highlight a specific section on the navigation bar based on the scroll position: let home = document.querySelector(" ...