Align divs both vertically and horizontally within a wrap to eliminate unnecessary spacing

I am facing an issue centered horizontally and vertically within wrapping divs (blue frame) in a large div (green frame with a height of 100%). I was recommended to use the flex display, but there were significant gaps between the lines and I want the elements to only have the spacing that I set for them (e.g., margin: 10px).

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

The code has been uploaded on CodePen: https://codepen.io/misiekdp/pen/ZEWgagr or view the working snippet below:

body {
  margin: 0;
}

.wrapper h1 {
  margin: 0;
  text-align: center;
  font-size: 30px;
}

#x1 {
  height: calc(100% - 58px);
  margin: 10px;
  border: 1px solid green;
  /* padding: 5px; */
  /* position: relative; */
  /* transform: translateX(10%) translateY(50%); */
  position: absolute;
  /* top: 50% */
  display: flex;
  flex-direction: row;
  text-align: center;
  flex-wrap: wrap;
  justify-content: center;
}
.jajucho {
  /* position: absolute; */
  /* display: block; */
  display: inline-block;
  height: 100px;
  margin: 10px;
  width: 100px;
  border: 1px solid blue;
  align-items: center;
  text-align: center;
  justify-content: center;
  /* display: flex; */
  /* position: absolute; */
}
<div class="wrapper">
  <h1>Menu</h1>
  <div id="x1">
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">
    </div>
    <div class="jajucho">

  </div>
</div>

Answer №1

If you want this to work seamlessly, follow these simple steps:

1. Group your elements with the .jajucho classes inside a single container for better vertical centering without affecting their layout, like this:

<div id="x1">
    <div class="item-container">
        <div class="jajucho">
        </div>
        <!-- Add other divs here... -->
    </div>
</div>

2. Include align-items: center; in the styling of #x1 to vertically center-align it (remember that justify-content: center only works horizontally)

#x1 {
    align-items: center;
    /* Add more CSS styles here...*/
}

Note: Additionally, change height: calc(100% - 58px); to min-height - especially useful when #x1 needs to be taller than the screen height on narrow screens.

See the Working Example:
Execute the code snippet and maximize the view to observe the centered content more clearly

body {
  margin: 0;
}

.wrapper h1 {
  margin: 0;
  text-align: center;
  font-size: 30px;
}

#x1 {
  min-height: calc(100% - 58px);
  margin: 10px;
  border: 1px solid green;
  position: absolute;
  display: flex;
  flex-direction: row;
  text-align: center;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

.jajucho {
  display: inline-block;
  height: 100px;
  margin: 10px;
  width: 100px;
  border: 1px solid blue;
  align-items: center;
  text-align: center;
  justify-content: center;
}
<div class="wrapper">
  <h1>Menu</h1>
  <div id="x1">
    <div class="item-container">
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </div>
      <div class="jajucho">
      </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

Creating Radial Fades with CSS3

Seeking guidance on creating a background similar to the one shown here using just CSS. I believe it can be done, but I struggle with CSS3. The goal is for the background to be compatible with all modern browsers, not overly concerned about support for br ...

Issue with animating multi-column layout in Chrome causing display problems

I'm currently utilizing jQuery to create an animated multi-column layout that extends off the screen horizontally. I have implemented two controllers - one red and one blue - to navigate back and forth within the layout. Strangely enough, while the an ...

Error Encountered: Angular JS Throwing Unhandled Injection Error

I am having trouble validating the fields in my index.html and js files. I keep seeing errors, even after trying different versions of angular-route.min.js and angular.js. AngularJs | Basic Login Form <body ng-app="myApp"> ...

What is the best way to adjust the width and height of react-flip-page through CSS for different media queries?

Incorporating the react-flip-page npm package to add animation to a book on my web application has been exciting. However, I've encountered an issue with adjusting the width when using media queries due to the available props for setting size and colo ...

AngularJS ng-model is experiencing issues with input number type and arrow keys specifically in the Edge browser

When using an input with type="number" associated with a ng-model, I am unable to update its value by pressing the up and down arrows in Microsoft Edge. The ng-model is not updated in this scenario. Here is an example to demonstrate the issue: https://pln ...

Choosing specific characteristics inside a <div> element using jQuery

In one of the tables, there is a td element with the following structure: <td> <div id="spcheck"> <div id="check" data-1='<%# Eval("sth1") %>' data-2='<%# Eval("sth2") %&g ...

I am currently encountering a glitch with my video VTT file, as the subtitles are not appearing on screen

welcome to the world of coding WEBVTT 00:01.000 --> 00:8.000 coding is my passion join me on this journey ...

Calling functions in AngularJS/HTML can help you execute specific

Just started with angularjs and facing some major issues haha... I have something that seems to be working fine, but I can't figure out what's wrong with this code... can someone please help me? Here it is: Basically, the scope.create function ...

Is your CSS not reflecting changes in Node.js?

I am having trouble getting the CSS to update in the browser using Browser Sync via Gulp on Node.js. What could be the issue? Below is my Gulp.js file: var themename = 'playtest haha 1'; var gulp = require('gulp'), // Prepare ...

Looking for a personalized grid layout with 3 columns for your thumbnails?

I am trying to make this work and I stumbled upon this website. Instead of having 4 columns, I would like to have 3 columns with the same height. Is there a way to achieve this? This is how it appears on my webpage: x http://imagizer.imageshack.us/v2/15 ...

Delay the loading of HTML elements to improve website performance

While working on my portfolio, I noticed that the page load time is quite long due to all the images needing to load before the page is fully loaded. I started thinking if there is a way to delay or prevent the loading of images and then have them load la ...

Generating a webpage with a distinct serial number

Within my PHP page, I have gathered various values from the previous page and included a print button that allows users to print all of these fields along with a unique number. However, the issue arises when multiple users are logged in simultaneously as t ...

Styling group headers within an unordered list using pure CSS - possible?

Hey there, I'm looking to spruce up my UL by adding group headers. Here's a sneak peek at the structure I have in mind (keep in mind this is generated dynamically from a database): <ul> <li class='group group-1'> < ...

Why is the image not appearing in the Popover?

When hovering over, the popover only shows the title and not the image. Snippet of Code: <script type="text/javascript"> $(document).ready(function(){ var img ='<img src="/assets/Excel_1.png">'; $("#blob").popover({ title: ' ...

Toggle the backgroundImage visibility by setting it to either hidden or displayed using jquery

$('.arrow').css("background-image", "url('../img/arrow.png')").hide(); I have implemented a custom solution using CSS and jQuery to hide the downward arrow when scrolling down on my webpage. `$( document ).ready(function() {
 co ...

Switching up the navbar's background hue

Struggling to change the background color of individual navbar options using id's. Only the text in the navbar changes, not the background color. My goal is to have a unique background color for each navbar option within this bootstrap template I am c ...

Using *ngFor in Angular for Parent-Child component interaction

Is there a way to iterate through a child component multiple times with different data each time? For instance, in my 'sitelist' component (parent), I aim to loop through my 'summary' component (child) as many times as needed based on ...

The submit button remains unresponsive, yet upon removing its JavaScript code, it functions smoothly

This is the content of my index.html file. It contains JavaScript code. However, there seems to be a problem with the JavaScript validation as the Submit button does not perform any action when clicked. Surprisingly, when I remove the JavaScript code, the ...

How can I use jQuery to implement a progress bar for Iframes loading?

My job involves working on an intranet where I have a complex aspx page with multiple Iframes, all of which we control. Each iframe is dynamically set using JavaScript/jQuery by different buttons on the page. Some buttons set the src attribute for iframes ...

What steps should I take to repair the footer on this webpage?

Working on a footer using bootstrap and it's looking great, but I encountered an issue with blank space appearing at a specific resolution (see image below). Bootstrap Footer: https://i.sstatic.net/XifQe.png My Footer Design: <footer> < ...