How can we consistently center a window using a grid layout while keeping the elements off-center?

I'm trying to achieve a grid container that always stays centered while the elements with fixed width float left, even when breaking. In my example, the alignment is off and not centered. Can this be fixed with CSS only or do I need to use JavaScript? Thank you.

Here's what I mean:

#wrap {
  width: 100%;
  background: #f8f8f8;
  height: 300px;
}

#container {
  width: 100%;
  max-width: 400px;
  background: #f1f1f1;
  margin: auto;
}

#item {
  background: white;
  height: 50px;
  width: 50px;
  float: left;
  padding: 5px;
}

#item1 {
  width: 100%;
  height: 100%;
  background: red;
}
<div id="wrap">
  <div id="container">
    <div id="item">
      <div id="item1"></div>
    </div>
    <div id="item">
      <div id="item1"></div>
    </div>
    <div id="item">
      <div id="item1"></div>
    </div>
    <div id="item">
      <div id="item1"></div>
    </div>
    <div id="item">
      <div id="item1"></div>
    </div>
    <div id="item">
      <div id="item1"></div>
    </div>
    <div id="item">
      <div id="item1"></div>
    </div>
    <div id="item">
      <div id="item1"></div>
    </div>
    <div id="item">
      <div id="item1"></div>
    </div>
    <div id="item">
      <div id="item1"></div>
    </div>
  </div>
</div>

Answer №1

Please remember to utilize class for multiple items and refrain from using the same id more than once in your code.

You have the option to employ Flexbox in this scenario. Apply justify-content:center to center align the items.

Check out the Stack Snippet below:

#wrap {
  width: 100%;
  background: #f8f8f8;
  height: 300px;
}

#container {
  width: 100%;
  max-width: 400px;
  background: #f1f1f1;
  margin: auto;
  display: flex;
  flex-wrap: wrap;
  flex-direction: row;
}

.item {
  background: white;
  height: 50px;
  width: 50px;
  padding: 5px;
  box-sizing: border-box;
}

.item1 {
  width: 100%;
  height: 100%;
  background: red;
}
<div id="wrap">
  <div id="container">
    <div class="item">
      <div class="item1"></div>
    </div>
    <div class="item">
      <div class="item1"></div>
    </div>
    <div class="item">
      <div class="item1"></div>
    </div>
    <div class="item">
      <div class="item1"></div>
    </div>
    <div class="item">
      <div class="item1"></div>
    </div>
    <div class="item">
      <div class="item1"></div>
    </div>
    <div class="item">
      <div class="item1"></div>
    </div>
    <div class="item">
      <div class="item1"></div>
    </div>
    <div class="item">
      <div class="item1"></div>
    </div>
    <div class="item">
      <div class="item1"></div>
    </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

Why is the line height dimension missing from my model box when padding and margin are both set to 0?

I'm currently working on a project involving CSS/HTML and using Bootstrap 4 for my layout. I have a menu where I want the boxes to be the same size as the font-size, so I've set the margin and padding to 0. .menu-ppal { display: inline; li ...

Issue with Bootstrap Table Column Width Not Adjusting as Expected

Table columns are not adjusting width properly with the table-responsive class. I want to specify the width of the column containing Well Type to be 200px. <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="st ...

Issue with Internet Explorer 7 causing table to display with added height

Currently investigating why there is additional space being added by IE. The only fixed data is the width of the image (171 px). Using Jquery, I checked the height of the div in Firefox, Chrome, and Opera which came out to 164px, but in IE 7 it's 172 ...

Issues with password confirmation function in Vuetify components

I am struggling to create a registration form where users must confirm their password by typing it in twice, but I can't seem to get Vuetify to validate that the two passwords match. Despite trying to set up rules for this validation, my code doesn&a ...

What is the best way to ensure that my text is perfectly centered both horizontally and vertically within this DIV?

What's the best way to vertically and horizontally center text? Here is an example code that needs to be centered: <div style="display: block; height: 25px;"> <span id="ctl_txt" style="background-color: rgb(255, 0, 0);"> Basic ...

React Native ellipsizeMode - Using two Text elements side by side

I am trying to achieve a layout similar to the following scenarios: Case 1: If the text is longer than the screen width +--------------------------------------+ | very long teeeeeeeeeeeeeeeee.. (1234)| +--------------------------------------+ Case 2: If ...

Creating a mobile-friendly navigation bar with Vuetify for optimal responsiveness

I am attempting to utilize a vuetify tab component as my navigation menu in order to create a responsive navbar using vuetify. The goal is to have a normal navbar that functions like usual, but when viewed on a mobile device, it should hide the menu and di ...

Troubles with CSS in MUI Component Integration

How can I successfully implement a vertical timeline using Material UI in React? I've set it up, but the CSS isn't functioning as expected. Is there an error in my implementation? List of Dependencies: "@emotion/react": "^11.11.1& ...

Having trouble with my code trying to transfer elements back and forth between two unordered lists using an "addEventListener"

I have developed a task management system where users can create a to-do list for their daily tasks. Upon completion of a task, they can tick the checkbox next to it, which will trigger a strikethrough effect. The completed tasks are then moved from the "u ...

What is the best method for adding or removing items using ID instead of classes?

Is there a way to change the highlight class to an id, so that clicking outside the products will not trigger the highlight effect? I have added the window.onload section with my desired changes, but I am unsure how to switch the class highlight to an id. ...

"Enhance Your Website with a Stunning Background Image Using Twitter Boot

With a fixed navigation bar at the top and a container spanning span12 with full-width background, it is important for the content of the background image to be displayed fully regardless of window size. What would be the most effective way to create or c ...

Steps for arranging text in a dropdown list option

How can I format 3 words in a dropdownlist option so they are aligned properly? eg. <option value="1">ABCDE - ALPHA</option <option value="2">1234 - NUMERIC</option> <option value="3">ABC123 - ALPHANUMERIC</option> I woul ...

Steps for creating a one-sided container in CSS

I've created a container class with the following CSS: .container { margin: 0 auto; width: min(90%, 70.5rem); } This setup centers the content on the page within the container, which is great for organization. However, I'm looking to creat ...

Trigger javascript function with a button click

Is there a way to trigger a JavaScript function from an HTML button that is not functioning properly? REVISED To see the issue in action, please visit this jsfiddle link: http://jsfiddle.net/winresh24/Sq7hg/341/ <button onclick="myFunction()">Try i ...

Ensure that all divs have the same height and padding when resizing

I have 3 divs nested within a parent div. My goal is to dynamically set the height of all the child divs to match the height of the div with the maximum height, even when the screen resizes due to responsiveness. Below is my HTML structure: <div class ...

Encountering problem when attempting to incorporate fade in/fade out effect animation

I have a webpage that features multiple buttons, each triggering a fade-in/fade-out animation when clicked. This animation also involves changing the contents of a specific div. Though everything is functioning properly, there is a brief moment (about hal ...

Is it possible to display images in PHP when the value matches the specified string?

Is there a faster and more efficient way to display one or multiple images if $value == $string? For instance, let's say I have a cell containing the string 'r o g'. If a user inputs ro, it should output <img src="red.gif"> and <im ...

How come the html element doesn't have a default height of 100%?

Is there a reason why the html element is not automatically set at 100% height by default? Can you think of any scenarios where having it take up less than the full height of the window would be beneficial? html { height: 100%; } [Update] I modified th ...

What is the method for eliminating the box shadow on a table?

Seeking assistance on removing the shadow effect from ng-tables upon hovering. Can someone guide me on how to achieve this? See screenshot here: http://prntscr.com/jcpl5g widget-body { background-color: #fbfbfb; -webkit-box-shadow: 1px 0 10px 1px rgba(0, ...

Having trouble with the functionality of JQuery drop feature?

As I delve into implementing drag and drop functionality with JQuery, I encounter a peculiar issue. I have set up 3 'draggable' divs and corresponding 3 'droppable' divs. The intended behavior is for each draggable element to be accepte ...