Enclose a group of tiles in a shrinkwrap div

I've been struggling to make a div adjust to its content size here. I've attempted various methods to shrinkwrap it, but none seem to work.

<div class="outer">
    <div class="cont">
        <img src="http://www.placehold.it/100" />
        <img src="http://www.placehold.it/100" />
        <img src="http://www.placehold.it/100" />
        <img src="http://www.placehold.it/100" />
        <img src="http://www.placehold.it/100" />
        <img src="http://www.placehold.it/100" />
        <img src="http://www.placehold.it/100" />
        <img src="http://www.placehold.it/100" />
        <img src="http://www.placehold.it/100" />
        <img src="http://www.placehold.it/100" />
    </div>
</div>

Both

display: table;

and

display: inline-block;

have proven unsuccessful when applied to the inner div.

My goal is to have the inner div centered within the outer div, while keeping the contents of the inner div aligned to the left.

Answer №1

I remember a similar question being asked some time ago. My suggestion was to utilize media queries to clear the floats and ensure that the table shrinks to fit its contents. You can refer to the original post here: Centering a variable width grid while aligning its elements to the left. Additionally, I created a fiddle to demonstrate this solution here: http://jsfiddle.net/5hWXw/.

Take a look at the jsfiddle example above;

Answer №2

It appears that your question may not be entirely clear, but based on my understanding, you are looking to have the inner div expand to fit the full content. Here is a helpful jsfiddle link for reference. I removed the display: table; property from the inner div.

Answer №3

To achieve the desired layout, ensure that the parent .outer div is set to display as a block element. The child .cont div should be centered by giving it a specific width and using margin: 0 auto. The value of zero in the margin property is not necessary, but the left and right margins must be set to auto.

For a practical demonstration, you can view this example on jsfiddle: http://jsfiddle.net/y7KJt/

I utilized the font-size: 0; declaration within your .cont class to eliminate any unwanted whitespace when dealing with inline-block elements like <img> tags.

Answer №4

It's a bit challenging to provide a precise answer to your query in this context, as I'm not entirely clear on what your ultimate goal is. When you mention wanting the inner div to be centered, it seems like you intend for the inner div to have a fixed width while the outer div spans 100% width, if I understand correctly.

Using display: table in this scenario is likely not the most suitable option. Typically, this property is utilized to ensure child elements have equal heights, automatic distribution, or vertical alignment capabilities (such as top, middle, or bottom).

If your objective is solely to center the inner div and assign it a fixed width, I'd suggest opting for the auto margin approach.

Specify a max-width for your inner div to establish a width limit, and apply left and right margins set to auto.

Does this align with your intended outcome? http://jsfiddle.net/ZnER7/1/

UPDATE

Based on your feedback, I believe I have a clearer understanding of your objective :-) However, if you're concerned about your inner div expanding when child nodes wrap, there may not be a straightforward solution.

In this demonstration: http://jsfiddle.net/ZnER7/3/

It demonstrates the utilization of inline-block elements as a workaround. By treating the inner div like text, you can center the inner container, with text-align: left aligning the images to the left. Nevertheless, when the inner container shrinks to accommodate wrapped child nodes, it continues to attempt maximum expansion. This behavior is likely a consequence of text overflow and could be unchangeable. Even when extensive words are incorporated, the outcome remains consistent.

Best regards, Gion

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

Personalize the material design datatable checkbox option

Recently, I delved into the world of material-ui design in conjunction with ReactJS. However, I have encountered a roadblock while attempting to customize the style of a checkbox that is being displayed under the <TableRow /> component. (Check out th ...

jQuery not displaying Div elements as expected

After experimenting with the Coverflow technique and adapting it to work with a div, I have encountered a slight issue. Below is the HTML code I am using: <html> <head> <meta http-equiv="Content-type" content="text/html; ...

Mixing together an array of colors, experimenting with adding a touch of transparency

Here is my first question, diving right in. I recently created a canvas in HTML and followed a tutorial to generate random floating circles that interact with the mouse position......if you want to check it out, click here. The issue I'm facing now ...

Top solution for maintaining smooth navigation across web pages

As I dive into the world of web development, I find myself intrigued by the idea of reusing navigation and banners across multiple web pages. However, despite my research efforts, I have yet to come across a definitive answer. My objective is simple: The ...

jQuery animation for expanding accordion headers

I want to create a cool animation for my accordion headers that mimics a ribbon being dragged onto the wrapper when hovered over, and then dragged out when the hover ends. Everything was working fine in this initial jsFiddle, but as soon as I tried to ani ...

I aim to toggle the visibility of input fields upon clicking a button

Form.html <form [formGroup]="myForm"> <ion-button [(ngModel)]="isActive"(click)="onClick()" >Add</ion-button> <ion-item> <ion-label position="floating">First Name</ion-label&g ...

Include new material in the upper-right corner of each row

Struggling with various styling techniques like pull-right, float, and row-fluid, I have come here to seek help: My goal is simple - I want a Map to be displayed on the right side of a set of rows. Currently, it looks like this: Although close to what I ...

"Enhance Your Design with Hovering CSS Backgrounds

Seeking assistance with changing the background image on span hover. If anyone can assist, I have included the complete code for the website below. Take a look at the full website code here: Pastebin CSS Pastebin JavaScript ...

Struggling with navigating segmented dropdown buttons

I was following a tutorial on Udemy to learn about inputs and dropdown buttons for bootstrap. Everything was going well until I attempted to create a simple selection of albums for one of my favorite artists. The buttons kept separating when I tried to cen ...

Overrides of variables are not always complete

After setting up my project with npm install to utilize sass overrides, I encountered an issue. Despite adjusting the $theme-colors, only part of the page reflects the change. https://i.sstatic.net/xjrsx.png I attempted the following: $theme-colors: ("p ...

Which is the superior choice: WebSocket-Node or ws? Additionally, is there a universally accepted interface for nodejs websockets available?

I am considering switching from socket.io to regular websockets in order to utilize binary data transfers and eliminate the need for base64 encoding. There are two primary websocket libraries for nodejs available on GitHub: Worlize/WebSocket-Node einaro ...

What are some alternative methods for downloading the latest file version without relying on the client cache?

On my webpage, I have a table displaying users' data. Each row represents a user and includes their business card, which is a clickable tag that opens a PDF file stored on the server. <td class="business_card"> <a href="/static/users_doc ...

What is preventing these elements from being contained within particular divs?

I'm facing an issue with my HTML code and need some help unraveling what's happening. On my simple website, I have a container div, header div, and body div, but it seems like the HTML elements are not aligning properly within these divs (they a ...

Pagination functionality in TablePress allows for improved organization and

Currently, I am utilizing the TablePress plugin on my WordPress website and I am aiming to achieve a pagination layout similar to this illustration: . How can I modify the pagination to display the text 'page 1 of X' instead of 'previous&apo ...

How can you declare variables in CSS using LESS?

I am facing a challenge where I need to dynamically change the branding color and other styling variables on the portal based on certain conditions at runtime. I have successfully implemented this functionality using CSS with the code snippet provided be ...

Get rid of empty spaces in gridstack js

My latest project involves using gridstack js In the image displayed, I have highlighted the excess white space (in blue) that needs to be removed. Take a look at this visual: https://i.sstatic.net/Qgt62.jpg Any suggestions on how to eliminate this ...

GAE does not have access to background images

Having trouble loading background pictures in my GAE front-end app. "Failed to load resource: the server responded with a status of 404 (Not Found)". My app is a simple website, no GAE backend. Using Eclipse with Google AppEngine Plugin. Background pict ...

Utilize an array with dynamic referencing

I am attempting to reference a single index out of 30 different indices based on the user's actions, and then utilize ng-repeat to iterate through each item in the index. Controller: $scope.meals = [ { title: 'Abs', url:"#/app/mworkout ...

Resize an image to automatically adjust and fit within a designated container

Is there a way to resize an image automatically to fit inside a container with a blank background? Currently, the structure looks like this: <div class="row"> <div class="col-md-3"> <div class="panel panel-primary"> ...

A pop-up window displaying an electron error message appears, but no errors are logged in the console

In a Nutshell: While developing a single-page website with Electron, I encountered the common issue of jQuery not being globally accessible. To tackle this problem, I decided to simplify it by using a quick start example, but unfortunately, I'm strugg ...