I am using a flexbox to vertically align a div, with variable height, in its container (I'm open to other options). But I struggle to get a reliable scroll behavior when the container is smaller than the content.
html,
body,
.app {
height: 100%;
}
.app {
background-color: blue;
color: white;
}
.app header {
height: 80px;
background-color: red;
}
.app .container {
display: flex;
flex-direction: column;
background-color: black;
height: calc(100% - 80px);
align-items: center;
justify-content: center;
overflow: scroll;
}
.app .container .content {
width: 300px;
height: 200px;
background-color: yellow;
color: black;
}
<link rel="stylesheet prefetch" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
<div class="app">
<header>Header</header>
<div class="container">
<div class="content">
Variable content height
</div>
</div>
</div>
Thanks a lot!
Keep overflow:auto
and no need to use column as direction, simply keep it row. Then rely on margin:auto
to keep the element centred:
Example with no overflow and centred:
html, body, .app {
height: 100%;
}
body {
margin:0;
}
.app {
background-color: blue;
color: white;
}
.app header {
height: 80px;
background-color: red;
}
.app .container {
display: flex;
flex-direction: row;
background-color: black;
height: calc(100% - 80px);
justify-content: center;
overflow: auto;
}
.app .container .content {
width: 300px;
height:100px;
background-color: yellow;
color: black;
margin:auto;
}
<div class="app">
<header>Header</header>
<div class="container">
<div class="content">
Variable content height
</div>
</div>
</div>
Example with overflow and scroll
html, body, .app {
height: 100%;
}
body {
margin:0;
}
.app {
background-color: blue;
color: white;
}
.app header {
height: 80px;
background-color: red;
}
.app .container {
display: flex;
flex-direction: row;
background-color: black;
height: calc(100% - 80px);
justify-content: center;
overflow: auto;
}
.app .container .content {
width: 300px;
height:900px;
background-color: yellow;
color: black;
margin:auto;
}
<div class="app">
<header>Header</header>
<div class="container">
<div class="content">
Variable content height
</div>
</div>
</div>