How to keep sidebar and header fixed with overflow on content - html

How to only make #content scrollable but keep #header and #sidebar fixed?
And is flexbox the best way to do it?
https://jsfiddle.net/3pnj1k5b/
html,
body {
margin: 0;
}
#page-app {
display: flex;
height: 100%;
background: red;
}
#sidebar {
width: 200px;
background: blue;
}
#header {
height: 60px;
background: pink;
}
#body {
flex: 1;
background: green;
}
#content {
overflow: auto;
}
<div id="page-app">
<section id="sidebar">
sidebar
</section>
<div id="body">
<header id="header">
header
</header>
<section id="content">
content<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b
</section>
</div>
</div>

It's position:fixed that fixes element to their position. It was basically what I've added.
html,
body {
margin: 0;
}
#page-app {
display: flex;
height: 100%;
background: red;
}
#sidebar {
position: fixed;
height: 100%;
width: 200px;
background: blue;
z-index: 2
}
#header {
height: 60px;
background: pink;
position: fixed;
padding-left: 200px;
width: 100%;
z-index: 1;
}
#body {
flex: 1;
background: green;
}
#content {
overflow: auto;
margin-left: 200px;
margin-top: 60px;
}
<div id="page-app">
<section id="sidebar">
sidebar
</section>
<div id="body">
<header id="header">
header
</header>
<section id="content">
content<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b content
<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b
</section>
</div>
</div>

Make the html,body height: 100%;. Then add a max-height to #page-app. Then make #sidebar and #header position: sticky;. Finally, just set the height of #content to be 100% minus the height of the header. In your case I used a calc to set that height. See below:
html,
body {
margin: 0;
height: 100%;
}
#page-app {
display: flex;
max-height: 100%;
background: red;
}
#sidebar {
width: 200px;
background: blue;
position: sticky;
}
#header {
height: 60px;
background: pink;
position: sticky;
}
#body {
flex: 1;
background: green;
}
#content {
overflow: auto;
height: calc(100% - 60px);
}
<div id="page-app">
<section id="sidebar">
sidebar
</section>
<div id="body">
<header id="header">
header
</header>
<section id="content"> content<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b
</section>
</div>
</div>

Make the #header position: sticky and the #body scrollable with overflow-y: scroll;
Codepen here
html,body{
margin:0;
height: 100%;
position: relative;
}
#page-app{
display:flex;
height:100%;
background:red;
overflow-y: hidden;
}
#sidebar{
width:200px;
height: 100%;
background:blue;
}
#header{
height:60px;
background:pink;
position: sticky;
top: 0;
}
#body{
flex:1;
background:green;
overflow-y: scroll;
}
<div id="page-app">
<section id="sidebar">
sidebar
</section>
<div id="body">
<header id="header">
header
</header>
<section id="content">
content<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b<br>b
</section>
</div>
</div>

Related

Dashboard grid alternative

I am wondering, if there are any alternative/better ways to create this dashboard layout with flex or maybe grid? So I wouldn't need to add this pusher with 200px margin.
I heard about that it can be done using flex 1 1 0% or something like that, I am not sure how to implement it.
body {
margin: 0;
}
.content {
display: flex;
}
.sidebar {
position: fixed;
left: 0;
width: 200px;
background: red;
height: 100vh;
overflow-y: auto;
}
.body {
background: blue;
flex: 1;
height: 100vh;
}
.pusher {
margin-right: 200px;
}
.nav{
background: yellow;
height: 60px;
}
<div class="content">
<div class="sidebar"></div>
<div class="pusher">
</div>
<div class="body">
<div class="nav">
Nav
</div>
test
</div>
</div>
Here you go...
I removed the div with class="pusher" and changed/added the CSS as follows:
.sidebar {
width: 20vw;
}
.body {
position: absolute;
width: 80vw;
right: 0;
}
Basically, I made the div class="sidebar" and the div with class="body" make up to 100 % of the screen but in different relative units, i.e. vw (20 vw + 80 vw = 100 vw). So, now I just needed to add right: 0; to the div with class="body" in order to achieve the exact same result as you did with margin-right: 200px;. I also added position: absolute; to the div with class="body", otherwise it won't work.
See the snippet below.
body {
margin: 0;
}
.content {
display: flex;
}
.sidebar {
position: fixed;
left: 0;
width: 20vw;
background: red;
height: 100vh;
overflow-y: auto;
}
.body {
position: absolute;
background: blue;
height: 100vh;
width: 80vw;
right: 0;
}
.nav {
background: yellow;
height: 60px;
}
<div class="content">
<div class="sidebar"></div>
<div class="body">
<div class="nav">Nav</div>
<div>test</div>
</div>
</div>
Hi I change your HTML and CSS code and I do my best for you.
HTML CODE:
<div class="main">
<div class="sidebar">This is Sidebar</div>
<div class="content">
<div class="nav">
Nav
</div>
<div class="content-body">
test
</div>
</div>
</div>
CSS Code:
body {
margin: 0;
}
.main{
display: flex;
width: 100vw;
}
.sidebar {
left: 0;
width: 200px;
background: red;
height: 100vh;
}
.content {
display: flex;
flex-direction: column;
width: 100vw;
background: #ddd;
height: 100vh;
}
.nav{
background: yellow;
height: 60px;
}
.content-body {
background: blue;
height: 100vh;
}

Two scrollable content bars with fixed header and footer

I have this code with the reqirement to add another side menu to my existing page
https://jsfiddle.net/84j7wcqa/
<div class="wrapper">
<div class="header">
<div class="inner">header</div>
</div>
<div class="top">
<div class="inner">top</div>
</div>
<div class="content">
<div class="inner">
<div class="right">
<div style="height:1000px;">right</div>
</div>
</div>
</div>
<div class="footer">
<div class="inner">footer</div>
</div>
html, body {
height: 100%;
margin: 0;
}
.wrapper {
height: 100%;
width: 100%;
display: table;
}
.header, .content, .footer, .top {
display: table-row;
}
.header, .footer {
background: silver;
}
.inner {
display: table-cell;
}
.content .inner {
height: 100%;
position: relative;
background: pink;
}
.right {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
overflow: auto;
}
.top {
background-color: gold;
}
which looks this way:
But I want a additional content which is scrollable to look it like this
Question: Can this be solved with the table / table-row / table-cell approach?
Check my answer in this link. Hope this helps.
https://jsfiddle.net/m2vpcs1u/3/
HTML:
<div class="wrapper">
<div class="header">
<div class="inner">header</div>
</div>
<div class="top">
<div class="inner">top</div>
</div>
<div class="content">
<div class="inner">
<div class="right">
<div style="height:1000px;">right</div>
</div>
<div class="left">
<div style="height:1000px;">left</div>
</div>
</div>
</div>
<div class="footer">
<div class="inner">footer</div>
</div>
</div>
CSS:
html, body {
height: 100%;
margin: 0;
}
.wrapper {
height: 100%;
width: 100%;
display: table;
}
.header, .content, .footer, .top {
display: table-row;
}
.header, .footer {
background: silver;
}
.inner {
display: table-cell;
}
.content .inner {
height: 100%;
position: relative;
background: pink;
}
.right {
position: absolute;
left: 50%;
right: 0;
top: 0;
bottom: 0;
overflow: auto;
width:50%;
}
.left {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
overflow: auto;
width:50%;
}
.top {
background-color: gold;
}
You can use this code
html, body {
height: 100%;
margin: 0;
}
.wrapper {
height: 100%;
width: 100%;
display: table;
}
.header, .content, .footer, .top {
display: table-row;
}
.header, .footer {
background: silver;
}
.inner {
display: inline-block;
float: left;
width: 100%;
}
.inner .left {
display: inline-block;
width: 50%;
float: left;
overflow: auto;
height: 900px;
}
.inner .right {
display: inline-block;
width: 50%;
float: left;
overflow: auto;
height: 900px;
}
.content .inner {
height: 100%;
position: relative;
background: pink;
}
.top {
background-color: gold;
}
<div class="wrapper">
<div class="header">
<div class="inner">header</div>
</div>
<div class="top">
<div class="inner">top</div>
</div>
<div class="content">
<div class="inner">
<div class="left">
<div style="height:1000px;">left</div>
</div>
<div class="right">
<div style="height:1000px;">right</div>
</div>
</div>
</div>
<div class="footer">
<div class="inner">footer</div>
</div>
</div>

absolute positioning and dynamic height

I want to create a page like this:
and here is my HTML:
<div class="container">
<div class="article">
<div class="main-content">
Main content goes here...
</div>
<div class="content-meta">
<div class="content-title">
the title of content goes here...
</div>
<div class="content-info">
some information about content....
</div>
</div>
</div>
</div>
and CSS:
.container {
overflow:hidden;
position:relative;
width: 100%;
height: 350px;
}
.container .article {
width:100%;
position:absolute;
left:0;
top:0;
background-color: red;
}
.container .article .main-content {
width:50%;
float: right;
}
.container .article .content-meta {
width:50%;
float: right;
position: relative;
height: 350px;
}
.container .content-title , .container .content-info {
width: 100%;
position: absolute;
left: 0;
height: 50%;
}
.container .content-title {
background-color: green;
top: 0;
}
.container .content-info {
background-color: blue;
top: 50%;
}
it's working but when I use % instead of px for height of green and blue area, it just doesn't work. Why?
I mean, I set for both green and blue area height:50% but it didn't work. How can I solve this problem?
Note: I have 6 div.article elements and I want all of them to be stacked on top of each other and that's why I'm using position property.
In order to have percentage height to work you need to set both the parent elements .container .article .content-meta and .container .article to height:100%.
.container {
overflow: hidden;
position: relative;
width: 100%;
height: 350px;
}
.container .article {
width: 100%;
position: absolute;
left: 0;
top: 0;
background-color: red;
height: 100%;
}
.container .article .main-content {
width: 50%;
float: right;
}
.container .article .content-meta {
width: 50%;
float: right;
position: relative;
height: 100%;
}
.container .content-title,
.container .content-info {
width: 100%;
position: absolute;
left: 0;
height: 50%;
}
.container .content-title {
background-color: green;
top: 0;
}
.container .content-info {
background-color: blue;
top: 50%;
}
<div class="container">
<div class="article">
<div class="main-content">
Main content goes here...
</div>
<div class="content-meta">
<div class="content-title">
the title of content goes here...
</div>
<div class="content-info">
some information about content....
</div>
</div>
</div>
</div>
In fact, when you use absolute position, float won't be necessary.
.article {
position: relative;
height: 350px;
}
.main-content {
position: absolute;
left: 50%;
top: 0;
width: 50%;
height: 100%;
background: red;
}
.content-meta {
position: absolute;
left: 0;
top: 0;
width: 50%;
height: 100%;
}
.content-title,
.content-info {
position: absolute;
left: 0;
width: 100%;
height: 50%;
}
.content-title {
background: green;
top: 0;
}
.content-info {
background: blue;
top: 50%;
}
<div class="article">
<div class="main-content">
Main content goes here...
</div>
<div class="content-meta">
<div class="content-title">
the title of content goes here...
</div>
<div class="content-info">
some information about content....
</div>
</div>
</div>
Or, just use float without absolute position.
.article {
height: 350px;
}
.main-content {
float: right;
width: 50%;
height: 100%;
background: red;
}
.content-meta {
float: left;
width: 50%;
height: 100%;
}
.content-title,
.content-info {
float: left;
width: 100%;
height: 50%;
}
.content-title {
background: green;
}
.content-info {
background: blue;
}
<div class="article">
<div class="main-content">
Main content goes here...
</div>
<div class="content-meta">
<div class="content-title">
the title of content goes here...
</div>
<div class="content-info">
some information about content....
</div>
</div>
</div>
Alternatively, you can use flexbox if you don't need to support old browsers.
.article {
height: 350px;
display: flex;
flex-direction: row-reverse;
}
.main-content {
background: red;
flex: 1;
}
.content-meta {
flex: 1;
display: flex;
flex-direction: column;
}
.content-title,
.content-info {
flex: 1;
}
.content-title {
background: green;
}
.content-info {
background: blue;
}
<div class="article">
<div class="main-content">
Main content goes here...
</div>
<div class="content-meta">
<div class="content-title">
the title of content goes here...
</div>
<div class="content-info">
some information about content....
</div>
</div>
</div>

Fixed width absolute sidebar with fluid content

I'm trying to create a fluid layout site which also has a left sidebar with the following properties:
It doesn't scroll when the main content does
It takes up the full height of the page
The only layout I have seen is this one: http://stugreenham.com/demos/fluid-width-with-a-fixed-sidebar/
HTML
<div id="page">
<header id="sidebar">
//SIDEBAR CONTENT HERE
</header>
<article id="contentWrapper">
<section id="content">
//CONTENT HERE
</section>
</article>
</div>
CSS
html {
overflow: hidden;
}
#sidebar {
background: #eee;
float: left;
left: 300px;
margin-left: -300px;
position: relative;
width: 300px;
overflow-y: auto;
}
#contentWrapper {
float: left;
width: 100%;
}
#content {
background: #ccc;
margin-left: 300px;
overflow-y: auto;
}
However I think this is a poor solution because not only does it require negative margins but it also requires javascript.
Is there a better way to achieve this?
Demo
css
#sidebar {
position:fixed;
height:100%;
width: 300px;
background-color: #ccc;
overflow-y: auto;
}
#contentWrapper { /* not using margin */
box-sizing:border-box;
background-color:#eee;
position:absolute;
left:300px;
width:calc(100% - 300px);
min-height: 100%;
}
#contentWrapper { /* using margin */
box-sizing:border-box;
background-color:#eee;
margin-left: 300px;
/*position:absolute;
left:300px;
width:calc(100% - 300px);*/
min-height: 100%;
}
html,body,#page {
width: 100%;
height: 100%;
}
You can do something like this: http://jsfiddle.net/9U2U4/
Demo with lot of Text: http://jsfiddle.net/9U2U4/1/
CSS:
html, body {
height: 100%;
}
body {
margin:0;
padding:0;
}
#page {
height: 100%;
}
#sidebar {
position: fixed;
left:0;
top:0;
bottom:0;
width: 30%;
background-color: #eee;
}
#contentWrapper {
margin-left: 30%;
min-height: 100%;
position: relative;
background: #ccc;
}
#content
{
padding: 10px;
}
HTML:
<div id="page">
<header id="sidebar">// Sidebar</header>
<article id="contentWrapper">
<section id="content">
<p>Text</p>
</section>
</article>
</div>

Dynamic expand leftbar to footer

Folks, please help me expand leftbar to footer, it should be dynamically expanded to footer if content is expanded by inner DIV(s), please see below the code and demo:
HTML
<div class="wrapper">
<header class="header"> </header>
<div class="middle">
<div class="container">
<main class="content">
<div id="child">
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
<p>1</p>
</div>
</main>
</div>
<aside class="left-sidebar">Left bar</aside>
</div>
<footer class="footer"></footer>
</div>
CSS
.wrapper {
width: 500px;
margin: 0 auto;
min-height: 100%;
}
.header {
height: 100px;
background: blue;
}
.footer {
height: 60px;
width: 100%;
bottom: 0;
position: relative;
background:yellow;
clear:left;
}
.middle {
width: 100%;
min-height: 300px;
position: relative;
}
.container {
min-height: 300px;
width: 100%;
float: left;
}
.content {
width: 800;
min-height: 300px;
left: 280;
position: relative;
background:red;
padding-bottom:70px;
}
#child {
position:relative;
margin-top:100px;
left:160px;
min-height:500px;
width: 200px;
border: solid 1px white;
background:green;
}
.left-sidebar {
float: left;
width: 100px;
min-height: 500px;
height: 100%;
margin-left: -100%;
position: relative;
background: black;
}
DEMO: http://jsfiddle.net/ac6s7/23/
Your structure can be like this:
<div class="wrapper">
<header class="header"></header>
<div class="middle">
<div class="container">
<aside class="left-sidebar">Left bar</aside>
<main class="content">
<div id="child"></div>
</main>
</div>
</div>
<footer class="footer"></footer>
</div>
CSS:
.wrapper {
width: 500px;
margin: 0 auto;
min-height: 100%;
}
.header {
height: 100px;
background: blue;
}
.footer {
height: 60px;
width: 100%;
bottom: 0;
position: relative;
background:yellow;
clear:left;
}
.middle {
width: 100%;
min-height: 300px;
position: relative;
}
.container {
min-height: 300px;
width: 100%;
}
.content {
width: 800px;
min-height: 300px;
left: 280;
position: relative;
background:red;
padding:10px;
display:table-cell
}
#child {
position:relative;
margin-top:100px;
left:160px;
min-height:500px;
border: solid 1px white;
background:green;
width:200px;
}
.left-sidebar {
display:table-cell;
width: 100px;
min-height: 500px;
height:100%;
margin-left: -100%;
position: relative;
background: black;
}
Working Demo
Hope this helps you.
I believe that this is what you are looking for.
Fiddle Demo
I'm not sure what I've changed but this are the styles that I think that might have changed.
CSS
.container {
min-height: 300px;
width: 100%;
float: left;
height:100%;
}
.content {
width: 800;
min-height: 300px;
left: 280;
position: relative;
background:red;
padding:10px;
}
.left-sidebar {
left:0;
width: 100px;
min-height: 500px;
position: absolute;
background: black;
height:100%;
}