When CSS width:auto expands beyond its intended length

I'm experiencing an issue when I set the width to auto, the div expands to full length rather than adjusting to the width of the inner divs.

Here is the HTML code:

<div class="vidswraper">
<div>
<div class="videoimage">
<img src="images/icon1.jpg" height="100" width="100">
<p class="price">21.09</p>
</div>
</div>

<div>
<div class="videoimage">
<img src="images/icon2.jpg" height="100" width="100">
<p class="price">1.99</p>
</div>
</div>

<div>
<div class="videoimage">
<img src="images/logo.jpg" height="100" width="100">
<p class="price">12.12</p>
</div>
</div>

And here is the CSS code:

.vidwraper {
   margin: 0 auto;
   width: auto;
   height: 110px;
}

When using inspect element, it appears that the width is not adjusting to cover the 3 divs inside the .vidswraper container.

Please note that the inner divs are floated left and have a width of 110px.

Answer №1

The natural behavior of blocks is to occupy all the available space.

However, it seems you are looking for the shrink-to-fit algorithm.

There are multiple methods to achieve this:

  • display: table

    It should be noted that this method uses a table inner display layout model, which could pose issues if the content consists of tabular elements. Otherwise, the elements will be enclosed in an anonymous table cell.

  • display: inline-block

    An inline-level box is generated using this method instead of a block-level one. If multiple are used, they may be arranged horizontally rather than vertically.

  • display: inline-table, display: inline-flex, display: inline-grid

    These share the same drawback as display: inline-block. Additionally, they utilize an inner display role layout model that is neither flow nor flow-root, which can lead to complications.

  • float: left

    One drawback of this method is that the box is removed from the normal flow. Clearfix may be needed or the parent container may need to establish a block formatting context to address this issue.

    Additionally, floated elements will be arranged horizontally. Clearance can be used to prevent this.

  • width: fit-content

You may also want to explore width: max-content or width: min-content. However, these, in conjunction with width: fit-content, may not have widespread support yet.

Alternatively, placing your element in a specialized layout model could be worth considering. For instance, if placed in a flex container, it will be treated as a flex item and therefore (by default) will not expand to fill all available space.

Answer №2

Within the realm of HTML, divs are considered as block level elements, implying that they naturally span the full width of their containing element. To confine the width of divs to a specific dimension, you must declare an explicit width.

Answer №3

The property 'width:auto' really implies: "let the browser determine the width".

When applied to a DOM element with the default display value of block, width:auto is interpreted as occupying 100% of the parent element's content width.

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

Aligning a collapsible feature while ensuring that the content descends

My dilemma involves a centered menu with a dropdown feature. The issue arises from the absolute positioning, causing the entire menu to shift upward when the dropdown is activated due to the increased height. I am struggling to find an effective solution ...

Creating a typewriter animation using Tailwind CSS and Next.js

I am currently working on implementing a typewriter effect that is almost exactly how I want it. However, I am facing an issue with the printing process. Right now, each word gets printed on a new line and falls into place when done printing. What I actual ...

Pass a jQuery value to a PHP variable

I am looking to send a value when I click a button. For example, when I click on <a id="link1"> Page Home </a>, it should send the value to a custom variable php. Here is an example: <?php $linkredirect = ''; // This will be sent ...

Output each uploaded file using jQuery's console.log function

My AJAX upload form is set up and working, but when multiple files are selected they all upload at once. I want to create a separate div for each file to show the progress individually. However, when I try to test the number of files using this code snippe ...

What is the best way to pass createdDt and updatedDat values in an Angular form without displaying them on the template?

I am working on a message creation form in Angular where I need to include createdDt and updatedDt as hidden values. These values should not be visible in the template. I want to automatically set the current date and time for both createdDt and updatedD ...

Is there a way to obtain the coordinates of an SVG element by simply clicking on a specific point?

I'm still learning about SVG and I'm trying to trigger an event that captures the click coordinates when clicking on the SVG. I have a few questions: Since I'm using Angular, I'm unsure if it's possible to keep my function in th ...

Troubleshooting HTML/JavaScript with JSP and JSTL: iPhone only displaying the first option in select dropdown, rather than the selected option

My HTML includes a <select> element: <select id="mySelect"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> ...

Counting checkboxes in jQuery version 1.9

I recently upgraded my website from jQuery 1.6 to jQuery 1.9.1, and now some of my old code is not working as expected. Specifically, I have a piece of code that handles checkboxes in table rows, allowing only up to 5 checkboxes to be active at once and di ...

Having trouble with the Tap to copy discount code function not working in the Shopify cart drawer?

Our goal is to implement tap to copy functionality for code snippets on our Shopify website. It works seamlessly on the product detail page, but in the cart drawer, it only functions properly after the second page load. https://i.sstatic.net/xzMVY.png ...

Switch up the image displayed when sharing content via buttons

Currently, I have implemented a custom sharing feature on my website using the code below: <div id="share_buttons"> <img src="images/facebook.png" onclick="popup('http://www.facebook.com/share.php?u=<?php echo $URL; ?>', &apos ...

tips for aligning bootstrap flex items horizontally in multiple div containers

Is there a more efficient method to align flex items in different div flex containers so that the flex items have the same width, as shown below? <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" ...

Imitating the Frameset Separator's Actions

The latest HTML5 specification has eliminated the <frameset> element. One useful feature of the <frameset> tag that is hard to replicate without it is: In a frameset, you can adjust the position of the frame divider line with the mouse. Is t ...

Can the Browser width/document width be maintained when shrinking the browser window size?

https://i.stack.imgur.com/a4gfA.pngLooking for a solution to maintain the browser/document width at 350px, preventing users from minimizing the window. The desired width is actually 400px, not 350px. Please refer to the image above. I require the window ...

What is the process for making the default text in a text box appear grayed out?

Hey there! I have this cool idea for a text box. Basically, it starts off with default text but when you hover your mouse over it, the text disappears and you can start typing as usual: If you want to see how it looks like, you can check out this link: N ...

What is the best way to present the new segment?

Having trouble with creating new sections or content after the homepage? The issue is that the new content doesn't appear on a new page but rather on the homepage itself. An example of this problem can be seen on this CodePen project, where an h1 sect ...

Detecting changes in a readonly input in Angular 4

Here is a code snippet where I have a readonly input field. I am attempting to change the value of this readonly input from a TypeScript file, however, I am encountering difficulty in detecting any changes from any function. See the example below: <inp ...

CSS/SCSS/JS: Adjusting header height dynamically while keeping it fixed at the

Check out my Codepen demo here: https://codepen.io/nickfindley/pen/dJqMQW I am seeking to replicate the current behavior of the page without specifying a fixed height for the header. The goal is to make the header adjust dynamically based on the content, ...

Conceal an element along with its space, then signal the application to show alternative content using React

Greetings everyone! I seek assistance with a React Application that I am currently developing. As a newcomer to the Javascript world, I apologize if my inquiry seems trivial. The application comprises of two main elements: a loader, implemented as a React ...

CSS: text positioning issue observed in Mozilla browser

I am facing an issue with the text position inside the box/button element on my website. The text appears correctly in Chrome, but in Firefox, it is positioned above the box, making it difficult to read. Can someone assist me with fixing the CSS or identi ...

"Even rows are the only ones being highlighted on the table

I created a table that dynamically highlights all odd rows. To achieve this, I implemented a method to identify the row number and then apply the alt class to those specific rows. In order to highlight the rows on hover, I added a simple :hover selector ...