Flexbox - Same height columns inside row - css

Is it possible to make the columns inside each row the same height as each other? For example to make both the blue headings the same size and so on
Apologies if this has been asked before but I couldn't see an answer to this when searching.
<div class="row">
<div class="column">
<h2>heading</h2>
<div class="block-1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis, nesciunt.</div>
<div class="block-2">Lorem ipsum dolor sit amet.</div>
</div>
<div class="column">
<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod, maiores!</h2>
<div class="block-1">Lorem ipsum</div>
<div class="block-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. In quam praesentium suscipit laudantium illo voluptatibus eligendi, est exercitationem commodi reiciendis.</div>
</div>
</div>
.row {
display: flex;
}
.column {
display: flex;
flex-direction: column;
* {
flex: 1;
}
}
https://jsfiddle.net/vdLaq7t1/

You can do it with align-items: stretch (which is the default value for align-items), but you should redesign your HTML structure.
.column {
background: silver;
}
h2 {
background: cornflowerblue;
margin: 0;
}
.block-1 {
background: tomato;
}
.block-2 {
background: brown;
}
.row {
display: flex;
/* Not required because it's already the default value */
align-items: stretch;
}
.row > *{
flex: 1;
}
<div class="row">
<h2>heading</h2>
<h2>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod, maiores!</h2>
</div>
<div class="row">
<div class="block-1">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Debitis, nesciunt.</div>
<div class="block-1">Lorem ipsum</div>
</div>
<div class="row">
<div class="block-2">Lorem ipsum dolor sit amet.</div>
<div class="block-2">Lorem ipsum dolor sit amet, consectetur adipisicing elit. In quam praesentium suscipit laudantium illo voluptatibus eligendi, est exercitationem commodi reiciendis.</div>
</div>
Flexbox works in both X and Y axis. When you use flex-direction: column you are changing the main axis from X to Y. You can manage the main axis with the justify-content property, and the secondary axis with the align-items property.
The thing is that align-items work with the stretch value, but justify-content cannot.
So if you want the height to be the same (Y axis), you need to stretch it with align-items: stretch, but align-items only works in the secondary axis, so the main axis needs to be the X axis in this case, and that means that you cannot use flex-direction: column in order to make them the same height.

It can be done with many technics.
For example, you can place 6 columns in 1 row.
.row {
display: flex;
flex-wrap: wrap;
}
.column {
flex-basis: 70%;
}
.column.sm {
flex-basis: 30%;
}
.blue {
background: blue;
}
.silver {
background: silver;
}
.cornflowerblue {
background: cornflowerblue;
}
<div class="row">
<div class="column blue sm">heading</div>
<div class="column blue">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quod, maiores!</div>
<div class="column silver sm"><div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Inventore quaerat, facilis qui placeat! Voluptatem reprehenderit similique rerum officia iste error ab, animi nobis quaerat culpa possimus, nisi laboriosam aliquid hic.</div>
<div>Tempora eius eaque harum, temporibus sequi porro, minima quia, necessitatibus amet nisi unde reiciendis iure ipsa, facilis rerum qui dolores doloribus sed voluptatum! Dicta at, qui, exercitationem molestiae voluptas natus.</div></div>
<div class="column silver">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Iusto nemo ipsum voluptates mollitia eius enim, esse voluptatibus eaque doloremque vel asperiores quos unde similique rerum perspiciatis iure, ipsam eum soluta.</div>
<div class="column cornflowerblue sm">lorem</div>
<div class="column cornflowerblue"><div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore enim recusandae veniam optio delectus doloremque maiores quidem, impedit vel reprehenderit quam vitae, fugit atque assumenda molestiae debitis laboriosam blanditiis fugiat.</div>
<div>Excepturi ea minima accusantium delectus totam quae fugiat, ex eos inventore deleniti odit, commodi eveniet, eum ullam consectetur ipsa quasi odio similique. Doloribus tempore accusantium soluta, id deserunt maxime accusamus.</div></div>
</div>

Related

Limit the max-height to fit the content size

I have a resizable container, I want it to be able to be resized to fit the content inside, but not too large to waste space.
I got the code like
.container {
width: 200px;
height: 200px;
background: red;
resize: vertical;
overflow: hidden;
max-height: max-content;
}
.content {
background: yellow;
}
<div class="container">
<div class="content">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Saepe, molestiae? Officiis consequatur quod minima cum at ratione, sint ipsum architecto! Sit accusantium tenetur ducimus aut atque, eligendi in ullam sunt! Lorem ipsum dolor sit amet consectetur
adipisicing elit. Maxime explicabo dolor sequi veritatis aut. Recusandae quibusdam maiores doloremque natus, aspernatur minus dignissimos, id soluta amet, blanditiis illum odio ab alias.
</div>
</div>
I try different value of max-height, only fixed value such as 300px works, but it is not what I want. The requirement is that the red space can not be there, like the attachment. Any idea?
Try height: fit-content;
**UPDATE**
At first I didn't understand what you were trying to achieve, but now I'm pretty sure what you need here.
Unfortunately, I think that this is impossible without javascript (I may be wrong).
So here is simple example with ResizeObserver that looks for container and if this element height exceeds content height it will be shrinked back to max height of content.
disconnecting observer before change of height and reconnecting back after this change is mandatory because change of height will trigger ResizeObserver and we will got and infinite loop.
According to this issue, best workaround to prevent logging massive amount of errors into console, is to reconnect observer via requestAnimationFrame
const container = document.querySelector('.container');
const content = container.querySelector('.content');
const resizeObserver = new ResizeObserver(event => {
const maxHeight = window.getComputedStyle(content).height.replace('px', '');
const containerHeight = event[0].contentRect.height;
resizeObserver.disconnect();
if (containerHeight > maxHeight) {
container.style.height = maxHeight + 'px';
}
requestAnimationFrame(() => resizeObserver.observe(container));
});
resizeObserver.observe(container);
.container {
width: 200px;
height: 200px;
background: red;
resize: vertical;
overflow: hidden;
max-height: max-content;
height: fit-content;
}
.content {
background: yellow;
}
<div class="container">
<div class="content">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Saepe, molestiae? Officiis consequatur quod minima cum at ratione, sint ipsum architecto! Sit accusantium tenetur ducimus aut atque, eligendi in ullam sunt! Lorem ipsum dolor sit amet consectetur
adipisicing elit. Maxime explicabo dolor sequi veritatis aut. Recusandae quibusdam maiores doloremque natus, aspernatur minus dignissimos, id soluta amet, blanditiis illum odio ab alias.
</div>
</div>
I made a change to use JavaScript to set the max-height on the container with the offset set height from the content. That way it will not grow more than the content.
<div id="container" class="container">
<div id="content" class="content">
Lorem, ipsum dolor sit amet consectetur adipisicing elit. Saepe, molestiae? Officiis consequatur quod minima cum at ratione, sint ipsum architecto! Sit accusantium tenetur ducimus aut atque, eligendi in ullam sunt! Lorem ipsum dolor sit amet consectetur
adipisicing elit. Maxime explicabo dolor sequi veritatis aut. Recusandae quibusdam maiores doloremque natus, aspernatur minus dignissimos, id soluta amet, blanditiis illum odio ab alias.
</div>
</div>
<script>
var content = document.getElementById("content");
var container = document.getElementById("container");
container.style.maxHeight = content.offsetHeight + "px";
</script>

Centering multiple items in flexbox not working as expected [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 2 years ago.
I am trying to vertically content within a div with flexbox, so that it always remains centered.
However it is not working as expected. Interested to see where I'm going wrong.
Just to confirm, the 'left div' should display the contents in regular vertical order, not horizontally as is currently happening.
section {
display:flex;
}
div {
padding:12px;
width:400px;
height:300px;
display:flex;
align-items:center;
}
.left {background:#bada55}
.right {background:red}
<section>
<div class="left">
<h2>Very long title</h2>
<p>Caption goes here</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque at magni est facilis error! Perspiciatis ab tempore qui mollitia, nihil itaque praesentium deleniti hic asperiores soluta, rem natus. Quidem, necessitatibus?</p>
</div>
<div class="right">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa distinctio, labore, quod ex blanditiis totam esse eligendi consequatur inventore reprehenderit temporibus architecto molestias ratione, ullam deserunt tempora nesciunt corrupti! Tempora!</p>
</div>
</section>
Link to codepen
You can use flex-direction on .left to make it center vertically:
section {
display: flex;
}
div {
padding: 12px;
width: 400px;
height: 300px;
display: flex;
}
.left {
background: #bada55;
flex-direction: column;
justify-content: center;
}
.right {
background: red;
align-items: center;
}
<section>
<div class="left">
<h2>Very long title</h2>
<p>Caption goes here</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque at magni est facilis error! Perspiciatis ab tempore qui mollitia, nihil itaque praesentium deleniti hic asperiores soluta, rem natus. Quidem, necessitatibus?</p>
</div>
<div class="right">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa distinctio, labore, quod ex blanditiis totam esse eligendi consequatur inventore reprehenderit temporibus architecto molestias ratione, ullam deserunt tempora nesciunt corrupti! Tempora!</p>
</div>
</section>
EDIT based on your comment, I believe this works:
section {
display: flex;
align-items: center;
height: 300px;
}
div {
width: 400px;
padding: 12px;
}
div.right {
display: flex;
align-items: center;
}
<section>
<div class="left">
<h2>Very long title</h2>
<p>Caption goes here</p>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Cumque at magni est facilis error! Perspiciatis ab tempore qui mollitia, nihil itaque praesentium deleniti hic asperiores soluta, rem natus. Quidem, necessitatibus?</p>
</div>
<div class="right">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa distinctio, labore, quod ex blanditiis totam esse eligendi consequatur inventore reprehenderit temporibus architecto molestias ratione, ullam deserunt tempora nesciunt corrupti! Tempora!</p>
</div>
</section>

Article layout with CSS Grid: how to wrap text around images (or float them)?

I know that direct children of a grid item cannot be floated. I'm trying to create an article layout where something like this is achieved: pull an image to either side, and have the text flow around it.
Here's what I have so far on Codepen. With named template columns, I can have an image fill up only part of the width of the grid, but is there any way to let the surrounding text flow around it?
Hopefully my Codepen example makes sense; please let me know if I can provide additional information or clarification!
figure {
margin: 0;
}
img {
max-width: 100%;
height: auto;
}
article {
max-width: 800px;
margin: auto;
display: grid;
grid-template-columns: [wide-start] minmax(1em, 1fr) [main-start] minmax(0, 16em) [main-half] minmax(0, 16em) [main-end] minmax(1em, 1fr) [wide-end];
}
article>* {
grid-column: main;
}
figure {
grid-column: wide-start / main-half;
}
div {
grid-column: main-half / main-end;
padding-left: 1em;
}
<article>
<h1>Article</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore dolorem porro vero! In iste nemo repellendus? Doloremque eius officia beatae doloribus autem vitae enim qui. Minus, facere? Quaerat, laboriosam enim?</p>
<figure>
<img src="https://via.placeholder.com/500x300">
<figcaption>
<p>I wish the text could wrap around to my right</p>
</figcaption>
</figure>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Sunt, obcaecati, blanditiis ad nostrum autem atque error quasi, tempora debitis exercitationem illum. Maiores cum delectus, fugit repellat provident libero hic magni?</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates consectetur aspernatur est numquam alias consequatur praesentium quaerat totam dicta non asperiores similique inventore, a perspiciatis perferendis laudantium minus ratione animi!</p>
<figure>
<img src="https://via.placeholder.com/500x300">
<figcaption>
<p>Text is now in a column next to me (because I wrapped it in a <code><div></code>, but it won't wrap underneath me automatically</p>
</figcaption>
</figure>
<div>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Fugiat repellat consequatur, voluptas sint eum ea error sit hic dignissimos expedita totam suscipit officia consequuntur non, quaerat odio. Recusandae, consequatur maxime!</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Itaque eveniet, pariatur ipsa fuga hic nihil unde maiores, provident eius minima atque accusamus voluptate aspernatur perferendis. Ab rem inventore omnis at!</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. At vitae ex fugit sequi, natus asperiores? Laudantium optio, doloribus error dolores exercitationem aliquid esse, reiciendis mollitia alias vel illo, amet officiis?</p>
</div>
</article>
If I understood you correctly and you don't have to use only CSS Gird for the whole layout you can wrap your figure and paragraphs in another div, then trough that div give your figure max width of 50%, float:left, and some margin-right... you get the point.
figure { margin: 0; }
img { max-width: 100%; height: auto; }
article {
max-width: 800px;
margin: auto;
display: grid;
grid-template-columns:
[wide-start] minmax(1em, 1fr)
[main-start] minmax(0, 16em)
[main-half] minmax(0, 16em) [main-end]
minmax(1em, 1fr) [wide-end];
}
article > * {
grid-column: main;
}
.floating{
margin-bottom: 20px;
border-bottom: 1px solid #ddd;
}
.floating figure {
float:left;
max-width: 50%;
margin-right: 20px;
}
.floating p {
margin-top:0;
}
<article>
<h1>Article</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Labore dolorem porro vero! In iste nemo repellendus? Doloremque eius officia beatae doloribus autem vitae enim qui. Minus, facere? Quaerat, laboriosam enim?</p>
<div class="floating">
<figure>
<img src="https://via.placeholder.com/500x300">
<figcaption>
<p>I wish the text could wrap around to my right</p>
</figcaption>
</figure>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Sunt, obcaecati, blanditiis ad nostrum autem atque error quasi, tempora debitis exercitationem illum. Maiores cum delectus, fugit repellat provident libero hic magni?</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates consectetur aspernatur est numquam alias consequatur praesentium quaerat totam dicta non asperiores similique inventore, a perspiciatis perferendis laudantium minus ratione animi!</p>
</div>
<div class="floating">
<figure>
<img src="https://via.placeholder.com/500x300">
<figcaption>
<p>Text is now in a column next to me (because I wrapped it in a <code><div></code>, but it won't wrap underneath me automatically</p>
</figcaption>
</figure>
<div>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Fugiat repellat consequatur, voluptas sint eum ea error sit hic dignissimos expedita totam suscipit officia consequuntur non, quaerat odio. Recusandae, consequatur maxime!</p>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Itaque eveniet, pariatur ipsa fuga hic nihil unde maiores, provident eius minima atque accusamus voluptate aspernatur perferendis. Ab rem inventore omnis at!</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. At vitae ex fugit sequi, natus asperiores? Laudantium optio, doloribus error dolores exercitationem aliquid esse, reiciendis mollitia alias vel illo, amet officiis?</p>
</div>
</div>
</article>

center horizontally and vertically paragraph inside div

how i can center horizontally and vertically the paragraph inside the div , i came to this result
.Benefits {
min-height: 20%;
width: 100%;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}
<section class="Benefits">
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum n</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
</section>
.Benefits div {
min-height: 20%;
margin: auto;
width: 50%;
}
<section class="Benefits">
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum n</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
</section>
I have noted in my CSS what's going on here:
I think you're best using two cases of display: flex. One horizontally, one vertically.
.Benefits {
width: 100%;
max-width: 1248px;
margin: 0 auto; /* centers container */
padding: 10px; /* this combined with the margin in the divs will make the margins appear consistent */
box-sizing: border-box;
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
flex-direction: row; /* sort into rows */
align-items: center;
justify-content: space-between;
text-align: center;
}
.Benefits div {
width: calc((100% / 3) - 20px); /* calculate width for maximum accuracy | minus margin */
margin: 10px; /* space between boxes */
padding: 20px; /* space inside boxes */
box-sizing: border-box; /* calculates border and padding in width */
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
flex-direction: column; /* sort into columns */
align-items: center;
justify-content: center;
flex-grow: 1; /* forces singular div that falls beneath to be full width */
background-color: rgba(0,0,0,0.25);
}
#media screen and (min-width: 640px) and (max-width: 1023px) {
.Benefits div {
width: calc((100% / 2) - 20px); /* calculate width for maximum accuracy | minus margin */
}
}
#media screen and (max-width: 639px) {
.Benefits div {
width: 100%; /* calculate width for maximum accuracy | minus margin */
}
}
.Benefits div h1 {
margin-bottom: 20px;
}
<section class="Benefits">
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum n</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
<div>
<h1>Lorem ipsum </h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Omnis nemo quas dolor optio autem! Veniam, dolore neque nam harum quae excepturi distinctio optio explicabo ipsum laudantium nesciunt et autem a.</p>
</div>
</section>

Fixed div on the bottom of the scrollable div

I need a create 4 cards with non-changable height with buttons on the bottom, but there are different text amount in each card.
Position Fixed is connected to body element,
position absolute scrolls with content,
position sticky appears when content ends.
https://codepen.io/anatoli101/pen/NVzbGY Here is a basic exemple of my problem
.content{
display:flex;
width: 100%;
justify-content: space-around;
}
.fixed-div{
background-color: red;
height: 400px;
width: 300px;
overflow-y: auto;
}
.button{
position:sticky;
bottom:0;
background-color: grey;
width: 100%;
height: 30px;
}
You'll get a much nicer result (no occluded scrollbars) using flex:
.content {
display: flex;
justify-content: space-evenly;
height: 400px;
}
.container {
display: flex;
flex-direction: column;
flex: 0 1 250px;
margin: 0 5px;
}
.fixed-div {
background-color: red;
overflow-y: auto;
flex: 1 1 auto;
}
.button {
background-color: grey;
flex: 0 0 30px;
}
<h1>I need grey area fixed on the bottom, not dependent on text amount</h1>
<div class="content">
<div class="container">
<div class="fixed-div">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ad dolores aspernatur error rem aliquam omnis eveniet consequuntur asperiores, aut explicabo eaque eos quos facere necessitatibus sit est ipsam quae optio! Lorem ipsum dolor sit amet consectetur adipisicing elit. Itaque molestias natus officiis labore omnis corporis cupiditate quisquam nihil? Mollitia aperiam iusto iste minima ratione culpa veniam architecto facilis neque deleniti? Lorem, ipsum dolor sit amet consectetur adipisicing elit. Cumque aut, eum, quia facere inventore perferendis aperiam recusandae nihil at aliquam ipsum excepturi eligendi animi, nam vel unde. Suscipit, tenetur veritatis. Lorem ipsum dolor sit amet consectetur, adipisicing elit. Eaque, quam. Hic unde nam dolor debitis voluptates officia earum, nostrum ut quae recusandae voluptate eveniet commodi ipsa dolorum nulla totam molestiae?</p>
</div>
<div class="button"></div>
</div>
<div class="container">
<div class="fixed-div">
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Cumque aut, eum, quia facere inventore perferendis aperiam recusandae nihil at aliquam ipsum excepturi eligendi animi, nam vel unde. Suscipit, tenetur veritatis. Lorem ipsum dolor sit amet consectetur, adipisicing elit. Eaque, quam. Hic unde nam dolor debitis voluptates officia earum, nostrum ut quae recusandae voluptate eveniet commodi ipsa dolorum nulla totam molestiae?</p>
</div>
<div class="button"></div>
</div>
<div class="container">
<div class="fixed-div">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ad dolores aspernatur error rem aliquam omnis eveniet consequuntur asperiores, aut explicabo eaque eos quos facere necessitatibus sit est </p>
</div>
<div class="button"></div>
</div>
<div class="container">
<div class="fixed-div">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ad dolores aspernatur error rem aliquam omnis eveniet consequuntur asperiores, aut explicabo eaque eos quos facere necessitatibus sit est ipsam quae optio! Lorem ipsum dolor sit amet consectetur adipisicing elit. Itaque molestias natus officiis labore omnis corporis cupiditate quisquam nihil? Mollitia aperiam iusto iste minima ratione culpa veniam architecto facilis neque deleniti? Lorem, ipsum dolor sit amet consectetur adipisicing elit. Cumque aut, eum, quia facere inventore perferendis aperiam recusandae nihil at aliquam ipsum excepturi eligendi animi, nam vel unde. Suscipit, tenetur veritatis. Lorem ipsum dolor sit amet consectetur, adipisicing elit. Eaque, quam. Hic unde nam dolor debitis voluptates officia earum, nostrum ut quae recusandae voluptate eveniet commodi ipsa dolorum nulla totam molestiae?</p>
</div>
<div class="button"></div>
</div>
</div>
A good way to achieve this is by wrapping the element with the class fixed-div inside a container, and moving the element with the class button outside of the element with the class fixed-div, so that it is the direct child of the container element. Then you can set position: relative to the container element, and position: absolute to the element with the class button. As the container element will have the exact same size (and especially the same height) as the element with the class fixed-div, the element with the class button will be positioned at its bottom.
You also will need to add a bottom padding to the element with the class fixed-div so that the button doesn't not hide its content, and so that it doesn't affect its defined height, you can set its box-sizing property to border-box.
See the following code:
.content {
display: flex;
width: 100%;
justify-content: space-around;
}
.container {
position: relative;
}
.fixed-div {
box-sizing: border-box;
width: 150px;
height: 400px;
padding-bottom: 30px;
overflow-y: auto;
background-color: red;
}
.button {
position: absolute;
bottom: 0;
width: 100%;
height: 30px;
background-color: grey;
}
<h1>I need grey area fixed on the bottom, non depending of text amount </h1>
<div class="content">
<div class="container">
<div class="fixed-div">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ad dolores aspernatur error rem aliquam omnis eveniet consequuntur asperiores, aut explicabo eaque eos quos facere necessitatibus sit est ipsam quae optio!loremLorem ipsum dolor sit amet consectetur adipisicing elit. Itaque molestias natus officiis labore omnis corporis cupiditate quisquam nihil? Mollitia aperiam iusto iste minima ratione culpa veniam architecto facilis neque deleniti?Lorem, ipsum dolor sit amet consectetur adipisicing elit. Cumque aut, eum, quia facere inventore perferendis aperiam recusandae nihil at aliquam ipsum excepturi eligendi animi, nam vel unde. Suscipit, tenetur veritatis.Lore Lorem ipsum dolor sit amet consectetur, adipisicing elit. Eaque, quam. Hic unde nam dolor debitis voluptates officia earum, nostrum ut quae recusandae voluptate eveniet commodi ipsa dolorum nulla totam molestiae?</p>
</div>
<div class="button"></div>
</div>
<div class="container">
<div class="fixed-div">
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Cumque aut, eum.</p>
</div>
<div class="button"></div>
</div>
<div class="container">
<div class="fixed-div">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ad dolores aspernatur error rem aliquam omnis eveniet consequuntur asperiores, aut explicabo eaque eos quos facere necessitatibus sit est ipsam quae optio!loremLorem ipsum dolor sit amet consectetur adipisicing elit. Itaque molestias natus officiis labore omnis corporis cupiditate quisquam nihil? Mollitia aperiam iusto iste minima ratione culpa veniam architecto facilis neque deleniti?Lorem, ipsum dolor sit amet consectetur adipisicing elit. Cumque aut, eum, quia facere inventore perferendis aperiam recusandae nihil at aliquam ipsum excepturi eligendi animi, nam vel unde. Suscipit, tenetur veritatis.Lore Lorem ipsum dolor sit amet consectetur, adipisicing elit. Eaque, quam. Hic unde nam dolor debitis voluptates officia earum, nostrum ut quae recusandae voluptate eveniet commodi ipsa dolorum nulla totam molestiae?</p>
</div>
<div class="button"></div>
</div>
<div class="container">
<div class="fixed-div">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ad dolores aspernatur error rem aliquam omnis eveniet consequuntur asperiores, aut explicabo eaque eos quos facere necessitatibus sit est ipsam quae optio!loremLorem ipsum dolor sit amet consectetur adipisicing elit. Itaque molestias natus officiis labore omnis corporis cupiditate quisquam nihil? Mollitia aperiam iusto iste minima ratione culpa veniam architecto facilis neque deleniti?Lorem, ipsum dolor sit amet consectetur adipisicing elit. Cumque aut, eum, quia facere inventore perferendis aperiam recusandae nihil at aliquam ipsum excepturi eligendi animi, nam vel unde. Suscipit, tenetur veritatis.Lore Lorem ipsum dolor sit amet consectetur, adipisicing elit. Eaque, quam. Hic unde nam dolor debitis voluptates officia earum, nostrum ut quae recusandae voluptate eveniet commodi ipsa dolorum nulla totam molestiae? </p>
</div>
<div class="button"></div>
</div>
</div>
I reduced the width of the text elements so that it works within a narrow container, you can run the code in full page if it is still too narrow.
Leaving the button inside the scrolling element and setting position: relative to the latter and position: absolute to the former wouldn't work as the bottom of the button would be positioned inside the scrolling element at 400px which is the height of the scrolling element, and would scroll with its content.
Hope that helps.
You can try code:
.fixed-div p{
display: block;
margin: 0;
padding: 10px;
height: 350px;
}
This is a demo: https://codepen.io/phuongnm153/pen/zQaobq
use position: relative; for the parent (fixed-div) and position:absolute;bottom:0; for the child (button)
.content{
display:flex;
width: 100%;
justify-content: space-around;
}
.fixed-div{
position: relative;
background-color: green;
height: 400px;
width: 300px;
overflow-y: auto;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.button{
position:relative;
background-color: grey;
width: 100%;
height: 30px;
display: inline-table;
}

Resources