Tips for aligning fixed-size responsive tiles in the center

I'm looking to create a page filled with fixed-size tiles that cover the entire page, ensuring that as many tiles as possible are displayed in one row. If the tiles can't fit without horizontal scrolling, they should be pushed to the next row.

To achieve this layout, I used CSS properties like display: grid and

grid-template-columns: repeat(auto-fit, 210px)
on the container element. However, I ran into an issue where if the window width is not a multiple of the tile's width, there is empty space on the right side of the screen.

I want to center these tiles on the page, but simply adding auto margins or padding did not work. Can you suggest a straightforward way to accomplish this using only CSS?


Here's the code for the page:

<!DOCTYPE html>
<html lang="en">
<head>
<title>Page</title>
<style>
body {
  text-align: center;
}
main {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: repeat(auto-fit, 210px);
}
section {
  padding: 5px;
  background-color: #F3F3F3;
}
</style>
</head>
<body>
<main>
  <section>Tile 01</section>
  <section>Tile 02</section>
  ...
  <section>Tile 16</section>
</main>
</body>
</html>

Screenshots:

Tiles with ideal window width: https://i.sstatic.net/3m5JZ.png

Narrow window width without centering: https://i.sstatic.net/6ALZk.png

Desired centering with narrow window: https://i.sstatic.net/LAJII.png Done with margin: 0px 9% 0px 9%, margin: 0px auto 0px auto doesn't work.

Answer №1

justify-content:center; could be the solution for your situation:

When it comes to CSS, the justify-content property is key in determining how space is allocated between content items on the main axis within a flex container, as well as the inline axis of a grid container.

body {
  text-align: center;
}
main {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: repeat(auto-fit, 210px);
  justify-content:center;
}
section {
  padding: 5px;
  background-color: #F3F3F3;
}
<main>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  <section>Tile </section>
  // Additional sections omitted for brevity
</main>

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

Is it possible to add data in MongoDB without specifying a field name?

I have a couple of queries that revolve around the same concept: If I want to insert a new 'row' in MongoDB, can I do so by specifying the order of the fields? For instance, if my collection looks like items = { { name: "John", age: "28" ...

Heroku NodeJS - Page Not Found: Error 404

I recently set up a Heroku server with BootBot running on it, but I'm facing challenges while trying to render an HTML page from it. Here is what I have in my code: var app = express(); var path = require('path'); app.use(express.static(pat ...

Remove the gray border from anchor elements when they are in focus

I'm struggling to remove the unattractive grey border that surrounds anchor tags. The CSS property outline:none; eliminates it in Firefox, but how can I achieve the same effect in IE? Ideally using CSS expressions or jQuery. Accessibility is not a con ...

How can I make rows appear dynamically when the user clicks a button?

I am trying to add rows dynamically when the user clicks on a button. I have created a script for this purpose, but unfortunately, it is not working as expected. Can someone please assist me with fixing it? <script> var i; i = 2; function AddR ...

Bug in canvas rendering for Chrome versions 94 and 95

The Canvas drawing functionality in the Chrome browser seems to have some visible bugs. Here's an example of code that demonstrates this issue: const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d&apo ...

Using Google Maps within a Bootstrap modal window that is only partially loaded

The maps display perfectly fine in a regular div, but when placed inside a Bootstrap modal, only half or part of the map is visible. However, when I open the console, the issue resolves itself and the entire map is shown. Photo without console opened Pho ...

Trouble with Angular: mat-sidenav not responding to autosize or mode changes when resized

Currently, I am working on integrating the mat-sidenav into a project. Most of the functionality is working well, but there is one particular issue that arises. When running the app locally in a browser and resizing the window tab multiple times, it opens ...

What are the steps to create a navbar positioned below an image?

I have my navbar on another page, but I would like to center it directly under the logo or image. My align-items property doesn't seem to work, so I've had to set the height in the .logo class. Is there any other method I can use to achieve this? ...

Select only the immediate descendants of a div that are not images

Can we select only the immediate children of a div excluding images? Is there a way to achieve this using the :not-selector or any similar method? For example: http://codepen.io/anon/pen/hlrAg Incorporating a media query, I want to apply left/right-padd ...

Using HTML5 to implement a progress bar that updates on click

I currently have an HTML file that contains clickable content. <tr onclick="callfn('Afile.jsp?param1=<%= value1%>');" id="contenttr"> The function callfn is located in a JavaScript file that loads images. Because this function can t ...

Having trouble transferring my background image from my FTP to my website

I'm having trouble setting a background image for the header on my website. It seems that nothing is loading. The hosting service I use has disabled linking images via external URLs, so I tried uploading the image to Filezilla, but that didn't wo ...

Having trouble converting customized tabs into Bootstrap navigation tabs?

Creating custom tabs with unique tab logic <!-- <lntds-tabs [selectedIndex]="selectedTabIndex" (selectedTabChange)="tabChanged($event)"> --> <!-- <lntds-tab class="custom-tab-group lntdstabsco ...

Allow the content to be scrolled

When it comes to my CSS script, I encounter no issues on larger computer screens. However, users with smaller resolutions like 1024 x 768 are experiencing problems viewing the entire HTML page. Only half of the page is displayed, making it impossible for t ...

Exploring the best practices for utilizing the error prop and CSS with the Input API in Material UI while utilizing context

When working with the MUI Input API props and CSS, I encountered an issue related to the {error} and its use. This is how my code looks: const [value, setValue] = useState<string>(cell.value); const [startAdornment, setStartAdornment] = useState< ...

Discover how to access the translations of a specific key in a chosen language by utilizing the angular $translate functionality

How can I retrieve a specific language translation using angular's $translate function within a controller? The $translate.instant(KEY) method returns the translation of the specified key based on the currently selected language. What I am looking for ...

What is the best way to ensure uniform height for all div elements using JavaScript?

I'm working on creating a tool that can find the tallest element among a group of elements and then adjust the height of all elements to match the tallest one. Within this tool, I'm attempting to pass a selector to a JQuery method that is locate ...

Loading images efficiently using JavaScript

While working on my JavaScript exercises, I encountered something strange with the image slider script I was creating. When trying to change the image source using the 'setAttribute' method in the HTML code, I noticed that the source of the imag ...

Dynamic navigation menu with Bootstrap's responsive multi-level design

I recently developed a multi-level navigation menu using Bootstrap. Everything seems to be working smoothly when the screen size is 1200px or above. However, I encountered an issue with the second level dropdown menu not opening upon clicking on screens sm ...

Dynamic grid arrangement showcasing cells of varying heights

I am dealing with a grid layout where cells are dynamically generated using the following code: <style> .calWrapper{ width:200px; float:left; } </style> <div class="container"> <?php for($i=1; $i<=12; $i++){ echo ...

The error message "Angular formGroup requires a FormGroup instance. Kindly provide one."

I used angular reactiveforms to create a form. The default data is successfully printed on the form, however, I am facing an error in the console that says "formGroup expects a FormGroup instance. Please pass one in." Can someone guide me on how to resolve ...