How do you center an inline-block div inside its parent div using CSS?

(Code: http://jsfiddle.net/T4hrK/)

I have been searching endlessly, but cannot seem to find a solution. I have an outer div, and within it, I wish to center an inner div. This inner div contains numerous inline elements (such as images in a gallery) that can be adjusted to inline-block or floating block elements.

The issue arises when the number of inline elements inside the inner div causes them to wrap onto a new line. However, the inner div does not resize itself to fit these elements exactly; instead, it sizes itself to match its parent's width. As a result, I am unable to center it within its parent due to this mismatch in widths.

My goal is to have the inner inline elements left-aligned within their container div, while ensuring that the div accurately encloses them for easy centering within its parent.

For example:

<div style="border:1px solid red; padding:2px;">
  <div style="display:inline-block; border:1px solid green;">
    <span>1234567</span>
    <span>1234567</span>
    <span>1234567</span>
    <span>1234567</span>
  </div>
</div>

In this scenario, the inner green div is too wide, preventing me from easily centering it within the red div.

In this example, each span represents an image, the green div should perfectly wrap around them, and the objective is to center the green div inside the red div.

Answer №1

Do you need something similar to this? http://jsfiddle.net/Gxcwk/ This layout collapses based on the size of inner elements within the inner div. I adjusted the inner elements with the following CSS:

float:left; margin:2px 5px;

This creates a clear division between them. If you plan to use equally sized images, it will look neat in the grid.

Keep in mind that this effect only occurs on page load or refresh. For a more fluid and responsive solution, consider using jQuery masonry:

Answer №2

Would it be beneficial to include text-align: center; in the main div, similar to the example in this demo: http://jsfiddle.net/T4hrK/12/

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

Navigate through content to reach the bottom of the webpage

I am currently working on a layout that is similar to Facebook's messaging system (facebook.com/messages). I have made some progress with it, but not quite there yet. You can view my work so far here: https://jsfiddle.net/3nd0b17m/23/ https://i.sstati ...

What is the impact on positioning when including a class in Angular?

I've encountered a peculiar bug while trying to add a class to a ui-router element. When I apply the active class, an absolutely positioned element that spans the entire view suddenly snaps to the width of its parent. To see the issue in action, chec ...

Delay the execution using Javascript/jQuery, remove the last element from the DOM

Within a jQuery AJAX call, I am using a $.each() loop. Each element in the response data triggers the addition of a portion of HTML to a container class in my index.html file. To view the code and ensure proper formatting, visit the codepen here as the pa ...

Display the latest item using React

I've encountered a problem with displaying the last message in my pet chat application. Currently, I have to manually scroll to the bottom every time I open the chat in order to see the latest message. This scrolling behavior is not very user-friendly ...

Setting the value of a CSS property to .value

Currently, I am engrossed in a small project. Here's the gist: I aim to read text field values and assign them to corresponding CSS rules. For example, document.getElementById("BD").value; The value being read should be assigned to border-radi ...

Elements appear with varying widths in Firefox compared to Webkit-based browsers

I believe it would be simpler for you to inspect the elements rather than me writing the code, so I have provided a link: music band site. Now onto the issue: When the window width is reduced in Firefox, you will notice that the social icons in the yellow ...

When the height of the html and body is set to 100%, ScrollY/ScrollTop is unable to function properly

Utilizing height: 100% and overflow: auto on html/body has helped me fix a scrolling issue in Chrome. However, I've noticed that ScrollY and ScrollTop always return 0 now. Is there a way to determine the scroll position in this scenario? Here is the ...

Three.js is failing to render within a specified div

My current project involves utilizing three.js to create a cube rendering. Despite the cube appearing on the screen, I am facing issues with getting it to display within the specified div element and in the desired style. HTML: <div id="render"> & ...

Click on the logo to return to the home page

Logo Link Causing Menu Alignment Issue After setting the logo as a link, the menu unexpectedly shifts to the right. Upon inspecting the menu element, it appears that there is an additional hidden menu link on the left side leading to index.html. Can anyon ...

Adjust image size to maintain consistent dimensions

Is there a way to resize the images so they maintain the same aspect ratio? Currently, all the images are appearing distorted. https://i.sstatic.net/czpto.png Here is the HTML code for the card: <div class="container"> <div class="flex- ...

The z-index property in CSS is ineffective on Firefox browsers

My current project involves implementing a dual slider input range. The HTML code I have written looks like this: .acCalcInputRange input[type="range"] { pointer-events: none; position: absolute; bottom: 0.6rem; border: none; outline: none; ...

Tips for setting a Bootstrap 3 dropdown menu to automatically open when located within a collapsed navbar

Is there a way to have my dropdown menu automatically open when the collapsed navbar is opened? Take a look at this example I created in a fiddle to see what I'm working with so far. Right now, when you click on the navbar in its collapsed state, two ...

Positioning two labels within a Div container

Struggling to align two labels within a div using ASP.NET, where the length of the data displayed can vary. The challenge I'm facing is getting the first label to align to the left and the second label to the right. Since the data displayed is querie ...

Do the elements only find their rightful position when the window is interacted with?

I've encountered a strange issue with a button in my code that inserts HTML textareas onto the page. Initially, when the button is clicked, everything appears as expected. However, upon subsequent clicks, the textareas start to appear in random places ...

Attempting to get the boxes in the final row to show up

Exploring new territories in web design, I'm currently working on achieving the layout shown below: Box Layout I've encountered an issue where the last row of 4 boxes in the middle section is not appearing as intended. Once this row is successfu ...

Customize the color of the horizontal line in React using dynamic properties

Is there a way to customize the color of an <hr /> element in a React application? If we were to use a functional component to accomplish this, how would we go about it? ...

EU directive on cookies: What is the best way to store your preferences?

While there are numerous third-party solutions available to comply with the cookie EU directive, I prefer to learn how to implement it myself. My specific requirement is that when a user clicks on "Accept", their choice should be remembered for the entire ...

Enhancing WordPress Icons for Parent and Child Elements

I am seeking to customize icons on a WordPress website. The site contains a variety of product categories, including subcategories. https://i.sstatic.net/sTm1O.png My goal is to display one icon for parent categories and a different icon for child catego ...

Printing Strings in JavaScript IF Conditional Block

Hello there! I am looking for assistance with a JavaScript concept. Recently, I created some code where in the HTML file, there is a div element with an id of "GetID" where the string outputs are meant to be displayed. The JavaScript code looks something ...

What is the best way to ensure the width of the div is adjusted to fit the table it contains?

What is the best way to adjust the width of a div containing a potentially wider-than-screen table? Despite setting padding: 10px for the div, the right padding disappears when the table exceeds the screen width. Below is an example code snippet: <div ...