CSS nested flex box height - css

I have a row layout with a nested flex container in the second flex item of the root container. I want the nested flex container to be 100% of the used height of its parent.
<div class="steps root"><!--root container-->
<div class="step one">
I am very tall. Others should be as high as I am!
</div>
<div class="step two-and-three">
<div class="steps"><!--nested container-->
<div class="step two">
nested child 1. should have 100% vertical height
</div>
<div class="step three">
nested child 2. should have 100% vertical height
</div>
</div>
</div>
Here is the fiddle demonstrating the issue: http://jsfiddle.net/2ZDuE/316/
If I set an explicit height to my nested flex container, then everything works.
How to tell the nested container to automatically fill up vertical space of its container?

add display:flex to .step.two-and-three and remove padding-bottom
.steps.root {
width: 700px;
}
.steps.root>.step.one {
height: 300px;
/*simulate big content in left column*/
}
.steps {
display: flex;
}
.step.one {
flex: 1 1 33%;
background-color: rgba(0, 0, 0, 0.1);
}
.step.two-and-three {
flex: 2 1 66%;
background-color: grey;
display: flex; /* added */
}
.step.two {
flex: 1 1 50%;
background-color: green;
}
.step.three {
flex: 1 1 50%;
background-color: lime;
}
.step.two-and-three>.steps {
background-color: olive;
padding-bottom: 10px;
}
<div class="steps root">
<!--root container-->
<div class="step one">I am very tall. Others should be as high as I am!</div>
<div class="step two-and-three">
<div class="steps">
<!--nested container-->
<div class="step two">nested child 1. should have 100% vertical height</div>
<div class="step three">nested child 2. should have 100% vertical height</div>
</div>
</div>
</div>

Related

Double wide element in a grid layout—how to maintain aspect ratio of child element?

I have a grid of four equal columns. Each column contains a child element that's sized using either padding-bottom or aspect-ratio and all the ratios are identical.
In my example, each child element has a ratio of 0.8 (200px x 250px) and the grid itself has gutters of 40px.
In one row, however, I want a column to span two columns, which is not a problem, but when I do that it seems I'm missing something when it comes to calculating the child element's new aspect ratio to insure that all child element heights match. I've tried this using both grid and flexbox, and using the padding-bottom method or aspect-ratio and the results are the same.
I simply add two widths of the base image size I'm using (200px) and then I add one gutter;
200px + 200px + 40px = 440px
Despite that, the only window width that the double column's child is the same height as the others is at 1015px.
What am I missing?
Code below and # https://codepen.io/cfxd/pen/bGvvVMr (with some JS help to see dimensions and ratios and you can switch the first element's class between padding and ar to switch between using padding-bottom and aspect-ratio)
function displayDimensions() {
const els = [
document.querySelector('.grid .item:nth-child(5) .ratio'),
document.querySelector('.grid .item:nth-child(6) .ratio'),
document.querySelector('.flex .item:nth-child(5) .ratio'),
document.querySelector('.flex .item:nth-child(6) .ratio'),
];
els.forEach((el) => {
el.innerHTML = `${el.getBoundingClientRect().width}x${el.getBoundingClientRect().height}<br>(ratio: ${el.getBoundingClientRect().width / el.getBoundingClientRect().height})`;
});
document.querySelector('.window').textContent = `window: ${window.innerWidth}x${window.innerHeight}`;
}
document.addEventListener('DOMContentLoaded', displayDimensions);
window.addEventListener('resize', displayDimensions);
:root {
--gutter: 40;
--gutter-px: calc(var(--gutter) * 1px);
--image-width: 200;
--image-height: 250;
}
*, :after, :before {
box-sizing: border-box;
}
body {
margin: 0;
padding: var(--gutter-px);
background: rgba(0, 0, 0, 0.05);
}
.window {
position: fixed;
top: 0;
left: 0;
background: #fff;
padding: 8px 12px;
}
.grid, .flex, .item, .ratio {
width: 100%;
}
.flex, .grid {
gap: var(--gutter-px);
}
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
--grid-gutter: calc(var(--gutter-px) * 3 / 4);
}
.grid .ratio {
background: rgba(255, 0, 0, 0.5);
}
.grid .item:nth-child(5) {
grid-column: span 2 / span 2;
}
.flex {
display: flex;
flex-wrap: wrap;
margin-top: var(--gutter-px);
width: 100%;
--col-width: 25%;
--col-gutter-px: calc(var(--gutter-px) * 3 / 4);
}
.flex .ratio {
background: rgba(0, 255, 0, 0.5);
}
.flex .item {
width: 100%;
max-width: calc(var(--col-width) - var(--col-gutter-px));
flex: 0 0 calc(var(--col-width) - var(--col-gutter-px));
}
.flex .item:nth-child(5) {
--col-width: 50%;
--col-gutter-px: calc(var(--gutter-px) / 2);
}
.padding .ratio {
height: 0;
padding-bottom: calc(var(--image-height) / var(--image-width) * 100%);
}
.padding .grid .item:nth-child(5) .ratio {
--image-width: 440;
}
.padding .flex .item:nth-child(5) .ratio {
--image-width: 440;
}
.ar .ratio {
aspect-ratio: 0.8;
}
.ar .grid .item:nth-child(5) .ratio {
aspect-ratio: 1.76;
}
.ar .flex .item:nth-child(5) .ratio {
aspect-ratio: 1.76;
}
<div class="padding">
<div class="window"></div>
<div class="grid">
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
</div>
<div class="flex">
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
<div class="item">
<div class="ratio"></div>
<p>Text here</p>
</div>
</div>
</div>
https://codepen.io/cfxd/pen/bGvvVMr
I worked out a bit of a hack to get this working. Initially, I decided to set a fixed height for each box but using a relative unit like vw. Unfortunately, that required a lot of responsive adjustments.
What ultimately worked was creating essentially a duplicate of the grid within a double wide grid item, with two columns, and all box ratios—even the two boxes inside the double wide item—are set to the desired aspect ratio. This creates a seamless, uniform looking grid, except within the double wide element I added another box (3 boxes total) which I then position absolute and stretch to equal the double width and single height.
Unfortunately there are two boxes in use that aren't exactly semantic, but it does the trick.
Here's the working grid with a double wide child element that maintains equal heights with its neighbors: https://codepen.io/cfxd/pen/NWYmRpY
Edit: turns out it can be done with only one additional, non-semantic box: https://codepen.io/cfxd/pen/gOeyLmB
Edit 2: adding a more realistic codepen: https://codepen.io/cfxd/pen/QWmPdLN

pushing icons closer together in footer gone wrong?

im trying to create a footer with a few social media icons...however, the method i've tried has resulted in the following problem :
expectation:
reality:
as you can see i'm failing in bringing the social media icons closer together... i tried setting the columns flex % to - however that pulls everything closer together towards the left of the screen...
here is my .row and .column as well as the social media icons..
.row {
display: flex;
}
.column {
flex: 30%;
padding: 00px;
}
.marginauto1 {
margin: 30px auto 20px;
display: block;
horizontal-align; middle;
vertical-align: middle;
}
<footer class= "marginauto1">
<center><div class="row">
<div class="column">
<center><a class= "pointer" href="twitter_url">
first time taking a stab at this on my own and I'm just really stuck here.. any help or tips would be appreciated!
If you want to put all your icons together in the middle:
img{
height:50px;
}
div{
display:flex;
justify-content: center;
background-color:blue;
}
<footer>
<div>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAA81BMVEXgT1////9ktfYAiXvc3Nzg4ODeP1L4+Pj44OL22tzePVDgTF367O3fSVrn5+ffRFb5/P+x2ftdsvZ/wvj/7ljjZXJ7u8LhVWX99fbv7u7iW2rogozsmKHqipT87u/21NfwrrTtn6fzwcfoiJHmeoXlbnzzwMXyuL31zdHupa3mc3/f5uX/8FG93/v/8UlbsvxOqtgAhnHs3t/fzc7goabktbnekpjlsrfixciMyfh1u+uYxs2ozbehy8nx6mu71Kd+v+WNwtrE2Jrf43m+17LV3opqt/GCv9oXj4ogk5pEo8Q8orlNqtIZkJBIqMtYr+MvmqW74o8pAAALkUlEQVR4nOWdeXvbNhKHyag6KBBBq4ZLmqROU4dlrx1l2+1umzht0/U6drLO9/80C+qwKIkgcQxE2P79lSePo+j14JgZDAaWrVnNXsvpTwfDbuR5Yej7lu+HoedF3eFg2ndavabuL2Dp/HB3NDxNvBARQjBGCFkb0T9jTP8WhV5yOhy5Or+EJsKeMxmOSZuC4QzYoVJSTH9uPJw4PT1fRQNh4A4izye4iOyAFBPfiwZuAP91oAndWTxuiNFlKBvjeAY9ZEEJe5MkRFJ0W0oUJhPQ8QpHGE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O4ktmIUCxz1CxDSP3U8Oh7IyKhXOGOHCEdqv5xhyrxJQaoEqHtxtbx9n9sxbxuKByhbTtR4ziMuBE55V9HAyFlTI5gR2wlCnyKhDRunItXTIgI4cacMw7UREjt2NXo5ZCwq2Q/EELqj8dtLXsHIu0YoHYVgJC6ANMEQ09IjJMpSC0ZCCFldCILEJJ+VOQA1coBEdqpIbsWAVh2ECZWF8Z8S8ERUvUmSYiUIOm/DpOJcPxQJFBCKncWjxtypqTGa4zjmbTzwhA0IVXgDiLPF6OkdL4XDVwNdaoaCFP1nMlwTNoEY1wYaSFEf4L+3Hg4cUDH5laaCFdyR8PTxAsRISQlRRkwSkb/FoVecjocQQ/MHWklTNXstZz+dDDsRp4Xhr5v+X4Yel7UHQ6mfafVa5Z/hJr+D8W0TFVSelAUAAAAAElFTkSuQmCC">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRyFcxKQYRAhR9esHvHVZokZGCYFNC1_rMHhw&usqp=CAU">
</div>
</footer>
<center> tag will not applicable if you are using flexbox. Instead flex-box itself has some properties that you can make use of. In your use-case you can go with justify-content: center
.row{
display: flex;
justify-content: center;
}
.item{
color: red;
background: lightblue;
margin-left: 20px;
}
<footer class= "marginauto1">
<div class="row">
<div class="item 1">
<h1>Telegram</h1>
</div>
<div class="item 2">
<h1>Facebook</h1>
</div>
<div class="item 3">
<h1>Twitter</h1>
</div>
</div>
</footer>
If you don't want any space in between of 2 or more items you can remove the margin.
Its working as expected. You have defined .column with flex: 30%; which intrun will split into 3
flex-grow: 1;
flex-shrink: 1;
flex-basis: 30%;
So what have you done is allowing the element to grow till the available width with a minimum of 30% relative to the container. This is because the flex-basis is defined. This will be 33% each.
For your soution to work remove flex: 30%; from .column. This will take the width depending on the content.
If you want some gap between the element, try adding some margin or padding, I have used margin for that. Or you can set the width of column element.
Inorder to align the items center in horizontal axis, use justify-content: center; for the flex element.
Working Fiddle
.row {
display: flex;
justify-content: center;
}
.column {
padding: 0px;
margin-right: 20px;
}
.marginauto1 {
margin: 30px auto 20px;
display: block;
}
.pointer img {
width: 50px;
}
<footer class="marginauto1">
<div class="row">
<div class="column">
<a class="pointer" href="">
<img src="https://cdn1.iconfinder.com/data/icons/logotypes/32/twitter-512.png" alt="">
</a>
</div>
<div class="column">
<a class="pointer" href="">
<img src="https://cdn1.iconfinder.com/data/icons/logotypes/32/twitter-512.png" alt="">
</a>
</div>
<div class="column">
<a class="pointer" href="">
<img src="https://cdn1.iconfinder.com/data/icons/logotypes/32/twitter-512.png" alt="">
</a>
</div>
</div>
</footer>

Width of flex item is shrinking [duplicate]

This question already has an answer here:
flex items ignoring width
(1 answer)
Closed 1 year ago.
I have a DIV with "display: flex;" that contains 2 child DIVs next to each other. The first one is just a DIV that contains some ordered list items and the second DIV contains some images. The second DIV has display: flex and flex-wrap: wrap applied to it.
For some reason, when I give the first DIV a width of 200px, it's not working... it doesn't get a width of 200px. But if instead I give it a min-width of 200px, then it works and gets a width of 200px. I need to understand why does width not work while min-width works...
/* Parent container that contains 2 child DIVs */
.parent_flex_container {
display: flex;
}
/* First child DIV */
#desktop_sidemenu {
width: 200px;
}
/* Second child DIV */
.flex_container {
display: flex;
flex-wrap: wrap;
}
/* Images of the second child DIV */
.Cac {
max-width: 50%;
height: 50%;
margin-bottom: 20px;
padding: 0 5px;
}
<div class="parent_flex_container">
<div id="desktop_sidemenu">
<nav>
<ul>
<li>Arabic</li>
<li>Green tea</li>
</ul>
</nav>
</div>
<div>
<div id="Featured_grid">
<div class="grid_title_holder">
<h2>Featured</h2>
</div>
<div class="flex_container">
<div class="Cac">
<img src="images/cover1.jpg" alt="" title="" />
<p class="Artist_name_holder">Artist Name</p>
<p class="Song_title_holder">Song Title</p>
</div>
<div class="Cac">
<img src="images/cover2.jpg" alt="" title="" />
<p class="Artist_name_holder">Artist Name</p>
<p class="Song_title_holder">Song Title</p>
</div>
<div class="Cac">
<img src="images/cover1.jpg" alt="" title="" />
<p class="Artist_name_holder">Artist Name</p>
<p class="Song_title_holder">Song Title</p>
</div>
<div class="Cac">
<img src="images/cover2.jpg" alt="" title="" />
<p class="Artist_name_holder">Artist Name</p>
<p class="Song_title_holder">Song Title</p>
</div>
<div class="Cac">
<img src="images/cover1.jpg" alt="" title="" />
<p class="Artist_name_holder">Artist Name</p>
<p class="Song_title_holder">Song Title</p>
</div>
<div class="Cac">
<img src="images/cover2.jpg" alt="" title="" />
<p class="Artist_name_holder">Artist Name</p>
<p class="Song_title_holder">Song Title</p>
</div>
<div class="Cac">
<img src="images/cover1.jpg" alt="" title="" />
<p class="Artist_name_holder">Artist Name</p>
<p class="Song_title_holder">Song Title</p>
</div>
<div class="Cac">
<img src="images/cover2.jpg" alt="" title="" />
<p class="Artist_name_holder">Artist Name</p>
<p class="Song_title_holder">Song Title</p>
</div>
</div>
</div>
</div>
</div>
So let's start with this when you using flexbox each child items will have 3 different elements which are:
flex-basis
flex-grow
flex-shrink
Where each of them has a different responsibility.
flex-basis
Flex-basis controls the default size of an element before it is manipulated by other Flexbox properties. It could be used as a width or height property. Let's see the below example:
.flexbox {
display: flex;
}
.red {
background: red;
flex-basis: 100px;
}
.green {
background: green;
flex-basis: 100px;
}
.blue {
background: blue;
flex-basis: 100px;
}
<div class="flexbox">
<div class="red">1</div>
<div class="green">2</div>
<div class="blue">3</div>
</div>
flex-grow
Now, when it comes to the property called flex-grow, the default is 0. That means the squares are not allowed to grow to take up space in the container. You can see the example below for more resolution:
.flexbox {
display: flex;
}
.red {
background: red;
flex-grow: 0;
}
.green {
background: green;
flex-grow: 0;
}
.blue {
background: blue;
flex-grow: 0;
}
<div class="flexbox">
<div class="red">1</div>
<div class="green">2</div>
<div class="blue">3</div>
</div>
So whats make a difference here, Well, let’s try incrementing flex-grow to 1 for every square:
.flexbox {
display: flex;
}
.red {
background: red;
flex-grow: 1;
}
.green {
background: green;
flex-grow: 1;
}
.blue {
background: blue;
flex-grow: 1;
}
<div class="flexbox">
<div class="red">1</div>
<div class="green">2</div>
<div class="blue">3</div>
</div>
flex-shrink
Flex-shrink is the opposite of flex-grow, determining how much a square is allowed to shrink. ts main use is to specify which items you want to shrink, and which items you don’t. By default, every square has a flex-shrink of 1. To see how shrink works see the below snippet:
.flexbox {
display: flex;
}
.red {
background: red;
flex-grow: 0;
flex-basis: 100px;
flex-shrink: 1;
}
.green {
background: green;
flex-grow: 0;
flex-basis: 100px;
flex-shrink: 0;
}
.blue {
background: blue;
}
<div class="flexbox">
<div class="red">1</div>
<div class="green">2</div>
<div class="blue">3</div>
</div>
NOTE: you have to resize the browser to see the difference in the above squares.
Actual answer
So your problem lies here in the flex-shrink property, hence default value of flex-shrink is 1 so it will look like this:
#desktop_sidemenu {
width: 200px;
flex-shrink: 1; /* default setting of browser */
}
So that is why the width property alone does not do the work for you which means your first div will be shrinked in order to prevent the boxes get out of order, then all you have to do to make it work is to give it a min-width: 200px or flex-shrink: 0.
NOTE: there is a shorthand for these three property called flex which you can give these three properties as one with the following order:
flex: 0 0 200px; /* flex-grow flex-shrink flex-basis */
For more information about these kinds of stuff, you can follow the MDN here.
I need to understand why does width not work while min-width works...
Because flex items are set, by default, to flex-shrink: 1, which means that they can shrink in order to minimize overflow of the container.
Your actual code (what the browser sees) is this:
#desktop_sidemenu {
width: 200px; /* author defined */
flex-shrink: 1; /* default setting */
}
You need to disable flex-shrink:
#desktop_sidemenu {
width: 200px;
flex-shrink: 0;
}
Now, because the item can't shrink below 200px, it's equivalent to min-width: 200px.
For more details, see "The flex-shrink factor" in my answer here:
What are the differences between flex-basis and width?

Automatically set height based on percentage width in flex display [duplicate]

This question already has answers here:
Maintain the aspect ratio of a div with CSS
(37 answers)
Closed 5 years ago.
I have 4 elements display flex. Are display as columns. Standard zurb foundation grid 25% width.
This elements are empty because their role is be container for image in background (I need to cover this is reason why I don't use img).
Exists in pure css3 manner to make this empty div's automatically same height.
width:25% and when this is 250px height should be 25%.
I can't do it in JS but...it would be great in pure css but my head is empty.
<div class="instagram-recent grid-x">
<div class="cell large-3 medium-3 small-12 instagram-individual-image-container" style="background-image: url('https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/21041955_144748202784493_5173738332273770496_n.jpg';)>
<div class="cell large-3 medium-3 small-12 instagram-individual-image-container" style="background-image: url('https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/21041955_144748202784493_5173738332273770496_n.jpg';)>
<div class="cell large-3 medium-3 small-12 instagram-individual-image-container" style="background-image: url('https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/21041955_144748202784493_5173738332273770496_n.jpg';)>
<div class="cell large-3 medium-3 small-12 instagram-individual-image-container" style="background-image: url('https://scontent.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/21041955_144748202784493_5173738332273770496_n.jpg';)>
</div>
css is Zurb Foundation 6.4.2 and for each
.instagram-individual-image-container
bacground-size is cover
for cell large-3 width is 25% I'd like make .instagram-individual-image-container height 100% of their current width.
Thanks in advance for any hints.
You could use :before and add padding:
html, body {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
}
#parent {
display: flex;
flex-flow: row;
}
.child:before{
content: '';
padding: 50% 0; /* vertical value as 100% equals width */
display: inline-block;
}
.child {
width: 25%;
}
#child1 {
background: red;
}
#child2 {
background: blue;
}
#child3 {
background: green;
}
#child4 {
background: yellow;
}
<div id="parent">
<div class="child" id="child1"></div>
<div class="child" id="child2"></div>
<div class="child" id="child3"></div>
<div class="child" id="child4"></div>
</div>
You can do it like this, however the content that you wanna put inside the divs, must be placed with position:absolute;
https://jsfiddle.net/4jz26uyn/1/

How to equalise sibling div heights?

<div class="wrapper">
<div class="sidebar-wrapper"></div>
<div class="content-wrapper"></div>
</div>
The height of content-wrapper is dynamic (auto). Is there any way to get the height of it and use it for the sidebar-wrapper so that it looks nice?
Displaying them like table cells would do it. Table cells can adjust their height automatically to the content, and all cells on the same row get the same height. If you give the side bar a fixed width (which is likely), you can easily get the content wrapper to fill the remaining space.
Whichever has the longest content will determine the height.
.wrapper {
width: 100%;
display: table;
}
.sidebar-wrapper,
.content-wrapper {
display: table-cell
}
.sidebar-wrapper {
width: 200px;
background-color: silver;
}
.content-wrapper {
background-color: yellow;
}
<div class="wrapper">
<div class="sidebar-wrapper">Here is <br>content<br>that is .....<br><br><br><br><br><br>Quite long</div>
<div class="content-wrapper">Smaller content</div>
</div>
Flexbox is an alternative to #GolezTrol's CSS tables.
.wrapper {
display: flex;
}
.sidebar-wrapper {
width: 200px;
background-color: silver;
}
.content-wrapper {
background-color: yellow;
flex: 1;
}
<div class="wrapper">
<div class="sidebar-wrapper">Here is
<br>content
<br>that is .....
<br>
<br>
<br>
<br>
<br>
<br>Quite long</div>
<div class="content-wrapper">Smaller content</div>
</div>

Resources