Stacking columns horizontally on wider screens and vertically on smaller screens - html

I'm trying to accomplish the following. I've dumbed it down for purposes of demonstration:
A page with a header, a footer, and 2 columns
The header is always on top (i.e., vertically above the rest of the content) with height 20px
The footer is always at the bottom (i.e., vertically below the rest of the content) with height 20px.
In between, there are 2 columns
If the viewport is wide enough, the 2 columns are presented side by side, and everything fits inside the viewport. Column A takes 75%, Column B 25%. Each is (100% - 40px) tall
If the viewport is not wide enough, the 2 columns are presented on top of each other, each 100% wide. Column A is still (100%-40px) tall, column B has a height to fit its content. The page is now more than 1 viewport tall (with the header visible when scrolling all the way up, and the footer visible when scrolling all the way down)
If content does not fit a column, a scroll bar should appear inside the overflowing column
I've accomplished all points except number 6 here
I cannot got number 6 to work. I've tried min-width. I've also tried to 'port' the whole thing to bootstrap (I'm using bootstrap elements on the page already) but that doesn't work nicely with some angular components (that need to poll their parent's size; for some reason the element then keeps growing)
I'd be grateful for good ideas!
edit
I've tried using flexboxes. It's closer, but the viewport doesn't scroll when it gets too narrow... ideas? https://jsfiddle.net/498xpp6n/2/
edit
I've thought about it some more, and made a little change to the wanted column heights when going into stacked mode. Hope that doesn't ruin someone's day

The layout is possible with flexbox. This is all you need:
HTML
<div id="outer-flex-container"><!-- primary flex container -->
<div id="header">The Header The Header The Header ... </div><!-- flex item #1 -->
<div id="inner-flex-container"><!-- flex item #2 -->
<div id="mainpanel">Mainpanel Mainpanel Mainpanel Mainpanel Mainpanel ... </div>
<div id="aside">settings settings settings settings settings settings ... </div>
</div><!-- end #inner-flex-container -->
<div id="footer">The Footer The Footer The Footer ... </div><!-- flex item #3 -->
</div><!-- end #outer-flex-container -->
CSS
html { height: 100%; }
body { height: 100%; margin: 0; }
#outer-flex-container {
display: flex;
flex-direction: column;
height: 100%;
}
#inner-flex-container {
display: flex;
height: calc(100% - 40px);
}
#header { height: 20px; }
#footer { height: 20px; }
#mainpanel { flex: 0 0 75%; }
#aside { flex: 1; overflow-y: scroll; }
#media screen and ( max-width: 500px) { #inner-flex-container { flex-direction: column; } }
I believe the code above covers all seven points in your question :-)
DEMO
UPDATE (based on comments)
HTML
<div id="outer-flex-container"><!-- primary flex container -->
<div id="header">The Header The Header The Header ... </div><!-- flex item #1 -->
<div id="inner-flex-container"><!-- flex item #2 -->
<div id="mainpanel">Mainpanel Mainpanel Mainpanel Mainpanel Mainpanel ... </div>
<div id="aside">settings settings settings settings settings settings ... </div>
<div id="footer">The Footer The Footer The Footer ... </div>
</div><!-- end #inner-flex-container -->
</div><!-- end #outer-flex-container -->
Notes:
moved footer into .inner-flex-container
now only two primary flex items
three inner flex items
CSS
body { margin: 0; }
#outer-flex-container {
display: flex;
flex-direction: column;
}
#inner-flex-container {
display: flex;
flex-wrap: wrap;
}
#header { height: 20px; }
#mainpanel {
flex: 0 0 75%;
height: calc(100vh - 40px);
min-height: calc(100vh - 40px);
overflow-y: auto;
}
#aside {
flex: 0 0 25%;
height: calc(100vh - 40px);
overflow-y: auto;
}
#footer {
flex-basis: 100%;
height: 20px;
}
#media screen and ( max-width: 500px) {
#inner-flex-container { flex-direction: column; }
#mainpanel { height: 100vh; }
#aside { height: auto; }
}
Notes:
removed fixed heights (was limiting footer positioning on smaller screens)
added footer to inner flex container so it's always below content columns
Revised Demo

Related

How can I get a scroll bar in the right-bottom div

I'm trying to lay out a web page that has three reasons - left, top right and bottom right. The left and bottom right regions should have scrollbars in them, and the entire page should fill the screen. I'm using Bootstrap 4.
I can get the scrollbars working properly around the left region. The problem is with the right regions - the horizontal scrollbar appears on the bottom-right region, as it should, but the vertical scrollbar appears on the entire page. Note that the bottom-right also has a vertical scroll bar, but it's disabled.
#outer {
height: 100vh;
overflow: none;
}
#left-col {
height: 100vh;
overflow: scroll;
background-color: #ff85d4;
}
#left-large {
height: 5000px;
width: 5000px;
}
#right-col {
height: 100vh;
}
#right-top {
background-color: #abff64;
}
#right-bottom {
overflow: scroll;
background-color: #ccddff;
}
#right-bottom-inner {
width: 2000px;
height: 2000px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row" id="outer">
<div class="col-9" id="left-col">
<div id="left-large">
Large left
</div>
</div>
<div class="col-3" id="right-col">
<div id="right-top">
<p>
Top 1
</p>
<p>
Top 2
</p>
<p>
Top 3
</p>
</div>
<div id="right-bottom">
<div id="right-bottom-inner">
Right bottom inner
</div>
</div>
</div>
</div>
</div>
How can I make the bottom-right region have its own scrollbars?
Like I said in the comments this is another way to do this with CSS grid which seems like the perfect tool for something this ... "gridy" :)
I gave all the boxes plenty of space you can scroll... adjust at will.
Update: I made the right-top box max-content and grow for as long as the content is while giving the right-bottom box a min height of 20px.
body {
padding: 0;
margin: 0;
}
.wrapper {
display: grid;
grid-template-columns: 1.6fr 0.4fr;
grid-template-rows: max-content minmax(20px, 1fr);
gap: 1rem;
height: 100vh;
width: 100vw;
}
.wrapper-inner {
height: 2000px;
width: 2000px;
}
.left,
.right-top,
.right-bottom {
overflow: auto;
padding: 1rem;
}
.left {
grid-row-start: 1;
grid-row-end: 3;
background: hotpink;
}
.right-top {
background: lime;
}
.right-bottom {
background: skyblue;
}
<div class="wrapper">
<div class="left">
<div class="wrapper-inner">
Large left
</div>
</div>
<div class="right-top">
<p>
Top 1
</p>
<p>
Top 2
</p>
<p>
Top 3
</p>
</div>
<div class="right-bottom">
<div class="wrapper-inner">
Right bottom inner
</div>
</div>
</div>
If you want to force a scrollbar on any block element, you'll need to set a fixed height and include an overflow-x:scroll or overflow-y:scroll property depending on where you want the scrollbar to appear.
If you want a horizontal scrollbar, use overflow-x:scroll;, if you want a vertical scrollbar, use overflow-y:scroll;
flex is magic
CSS
/*Allow children to auto fixed height*/
#right-col {
display: flex;
flex-direction: column;
}
#right-top {
height: fit-content; /*Only use essential*/
}
#right-bottom {
overflow: scroll; /*See child content*/
height:100%; /*use all remain height*/
}
Seeing that the height needs to be explicit (either as percentage or a fixed number), I couldn't find a CSS-only solution.
Instead I used a ResizeObserver to track changes to the size of the right-col and right-top elements (in my actual problem, the right-top element changes in size), calculated the right-bottom element (right-col.height - right-top.height basically), and applied it as a dynamic style on the right-bottom element.
Not pretty but it's working.
I tried using a CSS grid, too (instead of bootstrap altogether), but without explicitly specifying the height of the right bottom element, the scrollbars misbehaved there, as well.

how to have to sections that resize based on content and another that uses the remaining space

I'm trying to create a webapp screen where the header is stuck to the top and the footer stuck to the bottom all the time, and the main content is displayed inbetween with a scroll bar if necessary.
I want to make this possible in multiple devices, so the header and footer can change in height to fit the their content in smaller screens and the main content should use the remaining space.
Is is possible to create this behaviour with css? (maybe using flexbox?)
You are right - flexbox is perfect for this:
html,
body {
margin: 0;
padding: 0;
}
.container {
display: flex;
flex-direction: column;
height: 100vh;
}
header {
background-color: #0800ff;
}
.content {
background-color: #fff;
flex-grow: 1;
overflow-y: auto;
}
footer {
background-color: #fec11a;
}
<section class="container">
<header>...</header>
<div class="content">
<ol>
<li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li>
<li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li>
<li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li>
<li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li>
<li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li><li></li>
</ol>
</div>
<footer>...</footer>
</section>
Notice how I use height: 100vh for the .container and flex-grow: 1 for the .content. That does the trick.

CSS Flexbox or CSS Grid for Header, Sticky Footer and Vertically Aligned Middle Content all together?

I have a requirement on which we have Header, Middle Section and Footer.
Header and Footer height can be more or less depending on the various screen sizes and content within them.
Footer will be sticky, at the end of the screen if the page doesn't contain scroll and should not overlap over the middle content if scrolling occurs for small screens. Typical sticker footer behavior.
Middle section contains two columns. And Each column content should be vertically center aligned.
Please refer below screenshot for reference what
Note: I can use normal sticky footer and CALC to adjust the height of the main content, but it will not be dynamic. I don't want to use javascript to do all mathematics on DOMContentLoaded and window resize.
My approach uses a bunch of flexboxes and keeps things simple.
.container is a columnar flexbox
main takes up the most available space
header, footer take only the space they need (dynamic)
main is also a flexbox, but in the row direction to house the left and right panels
the panels, too, are flexbox containers, centering their content horizontally and vertically
You might want to view the demo in 'Full page' mode, or in jsFiddle
html,
body {
margin: 0;
}
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex-grow: 1;
display: flex;
background-color: #eee;
}
.panel1,
.panel2 {
background-color: brown;
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
}
.panel-content {
background-color: #ccc;
padding: 3em;
}
header,
footer {
background-color: #ccc;
padding: 1em;
text-align: center;
}
<div class="container">
<header>Header</header>
<main>
<div class="panel1">
<div class="panel-content">
Content
</div>
</div>
<div class="panel2">
<div class="panel-content">
Content
</div>
</div>
</main>
<footer>Sticky Footer</footer>
</div>
jsFiddle

Make elements fill the remaining space

I have a simple HTML document. I have a header, a section and a div (that contains an unknown number of other divs).
The header and the section do not (and can not) have set heights. Their height comes from the content. Only their width is known (set to 100%).
Is it possible, with flexbox or other means, to get each of those child divs, in this case with class="fill" to be the height of the body - minus the header and section?
In other words, when someone goes to the page, I want them to see the header and the section and then have the first div.fill reach all the way to the bottom, forcing them to scroll to see the next div (but not scroll to see the bottom of the first child div).
I am using a templating system so unfortunately the structure of the HTML can not change and I would like to do this only in CSS.
<html>
<body>
<header> Header content, might contain an image</header>
<section> This is the sub header, unknown height </section>
<div class="container">
<div class="fill">I Want</div>
<div class="fill">Each of These</div>
<div class="fill">To be </div>
<div class="fill">The height of the body - the Header - the Section</div>
</div>
</body>
</html>
body {
display: flex;
flex-direction: column;
height: 100vh;
margin: 0;
}
.container {
flex: 1; /* 1 */
display: flex;
flex-direction: column;
overflow-y: scroll;
}
.fill { flex: 0 0 100%; } /* 2 */
header { background-color: aqua; }
section { background-color: orange; }
.fill:nth-child(odd) { background-color: yellow; }
.fill:nth-child(even) { background-color: lightgreen; }
<body>
<header> Header content, might contain an image</header>
<section> This is the sub header, unknown height </section>
<div class="container">
<div class="fill">I Want</div>
<div class="fill">Each of These</div>
<div class="fill">To be </div>
<div class="fill">The height of the body - the Header - the Section</div>
</div>
</body>
jsFiddle
Notes:
The flex-grow: 1 component of flex: 1 tells the .container element (a flex item child of body) to consume all remaining space. This will cause .container to use up any space not consumed by header and section.
The flex-basis: 100% component of flex: 0 0 100% tells the .fill items (flex item children of .container) to consume 100% height of the parent. So these items will always take the full height of flex-grow: 1 on the parent.
Because flex items are set, by default, to shrink in order to not overflow the container, an override is set with flex-shrink: 0 in the flex: 0 0 100% rule. This disables the shrinking feature and allows the items to stay fixed at 100% height. (Otherwise, regardless of the defined height / flex-basis, the items would shrink evenly to prevent an overflow. See demo.)
If you change the structure of the elements a bit you can get it with only css.
Basically add the first .fill element in a container with the header and the section (let's call it first). For the other divs use height: 100vh
body {
margin: 0;
}
.container {
display: flex;
flex-direction: column;
height: 100vh;
}
.fill {
height: 100vh;
overflow: auto;
}
.first {
flex: 1;
}
header { background-color: aqua; }
section { background-color: orange; }
.first, .fill:nth-child(odd) { background-color: yellow; }
.fill:nth-child(even) { background-color: lightgreen; }
<html>
<body>
<div class="container">
<header> Header content, might contain an image</header>
<section> This is the sub header, unknown height </section>
<div class="first">I Want</div>
</div>
<div class="fill">Each of These</div>
<div class="fill">To be </div>
<div class="fill">The height of the body - the Header - the Section</div>
</body>
</html>

"Holy Grail" three column layout using flexbox

I'm trying to achieve the three-column layout generally described as the "holy grail" (see this ALA article) using the new display: flex syntax.
The requirements are as follows:
A header and footer, with between them three columns
The outer columns have fixed widths
The inner column stretches to fill the space between the side columns, with a minimum and maximum width beyond which it will not stretch (so neither should the container)
The footer should be at the bottom of the viewport, until the content actually pushes it below
I got the first three requirements down with the following code:
<body>
<div class="container">
<header class="masthead">
<h1>The Header</h1>
</header>
<div class="side-left column">
Left sidebar
</div>
<div class="middle column">
Content goes here
</div>
<div class="side-right column">
Right sidebar
</div>
<footer class="footer">
© Footer
</footer>
</div>
</body>
CSS:
.container {
display: flex;
flex-flow: row wrap;
min-width: 500px;
max-width: 1100px;
}
.masthead {
flex: 1 100%;
}
.side-left,
.side-right {
flex: 0 0 150px;
}
.middle {
flex: 1;
}
.footer {
flex: 1 100%;
}
Live in action: jsBin
However, I'm stuck with the 100% height. I already tried setting either some of the columns or the container to height: 100% or min-height: 100% but none seem to work. Do I need one of the many other flex properties to handle this? I can't seem to see the forest through the trees.
.container { min-height: 100vh; }