Tips for Aligning Divs in the Center of the Grid

I recently created a grid layout that adjusts responsively and stacks as the page size shrinks.

Even though it works well, I am having trouble centering three divs within the layout. The alignment seems slightly off when the page is fully expanded.

If you want to take a look at my layout, check out

Thank you for your help!

Answer №1

There are several ways to achieve this, one option is using the display:inline method:

div.Box {
    width: 20%;
    height: 250px;
    background: red;
    display: inline-block;
}

#Container {
  text-align: center;
}

JSFiddle (using percent width)

JSFiddle (using px width)

div.Box {
    width: 200px;
    height: 250px;
    background: red;
    display: inline-block;
}

Another approach is to set float: left; for div.Box and then specify a fixed width for the parent element with margin: auto to center it.

JSFiddle

div.Box {
    width: 200px;
    height: 250px;
    background: red;
    float: left;
    margin: 0 5px 0 5px;
}

If you want to make it responsive, you can use media queries for different screen resolutions.

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

Developing a trivia game using HTML and JavaScript

I'm in need of some serious assistance with creating a quiz using HTML. My goal is to have a web page where users can visit, take a quiz, and have their responses stored. Unfortunately, I don't have the knowledge or skills required to do this on ...

Easily input new information into your MySQL database with just a click by utilizing a link through PHP and HTML

Is there a method to modify information in a MySQL database when a user clicks on a hyperlink? I'm developing a voting application. It enables users to save questions for immediate use or later use. When the user is viewing their created questions, t ...

Utilize AngularJS to enable a CSS Class through an array order

I need to activate a heartbeat CSS animation on 6 different divs, but I want to do it in a specific sequence. For instance, let's say I have an array: self.arraySequence = [0,3,5,3]; In this array, each number represents the position of the div tha ...

Is there a way to eliminate the black border on my CSS/HTML using Bootstrap?

First and foremost, I want to clarify that the border does not appear when I test it in VSCode on my local machine's server, however, hosting with NameCheap reveals it. This is what I am attempting to eliminate: Dropdown Menu Item with Border Upon i ...

Organizing semantic elements at the beginning of a column

I am facing a challenge with formatting a long text into 2 columns while also including a fixed height table in one of the columns. The goal is to have the table at the top of the 2nd column without splitting the text or using JavaScript. The table shoul ...

Utilizing Angular's CdkDragDrop feature within a table cell to drag items across multiple rows

I am currently working on implementing a table in Angular 7 that contains a list within one of the columns, and I want to enable users to drag and drop items from this list to different rows. I have been attempting to utilize CdkDragDrop within a table st ...

Utilizing data binding in AngularJS upon choosing an item from a drop-down menu

I'm new to AngularJS and would appreciate any help on how to accomplish the following task: When I select an option from a dropdown list, I want the relevant information to be displayed in readonly fields. Here is an attachment for reference: https:// ...

What is the best way to customize a button component's className when importing it into another component?

Looking to customize a button based on the specific page it's imported on? Let's dive into my button component code: import React from "react"; import "./Button.css"; export interface Props { // List of props here } // Button component def ...

Troubleshooting: Node.js with Express - Issue with Loading Static JavaScript File in HTML

Despite trying numerous tutorials and following recommendations from various sources, my issue remains unresolved. I am attempting to create a basic static website using node.js and express to leverage server-side functionalities like file system access, d ...

The margin-bottom attribute does not display any vacant area

I recently attempted to center the inner box within the outer box in this codepen. To achieve this, I utilized a workaround by applying margin-bottom: 20px;. However, it appears that this approach did not generate any visible space between the bottom line ...

Cycle through items and switch states using classList in JavaScript

Instructions: Utilize javascript and the classList property to change which elements have the .highlight class. Iterate through all the <li> elements and toggle the .highlight class on each one. Do not make any changes to the HTML and CSS. Your end ...

The smooth transition of my collapsible item is compromised when closing, causing the bottom border to abruptly jump to the top

Recently, I implemented a collapsible feature using React and it's functioning well. However, one issue I encountered is that when the collapsible div closes, it doesn't smoothly collapse with the border bottom moving up as smoothly as it opens. ...

Contrast the values across various template sections

Due to specific requirements, I need to apply a bit of styling (margin) to the footer of the page. This styling should only be visible when the content section of the page is a product archive. I attempted to create a variable in the archive-products.php ...

TinyMCE version 5.x - Stand out with a specific selection in a personalized drop-down navigation bar

In my customized TinyMCE 5.x dropdown menu, there are 3 options that adjust the width of the editor. I am looking for a way to indicate the currently selected option, but I am unable to interact with the menu items once they are initialized. It seems like ...

I'm looking for a way to add an onclick event to every element created by Vue.js through a "v-for" loop in order to toggle the visibility of its inner content

My Vue data consists of: data: function() { return { results: { "items": [ { "id": "770c257b-7ded-437c-99b5-3b8953b5be3a", "name": "Gsbssbb", "price": 9559, "colour": { ...

Adaptable background image - smoothly resize/reposition

Is there a way to smoothly resize my background image instead of it jumping abruptly? Currently, when using media queries, the background image goes from 900px to 300px in height instantly at a certain point. How can I make this transition more gradual? ...

Ways to achieve a blurred background effect on the body using CSS

I've been experimenting with setting a background color using linear gradient, but now I want to add a blur effect to it. I've tried various codes but haven't had any success so far. Here is the CSS code I have been working on: body{ ...

Utilizing Bootstrap 3.0 Media Queries for Responsive Design

Currently, I am working on a small project utilizing Boostrap 3 (html5boilerplate custom build) and attempting to incorporate the "official" media queries as outlined in the Bootstrap documentation: /* Extra small devices (phones, up to 480px) */ /* No me ...

Unable to switch the text option

[Fiddle] I'm currently working on a project where I want pairs of buttons to toggle text by matching data attributes. While I can successfully change the text from "Add" to "Remove" on click, I am facing an issue with toggling it back to "Add" on the ...

I am puzzled by the fact that the center cell of my table is longer than the surrounding cells

I'm currently working on a project that involves creating a webpage. I have a table, and when I execute the code, I notice that the center cell appears longer than the rest of the cells. table,td,tr{border: 1px solid red; } <!document html> ...