Tips on automatically adjusting the width of a div to fit the content, maintaining center alignment, and ensuring the contents float to the left

I am facing an issue with a div element that has multiple children. My goal is to have the children neatly fit to the bottom left of the grid when the window expands, utilizing the next available space efficiently. However, as the div expands with the window, it creates unnecessary extra room on the right side before adding another column. I am struggling to center the div while keeping the content floating left and ensuring that the div is only as wide as necessary for the content.

.wrapper{
  display:inline-block;
  width:auto;
  text-align:center;
  border:solid black 5px;
  margin:10px auto;
}

.wrapper > *{
 margin: 0 auto;
 border:solid black 1px;
 text-align:center;
 display:inline-block;
 float:left;
 width:150px;
 height:150px;
 margin: 0px 10px 10px 10px;
}

Despite trying various combinations of block elements and inline-block elements, I have not been able to find a solution.

I apologize if my explanation was unclear, but it may make more sense when looking at the provided code in the fiddle.

https://jsfiddle.net/rfzk5fhx/

Answer №1

It's possible that I may not fully understand your question, but if you're looking to have your container div adjust its width and height based on its content, you can achieve this by using inline-block display and removing any width and height CSS properties.

Below is the code snippet:

ul {
    background: #aaa;
    padding: 10px;
    display: inline-block;
    margin:10px auto;
}

You can view the updated fiddle at - https://jsfiddle.net/rfzk5fhx/2/

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 use jQuery to access dynamic HTML elements?

I have created a dynamic table that allows users to add and delete rows. However, I am facing an issue with accessing the information inputted by the users for calculations because it seems to be inaccessible once dynamically added to the page. Below are ...

Switching from file:// to http:// in Angular / Ionic is a necessary step when incorporating external scripts like the Disqus directive into your project

Currently, I am attempting to incorporate the disqus directive into my project. The documentation for this directive can be found here. However, I have encountered some issues due to the particular setup of my application. There is a line in the script wh ...

What is the proper way to insert a video using html5?

Hi there! I've been working on creating a webpage and have successfully added all other elements except for embedding a video using html5. It is crucial to the overall design of my website, so any assistance you can provide would be greatly valued. ...

Potential Performance Issue with Material UI's withStyles Function

I've been given the task of optimizing the loading speed of a page in our react redux web app. When the page load action is triggered, there seems to be a slight freeze lasting about half a second. After checking the profiler, everything seems fine ...

Concealed Separator for Text Elements in HTML

I am in search of a method to distinguish certain specific strings within HTML code. Although I can recognize the desired strings, they may also appear as part of longer strings throughout the document. In order to locate them, I currently insert a special ...

Searching for link href and text in a PHP DOM document

Within an html tag, there are the following elements: <ul class="l1nk"> <li class="dir online"> <a target="_blank" rel="nofollow" href="https://sample.com/embed/2882015?ref=7Qc" t ...

What is the best way to determine the final letter of a column in a Google Sheet, starting from the first letter and using a set of

My current approach involves generating a single letter, but my code breaks if there is a large amount of data and it exceeds column Z. Here is the working code that will produce a, d: const countData = [1, 2, 3, 4].length; const initialLetter = 'A&a ...

Incorporate dynamic HTML into Angular by adding CSS styling

Just starting out with Angular and have limited front-end experience. I'm feeling a bit lost on how to proceed. Currently, I have a mat-table with a static datasource (will connect to a database in the future), and I need to dynamically change the bac ...

Exploring the integration of Django Rest Framework with Angular

I am just starting out with Django Rest Framework (DRF) and AngularJs. I am trying to figure out the best way to integrate these two technologies. Combining DRF and AngularJs in one project (Most of the tutorials recommend this) Using DRF as the backend ...

React fails to display the content

Starting my React journey with Webpack configuration. Followed all steps meticulously. No error messages in console or browser, but the desired h1 element is not showing up. Aware that React version is 18, yet working with version 17. App.jsx import Reac ...

Incorporate a Background Image, which is linked to a website URL, by utilizing an external CSS file within

I am struggling to incorporate an external CSS file (specifically a background image from a URL) into my HTML document. I have successfully applied internal CSS, so the issue is not with the website link. Despite reviewing similar posts addressing comparab ...

Here is a way to trigger a function when a select option is changed, passing the selected option as a parameter to the function

Is there a way to call a function with specific parameters when the value of a select element changes in Angular? <div class="col-sm-6 col-md-4"> <label class="mobileNumberLabel " for="mobilrNumber">Select Service</label> <div cla ...

Organizing DIVs upon website initialization

My website features a layout with three columns: <div id="column1"></div> <div id="column2"></div> <div id="column3"></div> I currently have 3 divs on the webpage: <div id="1">aaa</div> <div id="2">b ...

Arrange elements within a div using Bootstrap

I have been struggling to align items within a div tag and despite trying various methods, I have not been successful. . Here is the current structure of my code: https://i.stack.imgur.com/t9PAj.png I attempted to use flexbox, but encountered an issue ...

What could be the reason for the absence of definition for 'res'?

As I work on coding a bot using discord.js, I am facing an issue while trying to set up a system where the bot can send a message as long as it is not blacklisted. However, every time I attempt to run the function, I encounter this error message: Reference ...

A step-by-step guide on configuring data for aria's autocomplete feature

Currently, I am implementing aria autocomplete and facing an issue while trying to populate data from the server into the selection of aria autocomplete. I have tried setting the selected property of the aria autocomplete object, but it doesn't seem t ...

Using ESP8266 as a client and setting up an AJAX web server

My goal is to use ESP8266 as a client that will be controlled by a server running on my computer. I want the server to send commands to the ESP8266 using AJAX, and for the ESP8266 to respond to these commands and also be able to send data back to the ser ...

Swapping a value within an array and moving it to a new position

Consider this scenario: I am dealing with a list of arrays containing values like: let data = [ "10-45-23:45", "10-45-22:45", "10-45-20:45", "10-45-23:45", "10-45-23:59,00:00-04:59", "10-45-23:59, 0 ...

Send a MySQL query and an HTTP request to an SMS gateway through JavaScript

I have a scenario where data is being received by my confirm.php file. In this file, I have the following code and currently facing an issue with the javascript part. Any help and suggestions would be greatly appreciated. The objective of my javascript is ...

Tips for keeping the main section from scrolling while scrolling through the side navigation

Within my Angular application, I have implemented a sidenav and a main section. My desired behavior is to prevent any scrolling in the main section while I am scrolling in the sidenav, similar to the functionality seen on the Angular Material Design docume ...