What is the best way to ensure the width of a DIV adjusts to its content before centering it?

I'm trying to make the width of the parent div adjust based on how many inline-block displayed boxes can fit in one row. When the parent div reaches 100% width and a box is too wide for the first row, it should drop down to the next line. That's working fine, but I also want the parent div to shorten slightly and align horizontally center to eliminate any unwanted spaces. You can see the codepen example here.

Everything looks good when the box width is below 200, but once it hits 200 there are some unnecessary spaces to the right.

HTML:

<div class="hello">
   <div class="hi"></div>
   <div class="hi"></div>
   <div class="hi"></div>
   <div class="hi"></div>
   <div class="hi"></div>
   <div class="hi"></div>
   <div class="hi"></div>
   <div class="hi"></div>
   <div class="hi"></div>
</div>

CSS:

.hello{
   background: red;
   display: table;
   width: auto;
   margin: 0 auto;
}
.hi{
   display: inline-flex;
   width:200px;
   height:170px;
   margin: 10px;
   background: blue;
}

Answer №1

To help center the content, you can apply the CSS properties display: flex; and justify-content: center;.

.container{
  background: red;
  display: flex;
  width: auto;
  margin: 0 auto;
  flex-wrap: wrap;
  justify-content: center;
}
.item{
  display: inline-flex;
  width:200px;
  height:170px;
  margin: 10px;
  background: blue;
}
<div class="container">
  <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></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

What is the best way to incorporate transition effects into images as they appear on the screen?

I'm currently working on my React app and trying to add effects to images when they appear on the page. I have tried using the code below, but it doesn't seem to work as expected. I directly applied styles to the div tag because I couldn't g ...

The inclusion of Jquery Mobile causes CSS to malfunction

After integrating jQuery Mobile into my HTML via a CDN, I noticed that it completely overrides my styles and disrupts my design. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" /> <script src="http:// ...

Creating a primary php file in Apache without the use of SQL or any database: is it possible?

Forgive me if this comes across as rude, but I'm struggling to grasp the concept of apache, PHP, and servers in general. To help myself understand better, I want to create a very basic website that assigns an ephemeral ID to each user (not a session). ...

The specified font is not loading despite being correctly defined

I have a font stored locally in resources/fonts/ and I'm correctly linking to it, but for some reason, it's not loading. Here is the <style> section in my html: <style> @font-face{ font-family:"franklin_gothic_condensed"; ...

What is the best way to add a background to text that has been wrapped?

How can I achieve a background effect for text that is wrapped within a container using CSS? Is it possible to do this by referring to an image like this: . ...

Creating a process to produce a random number and send it directly to an automated email

I'm currently utilizing a form builder from jqueryform.com to construct a registration form. My goal is to have each registered user assigned with a unique account number, which will be a randomly generated 6-digit number. Additionally, I want the pro ...

Convert price to Indonesian Rupiah currency format with the help of Vue.js

Can someone help me convert the price format from IDR 50,000.00 to IDR 50.000 using JavaScript and Vue? I found a script on this website, but I am having trouble understanding how it works. The script looks like this: replace(/(\d)(?=(\d{3})+(?: ...

Tips for avoiding duplicate usernames in mySQL when working with PHP

My users register for the database, but how can I prevent duplicate usernames in the database? I want to inform the user if their username already exists. <?php $error = ""; // error $GoodJob = ""; if(isset($_POST[&apos ...

There seems to be a glitch in my PHP comment validation function—it does not seem to be functioning

This form is contained in a PHP file named single.php. <form action="comments.php" method="post" > <?php include(ROOT_PATH . "/app/helpers/formErrors.php"); ?> <input type= "hidden" name="id" value= & ...

Tips for preventing horizontal list items from stacking on top of each other in React

After successfully creating a horizontal list using HTML and CSS, I encountered an issue when trying to replicate the same code in my React application. In React, the list elements were stacking on top of each other due to a styling conflict with the style ...

Using JQuery to hide elements by setting CSS display as none instead of block

Is there a way to change the display of a content div to block when a specific tab is selected by the user, while hiding all other content divs? Here is the JQuery code I have tried so far: $(document).ready(function() { function resetTabs() { $("# ...

Avoiding the <br> tag in list items with TinyMCE

I am currently using tinyMCE and I needed to enable the "force_br_newlines: true" option. Without this setting, if I press the "Enter" key twice and check the source code, I only see one <br> tag. However, when I set the option to TRUE, it creates a ...

Using jQuery, JavaScript, and PHP, we can easily implement the functionality to disable the form and submit button

After submitting a form, I am trying to disable both the submit button and the form itself. However, every solution I have attempted either disables the form and button without submitting it or submits the form without disabling the button/form. This is t ...

What is the best way to apply a 2px padding to text-decoration: underline?

As a dedicated frontend developer, I believe in creating underlines with a 2px padding instead of the default 1px. Is there an easy solution for achieving this? PS: Yes, I am aware of using a div with a black background color and 1px * Npx with position: ...

Establishing connections for pages within subfolders

MasterPage CSS was defined to ensure consistent styling across all pages, but an inline style for the menu was mistakenly added. Pages are linked relatively as follows: <ul> <li style="left: -1px; top: 0px; width: 90px"><a href="Home.aspx" ...

Difficulty with setting a border for text spanning multiple lines

I am trying to style a header text with a border around it. The issue arises when the larger text spans over two lines, causing the border to break in between. In an attempt to fix this, I applied the CSS property box-decoration-break: clone;. However, thi ...

Is there a way to enable scrolling on the page upon loading, without moving the embedded PDF object itself?

After embedding a PDF object on my webpage, I noticed that when loading the page and scrolling using the mouse wheel, it actually scrolls up and down inside the PDF instead of moving through the entire page. To fix this issue, I have to first click in a bl ...

Insert the current row into the table

I'm working with an HTML table structure that looks like this: <table id="test"> <thead> <tr> <td></td> <td></td> <td></td> <td></td> ...

What could be causing my scrollable div to not function properly within a Bootstrap modal?

I am facing a frustrating issue. I have a simple DIV element that I want to make scrollable, containing a table inside it. The process should be straightforward as I have a separate .html file with the code that functions correctly. Here is an excerpt of ...

The signal property 'ɵunwrapWritableSignal' is not found on the specified type 'typeof import/node_modules/@angular/core/index")'

Despite attempting the solutions provided in previous threads, none of them have been successful for me. Can someone please lend a hand with this issue? https://i.stack.imgur.com/sGRsn.png ...