Stop child element from extending parent container dimensions

I am facing a scenario where I have a fixed-position container element with two children. Both children contain text. The challenge is to dynamically adjust the width of the container based on one child's content, while ensuring that the other child's text wraps appropriately.

For instance:

.container {
  position: fixed;
}

.wrap {
  background: red;
}

.stretch {
  background: green;
}
<div class="container">
  <div class="wrap">
    this text is very very long
  </div>
  <div class="stretch">
    shorter text
  <div>
</div>

In the above scenario, the goal is for the container's width to be in line with the shorter green .stretch div. Subsequently, the red .wrap div should also adapt its width accordingly and wrap the text inside it.

https://i.sstatic.net/Xn5Zam.png

Answer №1

My solution was as follows:

  • The child div should have a width that depends on its content -> max-content
  • The parent's width should be as small as possible based on its content -> min-content

The code demonstrating different behaviors:

.container {
  width: min-content;
  
  border: 2px solid blue;
  margin: 5px;
}

.wrap {
  background: red;
  width: auto; /* default btw */
}

.stretch {
  background: green;
  width: max-content;
}
<div class="container">
  <div class="wrap">
    this text is very very long
  </div>
  <div class="stretch">
    shorter text
  </div>
</div>

<div class="container">
  <div class="wrap">
    shorter
  </div>
  <div class="stretch">
    shorter text
  </div>
</div>

<div class="container">
  <span class="wrap">
    shorter
  </span>
  <div class="stretch">
    shorter text
  </div>
</div>


To learn more about min-content and max-content, check out this answer or the specification.

  • max-content inline size: the narrowest inline size it could take while fitting around its contents if none of the soft wrap opportunities within the box were taken.

  • min-content inline size: the narrowest inline size a box could take that doesn’t lead to inline-dimension overflow that could be avoided by choosing a larger inline size. Roughly, the inline size that would fit around its contents if all soft wrap opportunities within the box were taken.

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

Code breaking due to Selenium locating element by class

When my application opens a login page, it looks for valid combinations. Initially, there is a button labeled as "check availability" that Selenium can locate and click on. If the username is already taken, a new button appears with the text "check anothe ...

Tips for positioning two divs side by side in block formation

I'm attempting to display two distinct div elements as blocks, one for the name and the other for the names. Here is the Fiddle The names block should be displayed as a separate block next to the name div, regardless of screen responsiveness. How ca ...

Feeling unsure about the descendant selector

Assume I'm using the selector below: #doctors h2 { /* Insert CSS Code Here */ } Does this selector refer to all <h2> elements that are children of elements with the id="doctor" attribute? ...

If there is no content present, the html element with contenteditable will have an <br>

I'm encountering an issue with a <div contenteditable="true"></div>. Whenever I enter content into it and then delete that content, the browser seems to automatically insert a <br> element into the div. Has anyone else experienced th ...

Customizing WordPress using CSS overrides in the Pinboard theme

I'm currently customizing a WordPress theme for my website (pinboard theme) and so far, it's been an amazing experience. One of the features of this theme is a built-in slider/carousel. However, I am looking to change the title of the slider from ...

What is the best way to limit the number of options displayed in the vue-multiselect

I'm looking to truncate multiple values on the vue-multiselect component. I attempted to override various classes without success, as shown in this example: .multiselect__content-wrapper { overflow-x: hidden; text-overflow: ellipsis; } ...

Is there a way to have a div element with a box-shadow effect on top of its h1 child element?

I am facing an issue where a div with a box-shadow and an h1 inside are not aligned properly, causing the shadow to not cover the h1 element. Below is the code snippet in question: h1 { position: absolute; top: 50%; left: 44%; -webkit-t ...

What could be the reason for an iOS web app to become unresponsive on the initial loading screen?

I recently completed the development of an HTML5 webapp, and during my local server tests on desktop browsers, iOS Safari, and as a homescreen bookmark webapp, everything ran smoothly. The offline functionality with the cache.manifest file also worked flaw ...

Trouble Scrolling Webpage Vertically on Screens Exceeding 768px Width

I've been searching high and low for an answer that seems to be right in front of me, but I just can't see it. The issue is quite simple: the page refuses to scroll vertically on screens wider than 768px: Whenever the screen size exceeds 768px, ...

Tips for coloring just the letters and excluding the Bengali Kar symbol

The Bengali language consists of 40 consonants, including ক, খ, গ, ঘ, ঙ, চ, ছ, জ, ঝ, ঞ, and more. Additionally, there are 10 vowels in the form of Kars, such as া, ি, ী, ু, ূ, ৃ, ে, ৈ, ো, and ৌ. My goal is to highli ...

Incorporate PHP conditional statements into HTML code

I am struggling with incorporating an "if else" statement into my php script. Specifically, I want the new_license_issued_date field to be null if it is currently pulling through as 01/01/1970. Can someone please provide guidance on this matter? Thank yo ...

Images overlaying text and each other in a cascading arrangement

I need help arranging an icon with a number on top in the left corner of a div container, alongside an image to the right. While it displays correctly on desktop, the mobile view overlaps the image on the right and the text on the left. Here is a snippet ...

Aligning text in the middle of two divs within a header

Screenshot Is there a way to keep the h4 text centered between two divs? I want it to stay static regardless of screen resolution. Currently, the icon and form remain in place but the h4 text moves. How can I ensure that it stays in one spot? <!doctyp ...

I'm looking for help on creating a three-column table using javascript/jquery. The table should display Product, Price, and Discount (which is calculated at 20%). Can anyone

Check out this code snippet. var items = ["Laptop", "Tablet", "Smartphone", "Headphones", "Camera"]; var costs = [599.99, 299.99, 799.99, 149.99, 499.99]; displayItems = ""; totalCost = 0; for (var j = 0; j < items.length; j++) { displayItems += " ...

Need help aligning content with flexbox? Having trouble with align-content and justify-content not working correctly?

First and foremost, I want to express my gratitude in advance for any assistance you can provide. My goal was to neatly align three pieces of content within each row, similar to the image displayed here: https://i.sstatic.net/vtsRj.png To achieve this lay ...

What are the best practices for formatting URLs in Python/Django?

Hello fellow developers, I've been working on a web application as part of my cs50w course and the code is almost there. However, I've run into an issue where the URL isn't being displayed correctly when using a specific function. Here&apo ...

CSS: Styling different <a href> tags to appear uniform within identical size containers

I currently have some links displayed on my webpage. These links are encapsulated within a border and have a background set for them, essentially creating a box around each one. <!DOCTYPE html> <style> a { background: #DDD; border: #BB ...

"What is the best way to use JavaScript to update several elements based on their class and incorporate distinct sub data for

Essentially, I am receiving a sequence of 5-10 events from an external server in the following format: event: add data: { "hostname":"name", "properties": {"info": "50"}} event: add data: { "hostname":"name2", "properties": {"info": "45"}} event: add da ...

Is the footer html/css duplicated on a different page and displaying differently?

I have been working on a website and just finished the main page with the header, body, and footer. Now, as I moved on to creating the second page, I encountered an issue. I copied and pasted the code for the header from the main page onto the second page ...

Trouble with displaying images in Bootstrap 4 cards

I am currently experimenting with Bootstrap 4 and trying to create cards, but I am facing issues with images not displaying on the cards. Currently, I only have one image in the first card, but it still does not render. You can view the code I have written ...