What is the counterpart of `width: max(100%, fit-content)`?

In order for my div to adjust its size based on the content if the parent div is smaller than the content,
and match the size of the parent otherwise,

I have been trying to achieve the first scenario by using width: fit-content,
and the second scenario using width: 100%,

I attempted to combine these with width: max(100%, fit-content),
but it appears that this approach is not effective.

Does anyone have any suggestions on how to accomplish this properly?

Answer №1

When setting the width to either max(100%, fit-content) or fit-content,
it is essentially the same as:

Consequently, the following code achieves the same result:

width: fit-content;
min-width: 100%;

Answer №2

Instead of attempting to determine the maximum between 100% and adjusting to fit the content, consider setting the minimum width of your <div> to match its parent's size. By setting the container in question (represented by the blue box below) as an inline-block with a minimum width of 100% relative to its parent (the red box), I was able to achieve the desired outcome.

This approach ensures that the container always occupies 100% of its parent's width unless the parent is resized to be smaller than the inner content. In such cases, the container will not shrink beyond the content's width, necessitating scrolling within the parent (red box).

This technique is effective when using the display properties flex and inline-block, but not with block. Perhaps someone here can offer insight into why this limitation exists.

For demonstration purposes, I have created an example where the parent (red box) can be resized to observe the solution in action:

.outer {
  width: 500px;
  resize: horizontal;
  overflow: auto;
  border: 5px solid red;
}
.inner {
  display: inline-block; /* or flex */
  min-width: 100%;
  border: 5px solid blue;
  box-sizing: border-box;
}
.content {
  white-space: nowrap;
  background-color: lightGrey;
}
<div class="outer">
  <div class="inner">
    <div class="content">For the blue box, the maximum aligns with the red box and the minimum aligns with the content</div>
  </div>
</div>

Answer №3

To limit the width of an element, you can use the max-width property.

.container{
  width:100px;
  border:1px solid black;
}
.content{
  width:200px;
  max-width:100%;
  height:20px;
  background-color:orange;
}
<div class="container">
  <div class="content"></div>
</div>
<br>
<div class="content"></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

Bootstrap form validation solution

Utilizing bootstrap validation to validate a jsp page. The folder structure is as follows: WebContent ├── bootstrap-form-validation ├── js └── pages All three folders are under the web content. If I create another folder called teacher ...

Animation using jQuery is functional on most browsers, however, it seems to

After creating an animation to simulate an opening door using jQuery, I discovered that it works perfectly on Firefox 24, Chrome 28, and IE 8. However, Safari presents a problem - the door opens but then the "closed" door reappears at the end of the animat ...

Image control failing to display SVG file

I am facing an issue with displaying an SVG file on the img control in angular10. When I try to use SVG, it's not showing up but it works fine when I change the file type to jpg for the same control. The img control is placed inside a nav bar. Here i ...

Swap out a div identifier and reload the page without a full page refresh

I am interested in learning how to dynamically remove a div id upon button click and then add it back with another button click to load the associated CSS. The goal is for the page to refresh asynchronously to reflect these changes. So far, I have successf ...

CSS designs that adapt with the screen: Triangles in

I'm currently working on creating a responsive triangle using CSS. I want both the height and width of the triangle to adjust as the window size changes. Below is my static code: #triangle { z-index: 2; width: 0; height: 0; position:absolute ...

At times, the Kendo UI Tooltip may linger on screen longer than expected, failing to disappear as

I've been experimenting with this issue for quite some time now, but I'm stumped. Whenever I quickly move my mouse cursor over a series of links, occasionally a tooltip will linger on the screen even after the cursor has moved away from the link. ...

How can I align two inner boxes vertically in the most efficient manner?

.dd{ height:100px; width:100%; border:1px soild red; background:#000; } .dd .d1{ height:20px; width:20px; border:1px solid green; display:inline-block; } .dd .d2{ height:20px; width:20px; border:1px solid green; display:inl ...

Show an error notification if the mobile device does not meet the requirements for jquery mobile version

Currently, I am in the process of creating a website using HTML5, CSS3, and jquerymobile. My goal is to have an error message appear saying "Your mobile browser does not support this application" if the page is not rendering correctly or if the jquery mobi ...

Modify the background color of a pseudo-element's property value dynamically

How do I change the background color of my burger menu by clicking on an icon? This is the CSS code I have: :root{ --pseudo-backgroundcolor: yellow; } .menu__icon span, .menu__icon::before, .menu__icon::after{ background: va ...

How to implement a delay for submenu dropdown in Bootstrap 3

I am trying to implement a delay on a submenu that I have created, rather than the entire bootstrap3 dropdown menu. The goal is to allow users to easily move their cursor to the submenu without it closing unexpectedly. Although the jquery code should still ...

What is the best method to display an asterisk (*) in red while using React and Material UI

In my form, I want to indicate required fields with a red star (*). Is there a way to display the star in red color? Also, why does the border turn blue when I click on an input field? Here is the code and screenshot for reference: https://codesandbox.io/ ...

The Bootstrap 3 Navbar displays a #EEEEEE color when a link is hovered over

I have not specified a hover color in the stylesheet for links to be #EEEEEE. I want the navbar hover effect to blend in with the navbar background, creating a seamless transition when hovered over. For reference, here is my current stylesheet code: Paste ...

Incorporate CSS and JavaScript files into every page of NetSuite

Is there a way to globally apply a CSS file or JavaScript code to all NetSuite pages in order to change the page direction to RTL? I attempted adding it through: SuiteScript >> Client >> Deployment : All Records, While I was able to successfu ...

Trigger a series of functions upon clicking with ReactJS

Need some assistance with an alert message functionality. I have a button labeled Checkout, and when clicked, it should clear the cart and display an alert. At present, the individual functions work as expected - calling emptyCart() works fine, and calling ...

Activate Dropdown on hover and automatically close the menu when a link is clicked

Looking for assistance with a Dropdown menu that opens on mouse hover but needs to close when a link is clicked within the menu. Do you think JavaScript would be necessary for this function? .dropdow { position: relative; ...

Establishing space between table rows

I need to create a specific margin between two rows in a table. In my css code, I have the following snippet: background-color: #72c2dd; margin-top: 25px; The background-color is being applied correctly, but the margin-top property is not working as int ...

developing a fluid grid system with flexible ordering

I'm currently working on a section for my app that consists of one row with 3 columns, each column containing 3 divs. JSFIDDLE: demo Here is how it should appear in desktop view: https://i.sstatic.net/ugAXd.png And here is the expected appearance ...

Misaligned Div Content

Check out my jsfiddle here The text inside the <div> is shifting upwards Could someone explain why this is occurring? And suggest a solution? I would truly appreciate any assistance. Thank you! ...

A CSS selector that can target a neighboring element with the identical class

How can I use a CSS selector to highlight the last adjacent elements with the same class? In the example code below, the first three and last two elements share the same class. However, I am looking to specifically highlight Two, Threee and Six, Seven. & ...

The sub-menu options are constantly shifting... How can I keep them in place?

Here we go again... I'm having trouble with my sub-menu elements moving when I hover over them. I've searched for a solution but haven't found anything helpful. The previous advice I received about matching padding and adding transparent bo ...