Create a stylish grid layout with perfectly centered content using CSS

I am in the process of developing a layout for print material on sticker labels. To achieve this, I have created a div element and applied the display:grid; property to customize the space allocation. My goal is to make item1, item2, and item3 overlap each other while being centered within their respective cells.

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: 70mm; // This may not be needed
  align-items: center;
}

.cell {
  border-style: dashed;
  width: 100mm;
  height: 70mm;
  align-content: center;
}

.item1 {
  z-index: 0;
  width: 40mm;
  height: 40mm;
  background-color: red;
  position: relative;
}

.item2 {
  z-index: 1;
  width: 20mm;
  height: 20mm;
  background-color: blue;
  position: relative;
}

.item2 {
  z-index: 2;
  width: 10mm;
  height: 10mm;
  background-color: green;
  position: relative;
}
<div class="grid">
  <div class="cell">
    <div class="item1"></div>
    <div class="item2"></div>
    <img class="item3" src="./src.png" />

  </div>
  <div class="cell">
    <div class="item1"></div>
    <img class="item2" src="./src.png" />
    <div class="item3"></div>
  </div>
  <div class="cell">
    <div class="item1"></div>
    <img class="item2" src="./src.png" />
    <div class="item3"></div>
  </div>
</div>

I am struggling to achieve the desired outcome: a grid with items perfectly centered both horizontally and vertically within each cell. The cell size must be precise (100mmx70mm) with a layout of 2 cells per row to fit the printed page.

If there is anyone with CSS expertise in the audience who can assist me, I would greatly appreciate it.

Answer №1

To achieve the desired layout, utilize absolute positioning along with translate transformations that are based on percentage values.

.grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: 70mm; // Appears unnecessary
  align-items: center;
}

.cell {
  border-style: dashed;
  width: 100mm;
  height: 70mm;
  align-content: center;
  position: relative;
}

.cell>*{
  position: absolute;
  left:50%;
  top: 50%;
  transform: translateX(-50%) translateY(-50%);
}

.item1 {
  z-index: 0;
  width: 40mm;
  height: 40mm;
  background-color: red;
}

.item2 {
  z-index: 1;
  width: 20mm;
  height: 20mm;
  background-color: blue;
}

.item2 {
  z-index: 2;
  width: 10mm;
  height: 10mm;
  background-color: green;
}
<div class="grid">
  <div class="cell">
    <div class="item1"></div>
    <div class="item2"></div>
    <img class="item3" src="./src.png" />

  </div>
  <div class="cell">
    <div class="item1"></div>
    <img class="item2" src="./src.png" />
    <div class="item3"></div>
  </div>
  <div class="cell">
    <div class="item1"></div>
    <img class="item2" src="./src.png" />
    <div class="item3"></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

Eliminate list items with a keyboard stroke

I am currently developing a straightforward todo list application using JavaScript. The main functionality I am trying to implement is the ability to add new items from an input field to a list, as well as the option to remove items from the list. While ...

Adjusting the widths of <input> and <select> elements

I am facing an issue with aligning an input text box under a select element, where the input is intended to add new items to the select. I want both elements to have the same width, but my attempts have not been successful (as I do not wish to specify diff ...

Utilizing CSS3 Columns to control the flow of elements and enforce breaks

Struggling to articulate my question, but I'll give it a shot :D Let's talk about the images: I'm having trouble explaining, I want to align the elements like in the first image, but all I'm getting is the second and third pictures. C ...

division of vertical space

Is there a way to ensure that the column containing bbb + ccc is the same height as the aaa and ddd columns? <html><body> <div style="width:500px;height:500px;background-color:yellow"> <div style="float:left;height:100%;background-co ...

Creating linear overlays in Tailwind CSS can be achieved by utilizing the `bg-gradient

Is there a way to add a linear background like this using Tailwind CSS without configuring it in tailwind.config.js? The image will be fetched from the backend, so I need the linear effect on the image from bottom to top with a soft background. Here are ...

Stack three DIVs vertically on the entire screen with a fixed page margin, ensuring no need for a scroll bar

I need help creating a layout with 3 DIVs stacked vertically, each taking up one third of the screen without causing a scroll-bar to appear. I also want an 8px margin around all three of them. Here's an image for reference... example image Ultimatel ...

How can we efficiently load a JSON file from a local path into an HTML document in a way that is compatible with all browsers?

I am attempting to retrieve JSON data from a local path and display it on a basic HTML page. I have tried various methods such as fetch, ajax in order to load the data and update the corresponding values on the web page. The goal is to host this page so t ...

Ways to showcase a collection of divs in a dual-column layout while minimizing any unnecessary vertical spacing

Is there a way to create two columns without changing the HTML structure? The content within each child element is dynamic, so there needs to be dynamic vertical spacing as well. I'm experimenting with different CSS properties for both the parent and ...

What is the best way to activate a JQ function with my submit button?

Is there a way to trigger a JQ function when clicking the submit button in a form? I managed to make Dreamweaver initiate an entire JS file, but not a particular function. ...

An easy way to attach a Contextmenu to a specific element

I have implemented a scrolling feature for one of the div elements in my Application. Inside this div, there is a templated table with over 100 rows. Users are able to add or delete rows using a contextMenu. The contextMenu offers 4 options - AddTop, AddB ...

Concealing Redundant Users in Entity Framework

I am faced with the task of sorting through a database containing over 4000 objects, many of which are duplicates. My goal is to create a new view that displays only one instance of each element based on the first and last name. I have already implemented ...

Is it possible to utilize Angular's structural directives within the innerHtml?

Can I insert HTML code with *ngIf and *ngFor directives using the innerHtml property of a DOM element? I'm confused about how Angular handles rendering, so I'm not sure how to accomplish this. I attempted to use Renderer2, but it didn't wor ...

HTML/ModX styling for selected textboxes

I am looking to enhance the style of my search-box, which is similar to the one on THIS website. While I have tried using :active, the effect only lasts for a brief moment. Currently, my code is heavily influenced by modX terms, but I will still share it h ...

utilizing symbols from a vector graphic icon库

There are countless icon images to be found on the internet. Recently, I came across this particular set and now I'm stumped on how to actually utilize them. Despite my efforts to find tutorials online, I have come up short in finding any helpful reso ...

Client Blocking Issue: ExpressJS encountering problem loading JavaScript file

I recently deployed my express app on a server (specifically, I used Heroku). Everything functions perfectly when I test the app locally. However, when I access the app online, I encounter an issue with a net::ERR_BLOCKED_BY_CLIENT error. This error states ...

When the mouse is clicked, the character fails to reach the intended destination or moves in the wrong direction on the HTML canvas

UPDATE: RESOLVED I am currently working on a game where the character moves by right-clicking. The character is meant to walk slowly, not teleport, towards the destination set by right-clicking on the canvas. However, I have encountered an issue where the ...

Unexpected labels continue to pop up in the footer of the HTML file

I have noticed that the following HTML always appears at the very bottom of the body tag in all my projects, regardless of whether I am using React.js or not. Interestingly, when I switch to an incognito browser, these tags disappear. Curiously, these sa ...

Unable to change the alignment to the left in the CSS styling

My table has th elements with a text-align: left property. I tried to override this with text-align: center in my CSS, but it's not working. Can anyone suggest a reason why? Here is my table: <table class="days_table" align="center"> <thea ...

Tips on Including Static Header and Footer in Multiple HTML Pages

What is the best way to reuse a header and footer on multiple HTML pages without repeating code? I have 5 HTML pages that all need to include the same header and footer elements. How can I efficiently reuse these components across all pages? ...

Unable to adjust the position of the logo image

I cannot figure out how to center the logo over a video background. Any suggestions on what I might be doing wrong? I've tried changing the "position" to relative and absolute, but without any success. #hero video { width:100%; height:100%; p ...