This question already has answers here:
One flex/grid item sets the size limit for siblings
(6 answers)
How can you set the height of an outer div to always be equal to a particular inner div?
(2 answers)
Closed 2 years ago.
I have a page like this :
<div id="parent">
<div id="childA">
List of some contents
</div>
<div id="childB">
The main content
</div>
</div>
#childA{
display: inline-block;
}
#childB{
display: inline-block;
}
I want to set the height of the parent div to the height of childB, but not childA.
If childA as a scrollBar in case it gets bigger than childB.
EDIT : I just realised I can approach the problem from a different angle. I hope it helps you understanding what I want. I need to set the height of a div (childA) according to the height of the div next to it (childB).
#childB{
overflow-y: scroll;
}
The height of both childs vary, but I want my parent's height equals to childB heigth.
I tried using table :
#parent{
display: table;
}
#childB{
display: table-row;
}
But childA keep increasing parent's height when it is bigger than childB
P.S. I know I can use JavaScript, but I would prefer not to use it.
I hope below example gives you some idea on how having some height and overflow-y on childA helps.
#parent {
display: flex;
border: 1px solid black;
}
#childA {
overflow-y: scroll;
height: 60px;
}
#childB {
}
<div id="parent">
<div id="childA">
List of some contents
List of some contents
List of some contents
List of some contents
List of some contents
</div>
<div id="childB">
The main content
The main content
The main contentThe main contentThe main content
The main content
The main content
The main content
The main content
</div>
</div>
Related
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>
This question already has answers here:
How to have child div of a flex, have its children full height [duplicate]
(1 answer)
Percentage Height HTML 5/CSS
(7 answers)
Closed 2 years ago.
I have been stuck on this and frankly don't even know how to google it, all my google efforts were fruitless. My HTML is as below:
<div class="uk-flex uk-flex-column uk-height-1-1">
<!-- normal div with height 100vh and flexbox of flex column -->
<div>div that will have height fit contents</div>
<div class="uk-flex-1">
<!-- div that will fill the remaining space -->
<div class="uk-height-1-1">div that should fill the height of the parent</div>
</div>
<div>div that will have height fit content</div>
</div>
Now my main problem is having the grand child div (.uk-height-1-1) to have its height fill the parent, how do I make its height fill the height of the parent div??
NOTE: The below links of questions I have been through them before, they do not answer my question
Fill remaining vertical space with CSS using display:flex
Make a div fill the height of the remaining screen space
UPDATE: Am using uikit, I posted the question initially the way it is to simplify the question, uk-height-1-1 = height: 100%
Best way to tackle something like this is assign borders to things when you are trying to see how the layout is
.viewport-height {
display: flex;
border: 1px solid blue;
height: 500px;
}
.flex-1 {
border: 1px solid red;
}
.full-height {
border: 1px solid greenyellow;
height: 100%;
}
If you look in the css above the .full-height is now the same height as the flex-1
If I understood your question, then this should be what you are looking for. Let me know if you need any additional help.
.viewport-height {
height: 100vh;
display: flex;
flex-direction: column;
}
.flex-1 {
display: flex;
flex: 1;
background-color: green;
align-items: center;
}
.div-with-height {
height: 50px;
}
full-height {
height: 100%;
}
<div class="viewport-height flex-column">
<!-- normal div with height 100vh and flexbox of flex column -->
<div class="div-with-height">div that will have height fit contents</div>
<div class="flex-1">
<!-- div that will fill the remaining space -->
<div class="full-height">div that should fill the height of the parent</div>
</div>
<div class="div-with-height">div that will have height fit content</div>
</div>
This question already has answers here:
Can media queries resize based on a div element instead of the screen?
(11 answers)
Closed 3 years ago.
I have a certain scenario like follows:
You have three inline-elements, two are the same essentially, but the one in the middle is not and its width will always be unknown.
You want either two things:
All three elements are inline on the same row
If the width is two
small, all three elements are stacked on top of each other
With display: inline-element, it will collapse as needed, meaning at a certain width two elements will be on top while one is on bottom. This isn't any of the desired conditions.
.div-a {
background-color: red;
}
.div-b {
background-color: blue;
}
div {
display: inline-block;
font-size: 4em;
}
<div class="div-a">
L
</div>
<div class="div-b">
Mid
</div>
<div class="div-a">
R
</div>
How can we make sure that either all blocks are stacked, or they are all inline? Remember the middle element will always be unknown width.
EDIT: Don't know why this was marked as duplicate, as I didn't even ask about media query resizing, that's just one of the solutions.
it depends on your viewport and elements width. as you put display inline element in div all div will be treated as an inline element. until you apply any new display property in any specific div. to make sure it comes to stacked, you can use display flax property.
Please read this article. Hope it will help you.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
use display flex to stay in one row and manage dynamically width middle element
<div class="a">
<div class="div-a">
L
</div>
<div class="div-b">
Midghghghghchcghgdc
</div>
<div class="div-a">
R
</div>
</div>
css
.a {
display:flex;
}
.div-a {
background-color: red;
}
.div-b {
background-color: blue;
}
div {
display: inline-block;
font-size: 2em;
}
This question already has answers here:
CSS when inline-block elements line-break, parent wrapper does not fit new width
(2 answers)
Closed 6 years ago.
EDIT. The question is reworded as suggested by #Aziz in his comments. It was previously asking about achieving the same effect with inline-block, which seems impossible, see the answer to
CSS when inline-block elements line-break, parent wrapper does not fit new width. The new question asks how else this effect can be achieved, so it is no more duplicate.
I have a parent row container with two child columns. I want each of the children to wrap their text content and then shrink to fit their width after the wrapping.
The example below almost achieves this effect:
.parent {
display: inline-block;
}
.child {
max-width: 180px;
display: inline-block;
border: solid thin;
}
<div class="parent">
<div class="child">
I am child with loooong text.
</div>
<div class="child">
I am child with loooong text.
</div>
</div>
Here is a Demo
The only problem is that each child does not completely shrink to fit its content. There is clear white space gap on the right of the text inside each child.
To achieve your desired effect, you just need to make sure that .child is a block element. that way it will occupy the parent's width and wrap the rest under it.
.parent {
border:1px solid red;
display: inline-block;
}
.child {
border:1px dashed red;
}
<div class="parent">
I shrink to content slightly longer
<div class="child">
incl.Child
</div>
but not enough!
</div>
https://jsfiddle.net/azizn/021xqwo4/
Say I have the following set up:
Some header text
a content div
Inside one parent div.
.outer {
max-height: 200px;
background-color: green;
}
.inner {
min-height: 50px;
/*for demonstration, the default height is set to a value greater than the outer's max-height. Normally, this wouldn't be the case intially */
height: 250px;
background-color: pink;
}
<div class="outer">
<span> Hi!</span>
<div class="inner"></div>
<!-- Placing a footer here, only to demonstrate that the content overflows (the background would be green, if it was inside the parent div) -->
<!-- You can ignore the existence of the footer in your answer -->
<span> bye! </span>
</div>
On the content div, I would like to specify a default height. However, imagine that the header text is actually a div, and its height may expand dynamically, pushing the content downwards.
This will cause the content to overflow eventually.
Rather than having the content overflow, I would like the inner div to shrink to fit within its parent's max-height, until the inner div hits its min-height, then it can overflow.
How can I achieve this?
You can try using Flexbox. Flexbox browser support.
Flexbox Solution:
.outer {
background-color: green;
display: flex;
flex-direction: column;
max-height: 200px;
}
.inner {
background-color: pink;
overflow-y: scroll;
}
<div class="outer">
<span> Header! <br /> <br /> <br /> Line Breaks to demonstrate a tall header.</span>
<div class="inner"><p>On the content div, I would like to specify a default height. However, imagine that the header text is actually a div, and its height may expand dynamically, pushing the content downwards.</p>
<p>This will cause the content to overflow eventually.</p>
<p>Rather than having the content overflow, I would like the inner div to shrink to fit within its parent's max-height, until the inner div hits its min-height, then it can overflow.</p> </div>
<!-- Placing a footer here, only to demonstrate that the content overflows (the background would be green, if it was inside the parent div) -->
<!-- You can ignore the existence of the footer in your answer -->
<span> Footer! </span>
</div>