Prevent child from from stretching parent container [duplicate] - html

This question already has answers here:
How to match width of text to width of dynamically sized image/title?
(2 answers)
Closed 3 years ago.
I have a fixed-position container element with two children. Both children contain text. I want one of the children to dynamically set the width of the container with its content. I want the other child's text to wrap appropriate based on that width.
For example:
.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 this example, I would like the container's width to match the shorter green .stretch div. I want the red .wrap div to have the same width, with the text wrapped inside, like:

The solution's come up with me was:
The child div needs to stretch its width depends on its content -> max-content
The parents's width needs to be as shrink as possible depends on its content -> min-content
The solution code with variant bahaviors:
.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>
You can read more about min-content and max-content from 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.

Related

Why flex element takes more place than it's text content and how to change it?

After breaking long words inside flex item, it's size doesn't shrink regardless of it's content.
<div class="parent">
<div class="outer">
<div class="inner1">Lots of text1 sdfsdfsdfsdftrtrtrtsddf</div>
<div class="inner2">Lots of text2</div>
</div>
</div>
.parent {
max-width: 300px;
}
div {
border: 1px solid black;
}
.outer {
display: flex;
}
.inner1 {
}
.inner2 {
https://jsfiddle.net/rfd0yh3t/21/
This is because div element is a block element and according to MDN Docs
A Block-level element occupies the entire horizontal space of its parent element (container), and vertical space equal to the height of its contents, thereby creating a "block".
And you can fix it by using min-content on the child container in it, this makes it takes the width of the longest word.`
You can check MDN Docs for more details

Parent flex doesn't shrink when using flex-basis and long text in child

I've got a parent flex that has a child which uses a flex-basis to wrap some text. I'd like for the parent to shrink to only the content of the child, but it instead takes up as much space as the text would take had it not wrapped.
.parent-flex {
display: flex;
background-color: blue;
}
.child-flex {
flex: 0 1 442px;
background-color: yellow;
}
<div class="parent-flex">
<div class="child-flex">
text lots and lots of text that goes on for a little while and wraps, ideally there should be no visible blue since the parent should fit the content exactly
</div>
</div>
I'd like for the above demo to show no blue at all. What can I do to shrink the parent-flex to the child-flex's current size?
Best solution I think is to use a max width, as it looks like you're fighting with the flexbox layout mode. You can slap a width of max content on the parent to stop it growing but I really don't see why when you can remove the background color.
.parent-flex {
width: max-content;
background-color: blue;
}
.child-flex {
max-width: 442px;
background-color: yellow;
}
<div class="parent-flex">
<div class="child-flex">
text lots and lots of text that goes on for a little while and wraps, ideally there should be no visible blue since the parent should fit the content exactly
</div>
</div>

Is it possible to have a css property that depends on the heights of specific elements? [duplicate]

This question already has answers here:
How can you set the height of an outer div to always be equal to a particular inner div?
(2 answers)
Closed 11 months ago.
I have an html container element which has children, some of which are of the css class child-active.
The container should only be as tall as the largest active child. i.e. I want a container's max-height to always be the largest height of its children which are of the class child-active. (The reason I want this is because it would solve a css transition issue for me).
I want something like:
.container {
max-height: calc(max(/* all the heights of .container > .child-active*/));
}
.child {
...
}
.child-active {
...
}
<div class="container">
<div class="child"> the height of this content is ignored </div>
<div class="child child-active"> the height of this content sets the height of the container </div>
</div>
Is this possible to do? Obviously I anticipate it will involve JavaScript.
Does having the .container element to be display:flex not solve the problem?
Otherwise, container queries might be worth checking out.
The simplest way would be to define the height of the child-active and use that same height as max-height for container, otherwise try playing around with max-height: max-content or max-height: auto or max-height: fit-content;
Or use display: flex, define the heights of child and active child and that should be it.
.container {
display: flex;
border: 1px solid red;
}
.child {
height: 20px;
border: 1px solid blue;
}
.child-active {
height:40px;
border: 1px solid green;
}
<div class="container">
<div class="child"> the height of this content is ignored </div>
<div class="child child-active"> the height of this content sets the height of the container </div>
</div>

How to center div to the display while having a dynamic width? [duplicate]

This question already has answers here:
How can I make a div not larger than its contents?
(43 answers)
Closed 2 years ago.
I have a div whose width I'd like to be dynamic based off of text that is inside the div. I've found that this is possible by setting the div's display to inline-block; however, now I can't get the div to center with the display. I don't want to use text-align: center on the div or the html body as I do not want the text inside the div to be centered. I'm open to having this problem fixed with or without inline-block. As long as the div is centered and the div width is dynamic based off of the text, then I'm happy.
<div id="wrapper">
<p>Text that will change div width.</p>
</div>
#wrapper {
display: inline-block;
}
Here's three different ways you can achieve this:
Play around with this code here
div.container {
width: fit-content;
margin: 0 auto;
}
p.content {
background-color: rgb(100, 0, 0);
color: white;
display: block !important;
padding: 10px;
}
p.content#fixed-width {
background-color: rgb(0, 100, 0);
width: 300px;
}
p.content#max-width {
background-color: rgb(0, 0, 100);
max-width: 55%;
}
<!DOCTYPE html>
<html>
<body>
<div class="wrapper">
<div class="container">
<p class="content">
Some text of whatever length you want, this paragraph does not have a defined width
</p>
</div>
</div>
<div class="wrapper">
<div class="container">
<p class="content" id="max-width">
Some text of whatever length you want, this paragraph has a defined max width
</p>
</div>
</div>
<div class="wrapper">
<div class="container">
<p class="content" id="fixed-width">
Some text of whatever length you want, this paragraph has a fixed width
</p>
</div>
</div>
</body>
</html>
#wrapper {
display: flex;
justify-content: center;
}
#wrapper > p {
flex: 0 0 auto;
padding: 10px;
background-color: #e8e8e8;
}
<div id="wrapper">
<p>Text that will change div width.</p>
</div>
Lot's of ways to accomplish this, set parent to flexbox container with display:flex and justify-content:center
// EDIT
When you set div to inline-block, you are telling the browser to render it as an inline element (mostly, basically), so you could eventually set text-align:center on the parent element and reset it again to text-align:left on #wrapper, this way wrapper will be centered, but paragraph left-aligned, yet it's not a good practice to change elements native's displays properties, so I would stick with flex approach.
You could use the css built in variable em and update every time the p element is edited like
// When changing the p element with document.querySelector("#wrapper > p") do
document.getElementById("wrapper").style.width = document.querySelector("#wrapper > p").innerHTML.length + "em";
The problem is you would have to insert that line of code every time you edit the p element.

100% width with div displayed as table-cell

I have been trying to figure out how to format a list that has elements (in my case buttons) on the sides. The major problem that I have had is that I do not want the list text to wrap around the buttons, I want it to behave more like a table; text should not wrap into other columns. To complicate things a little further, I do not necessarily know the sizes of the 'Side Elements' in advance.
This is the code I came up with:
<div style='display: table;'>
<div style='display: table-row;'>
<div style='display: table-cell'>
<!--- Variable Width Left Side Element --->
</div>
<div style='display: table-cell; width: 100%;'>
List Text
</div>
<div style='display: table-cell'>
<!--- Variable Width Right Side Element --->
</div>
</div>
</div>
JSFiddle
I have tried this on Firefox, Chrome, Safari and Internet Explorer and it seems to work fine. However, I am still concerned because I do not entirely understand why this works (and I do not like using code that I do not understand). My concern is with width: 100%. Shouldn't this specify that the cell should take up the entire parent, rather than all the space available in that row?
Ultimately, my question is this: Can I trust using width: 100% like this? Is this an appropriate way of solving my problem?
Ultimately, my question is this: Can I trust using width: 100% like this? Is this an appropriate way of solving my problem?
Yes, you can.
Tables are funny that way...when a cell is given a width of 100% it just means take up as much space as you can out of what is left.
So you've already allocated some of the 100% of the parent so the middle div just grabs what's left.
For what it's worth...a `flex-box alternative:
.row {
display: flex;
margin-bottom: .5em;
}
.row div {
padding-left: 5px;
padding-right: 5px;
}
.row div:first-child {
flex-basis: 55px;
}
.row div:nth-child(2) {
flex: 1;
}
.row div:last-child {
flex-basis: 35px;
}
<div class="container">
<div class="row">
<div style="background-color: red"></div>
<div>This text is not too long</div>
<div style='background-color: blue'></div>
</div>
<div class="row">
<div style='background-color: red'></div>
<div>This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long.</div>
<div style='background-color: blue'></div>
</div>
</div>
JSfiddle Demo
when you are mixing both absolute and relative measurements, its good to use calc() function. So the width of the middle element will be,
width: calc(100% - 90px)
100% being the full width of parent and 90px being the absolute measurements with you are excluding (55px + 35px).
Browser will automatically compute the actual width of the element based on the parent width (which is the available width minus 90px). Directly giving 100% is not the correct way, imho.
The width of a table-cell depends upon its content. The cells will expand based on their content unless table-layout: fixed is given on the table.
Instead of using tables you can use floats for example follow this link http://jsfiddle.net/osha90/e57dja5u/
<div >
<div class="clearfix" style="margin:10px 0px">
<div style='width: 55px; height: 35px; background-color: red;float:left'>
</div>
<div style='width: 35px; height: 25px; background-color: blue;float:right'>
</div>
<div style="overflow:hidden;">
This text is not too long
</div>
</div>
<div class="clearfix">
<div style='width: 55px; height: 35px; background-color: red;float:left'>
</div>
<div style='width: 35px; height: 25px; background-color: blue;float:right'>
</div>
<div style="overflow:hidden;">
This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long. This text is very long.
</div>
</div>
.clearfix:after,.clearfix:before{
content: "";
clear: both;
display: block;
}
Because table rows can't wrap, table cell with 100% width will force the cell to occupy as much space as he can. You other two cell have fixed width, because their content have a fixed width, and the a table cell minimum width is the one required to display the content. This means that the 100% width actual width is 100% - the two fixed cell.