This is perhaps a common question, but all the answers I have found around the web, didn't work properly.
I want to create a sidebar for my webpage, which fills the entire height of the webpage.
Then when you scroll the sidebars content should move along the rest of the sites content.
I tried this methods:
HTML:
<div id="wrapper">
<div id="sidebar"></div>
<div id="content-area"></div>
</div>
CSS:
body {
margin: 0;
}
#wrapper {
width: 100%;
}
#sidebar {
width: 280px;
position: absolute;
top: 0;
bottom: 0;
}
#content-area {
width: 900px;
margin-left: 280px;
}
..and:
HTML:
<div id="wrapper">
<div id="sidebar"></div>
<div id="content-area"></div>
</div>
CSS:
body {
height: auto;
min-height: 100%;
margin: 0;
}
#wrapper {
width: 100%;
height: 100%;
}
#sidebar {
width: 280px;
height: 100%;
float: left;
}
#content-area {
width: 900px;
height: 100%;
float: left;
}
The first one works fine, until the content is extended: http://jsfiddle.net/B3bCb/1/
If that happens then the sidebar stops according to the browser-window height.
The second one didn't worked at all: http://jsfiddle.net/B3bCb/2/
I have also tried the faux column method, but I need to have an CSS-shadow (which blur, spread and color, can be changed dynamically on the site) on my sidebar, which I cannot do properly in the faux column method (Faux column is just an image).
So how do I make my sidebar 100% in height, no matter how much content I have?
Making the sidebar position as "fixed" it stays, doesn't matter how much content you have. I don't know if I solve your problem but hope it helps ;)
Here's the code:
#sidebar {
width: 280px;
position: fixed;
top: 0;
bottom: 0;
background: blue; }
Here's the fiddle
The following css can be another alternative for this
#sidebar {
width: 280px;
position: fixed;
top: 0;
height:100%;
background: blue;
}
#content-area {
position:relative;
left: 280px; // width of your side box.
}
You have a couple of choices, but the gist is to do with CSS position.
The reason position: absolute; does not work is because it needs some tweaking for that to work. You need to disable scrolling on the html, body, and wrapper classes, and enable scrolling on the content-area.
html, body, wrapper {
overflow: hidden;
}
.content-area {
overflow: auto;
}
You can see an example of this here. It's a responsive sidebar layout I made.
The other option is to use a position: fixed; on the sidebar, as others have noted.
Check this fiddle http://jsfiddle.net/dJ654/2/
I have changed some styles
#sidebar {
width: 10%;
height: 100%;
position:fixed;
right:0px;
top:0px;
background-color:gray;
}
#content-area {
width: 90%;
height: 100%;
float: left;
background-color:green;
}
Related
Ok, i thought of starting afresh following some confusions in my previous similiar post. Here, I am trying to know the exact "reason" as to why exactly my outer container div ("container" , pink) is not automatically expanding vertically to fit the content div ("content" , red) (which automatically expands vertically with length of text). I am looking a reason more than the solution, because the reason will help me understand the concept more deeply. Please copy dummy text loremipsum... several times in the "content" div so that it overflows from page
Screenshot
here is the code:
html, body {
width: 100%;
left: 0px;
top: 0px;
margin: 0px;
height: 100%;
}
.container {
width: 600px;
left: 0px;
position: relative;
right: 0px;
background-color: rgba(216,86,112,0.5);
margin: auto;
height: 100%;
}
.content {
height: auto;
width: 200px;
background-color: rgba(255,0,0,1);
position: absolute;
top: 0px;
bottom: auto;
left: 0px;
right: 0px;
margin: auto;
}
</style>
</head>
<body>
<div class="container">
<div class="content">
/* Paste dummy text here more than a page */
</div>
</div>
</body>
</html>
The problem is the following:
Instead of using
.container {
height: 100%;
}
try:
.container {
height: auto;
}
and instead of
.content {
position: absolute;
}
use
.content {
position: relative;
}
Here's why
When an element is set to be 'position: absolute' it wont collapse with any other element, that's why your container doesn't expand at all.
When an element is set to be 'Height:100%' it takes the height of its container, in your case the cointainer is the body which means it will take 100% percent of your screen (in your case), but your content is way higher than the screen and that's why it overflows your content.
Hope you understand....
I'm trying to create 2 divs, one of them is the left sidebar and the other one is the body of the page where content shows up. What I'm trying to do is:
make the sidebar div height 100%
the body height 100% too
make the body's width change when sidebar width changes.
This is the code that I've tried so far:
#Sidebar{
background-color:#F0F0F0;
height: calc(100% - 80px);
width: 257px;
position: fixed;
left: 0;
bottom: 0;
overflow: hidden;
white-space: nowrap;
}
#content {
margin: 0;
position: fixed;
height: 100%;
}
when I do this, the content div shows IN the Sidebar!
#Sidebar {
height: calc(100% - 80px);
width: 257px;
position: fixed;
top:0;
left: 0;
bottom: 0;
overflow: hidden;
white-space: nowrap;
border:1px solid #000;
}
#content {
margin: 0;
position: fixed;
height: 100%;
border:1px solid tomato;
}
<div id="Sidebar">
Hello World!!
</div>
<div id="content">
Content Div
</div>
Note that i use Jquery .Resizable to change the width.
and this is a jsfiddle https://jsfiddle.net/j64r3bm1/
Can't You just put the sidebar
.sidebar {
position: fixed;
height: 100vh;
left: 0px;
top: 0px;
width: 275px;
}
and the body container
body {
padding-left: 275px;
}
it will never overlap and you can just build your body as you wish.
The fun thing about this is when you use media-queries to handle mobile version - you can just remove padding and move sidebar outside of body and on some button (or anything) make it slide into view.
Set the margin-left of your main content to the same width (+gap if you prefer) and change it when you resize your sidebar.
#content {
margin-left: 257px;
Updated fiddle: https://jsfiddle.net/j64r3bm1/2/
To explain what's happening: when you use position:fixed, it no longer takes up space and effectively 'hovers' on the page, so when your content div comes along, it gets put at left:0 because they're nothing to its left that's taking up space on the page.
You could also use position:fixed on your content and set the left - but then neither will take up 100% height (as they will both be 0width/0height).
Alternatively, you could take out the position:fixed and use float:left.
Looks like i figured out how to do it.
and I used JQuery to do that, all what i did is :
#Sidebarlist
{
background-color: var(--SidebarBackgroundColor);
height: calc(100% - 80px);
color:var(--SidebarTextColor);
width: 257px;
position: fixed;
left: 0;
bottom: 0;
overflow:hidden;
white-space: nowrap;
}
#MainContentdiv {
height: 100%;
}
And using jquery :
$('#Sidebarlist').resizable({
start: function( event, ui ) {},
stop: function( event, ui ) {},
handles: 'n,w,s,e',minWidth: 200,
maxWidth: 400
});
$( "#Sidebarlist" ).on( "resizestart", function( event, ui ) {
var marginleft = $("#Sidebarlist").width();
$('#MainContentdiv').css('margin-left',marginleft);
});
$( "#Sidebarlist" ).on( "resizestop", function( event, ui ) {
var marginleft = $("#Sidebarlist").width();
$('#MainContentdiv').css('margin-left',marginleft);
});
Thanks for anybody that answered my question.
You can simply add
left: 257px;
top: 0;
To the #content div and it will appear how you want it. To get this to resize, you'd adjust this. The alternative is to use a container, like this:
#Container {
height: 100%;
position: fixed;
width: 100%;
top: 0;
left: 0;
display: flex;
flex-direction: row;
}
#Sidebar {
height: calc(100% - 80px);
width: 257px;
display: block;
overflow: hidden;
white-space: nowrap;
border:1px solid #000;
}
#content {
margin: 0;
height: 100%;
border:1px solid tomato;
}
<div id="Container">
<div id="Sidebar">
Hello World!!
</div>
<div id="content">
Content Div
</div>
</div>
I'm gonna give you an out there answer, I've tested this in firefox and chrome and it works. But it's a bit cutting edge so I'm not sure if IE will support it.
It's using css variables, if you have a different variable in at the start (in the :root section) then the page will be calculated differently.
I also took your borders out as they mess with total width and used background colours to tell the divs apart.
EDIT:
I've added a tiny amount of JS to show the variable being updated on the fly and everything readjusting.
changeSidebar = function() {
var customSize = document.querySelector('.sidebar-width').value;
document.documentElement.style.setProperty('--menu-width', customSize + 'px');
}
:root {
--menu-width: 257px
}
#sidebar,
#sidebar-footer {
width: var(--menu-width);
left: 0;
overflow: hidden;
position: fixed
}
body {
margin: 0
}
#sidebar {
height: calc(100% - 80px);
top: 0;
white-space: nowrap;
background: orange
}
#sidebar-footer {
height: 80px;
top: calc(100% - 80px);
background: pink
}
#content {
position: fixed;
right: 0;
width: calc(100% - var(--menu-width));
height: 100%;
background: tomato
}
<div id="sidebar">
Hello World!!
</div>
<div id="sidebar-footer">
I'm just here to fill that space...
</div>
<div id="content">
Content Div
<br />
<input class="sidebar-width" value='257'>
<button onclick="changeSidebar();">Enter</button>
</div>
That is the Solution :
var offsetHeight = document.getElementById('Sidebar').offsetHeight;
document.getElementById('content').style.height = offsetHeight+'px';
#Sidebar{
background-color:#F0F0F0;
height: 700px;
width: 10%;
float:left;
left: 0;
bottom: 0;
overflow: hidden;
}
#content {
margin: 0;
width:90%;
float:right;
background-color:green;
}
<div id="Sidebar">
Side bar
</div>
<div id="content">
Hello World !
</div>
I have a page with the following basic layout:
<body>
<div id="header"></div>
<div id="wrapper ">
<div id="sidebar-container"><div id="sidebar"></div></div>
<div id="content"></div>
</div>
<div id="footer"></div>
</body>
The css is like this:
#wrapper {
clear: both;
float: left;
width: 100%;
height: auto;
}
#sidebar-container {
float:left;
width: 10%;
height: 100%;
min-height: 500px;
padding: 20px 0px 20px 0px;
background-color: green;
}
#sidebar {
width:100%;
height: 100%;
background-color: green;
}
#content {
float: left;
width: 90%;
height: 100%;
}
#footer {
clear: both;
position: absolute;
width: 100%;
bottom: 0;
margin: 0 auto;
height: 100px;
background: url("footer.jpg") repeat-x scroll 0 0 rgba(0, 0, 0, 0);
}
At present, the contents of #sidebar-container and #content determine the height of #wrapper. I'd like to get the divs within #wrapper to extent to the footer, which is positioned at the bottom of the browser window. Any suggestions on how to do this? Thank you.
If you support IE9 and up, you can use calc.
#wrapper {
position: relative;
height: calc(100% - {yourFooterHeight}px);
}
#wrapper > * {
height: 100%;
}
Example
body, html {
position: relative;
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#wrapper {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: calc(100% - 100px);
}
#wrapper > * {
height: 100%;
}
#content { background-color: #ebd24b; }
#footer {
position: absolute;
bottom: 0;
left: 0;
width: 100%;
height: 100px;
background-color: #000;
}
<div id="wrapper">
<div id="content"></div>
</div>
<div id="footer"></div>
From what I understand, you may need to use position: fixed for the sidebar and content. A height: 100% doesn't work on elements without a position of fixed or absolute.
You'll have to update some of your other styles, like removing floats, etc., but I believe what you're trying to do requires fixed or absolute positioning. Read more about the difference here: http://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/
Additionally, I would recommend investigating semantic HTML tags like footer and header. This doesn't affect the layout here, but it would be good to get in the habit.
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/HTML5_element_list
It can be a challenge as you content and sidebar have no relationship i.e. both on same level. You would need to have one of them be the parent of the other or you would need to write some JS. You can use pseudo elements in pure CSS, but in essence you would still need to have one of them "lead" as some element needs to be the box size relationship master.
Example, in Angular I use the following directive. You can then use min-height on the right column to keep a min height, after that it will automatically adjust the left (sidebar) to match the content area:
mmeMain.directive('mirror', function () {
return {
restrict: 'A',
link: function (scope, elem, attr) {
scope.$watch(function () {
angular.element(document.getElementById(attr.mirrorReflect)).height(elem.height()-85);
});
}
};
});
HTML
<div id="leftColumn" class=sidebar col-xs-6 ">
// html code
</div>
<div id="rightColumn" class="content form-column col-xs-6" mirror mirror-reflect="leftColumn" >
// html code
</div>
If you look at this fiddle: http://jsfiddle.net/bastien/PybrF/1/
#header {
position: fixed;
top: 0px;
left: 0px;
height: 50px;
width: 100%;
background-color: yellow;
}
#content {
top: 51px;
left: 0px;
bottom: 0px;
width: 100%;
height: 100%;
position: fixed;
overflow: auto;
background-color: orange;
}
If you resize the window then the vertical scrollbar gets visible in the content div. BUT it gets only visible (so it seems for me...) when I have exceeded the height in pixel of the header while resizing the window.
How can I get the vertical scrollbar correctly?
UPDATE
I want a header which stays fixed.
I want a content which has inside scrollbars.
something like this: http://jsfiddle.net/bastien/PybrF/7/
but the vertical scrollbars should start inside the content div and not start at the header/body.
Try this in your css:
* { margin: 0; padding: 0 }
#header, #content { width: 100%; position: absolute; }
#header {
height: 50px;
background-color: yellow;
}
#content {
top: 50px;
height: 70%;
overflow-y: auto;
background-color: orange;
}
Will produce this:
As for the height of the content to use all the space left, I would to a js function wired to the resize event to set the height of the content to the page height minus the height of the header. I honestly don't know another solution for this.
Due to your use of fixed positioning and application of overflow settings, only the #content area will scroll.
Consider this:
1) Add the orange background color to the body element and remove its margins:
body {
margin:0px;
padding:0px;
background-color: orange;
overflow-x: hidden;
overflow-y: auto;
}
2) Position the other elements relatively:
#header {
position: relative;
height: 50px;
background-color: yellow;
}
#container {
position:relative;
}
http://jsfiddle.net/PybrF/6/
EDIT:
I'm still unclear on what you're looking for, but here's another method.
This one keeps the header fixed and puts the scrollbar inside the #content area.
body {
background-color: orange;
margin:0px;
}
#header {
position: fixed;
top: 0px;
left: 0px;
height: 50px;
width: 100%;
background-color: yellow;
z-index:1; /* keep the header on top of the content */
}
#content {
position:relative;
padding-top:50px; /* height of the header */
}
http://jsfiddle.net/PybrF/8/
ok I knew it must work:
Still found some old similar code and refactored it:
have fun! :)
Sorry for telling crap.
Remove the width/height percentage settings and use the left/right/bottom etc settings. Thats enough.
Forget about the main div which was from this other project long ago.
http://jsfiddle.net/bastien/PybrF/12/
I am trying to do the following in a CSS template:
Dock the footer to the bottom when there is not enough content to
fill the page
Stretch the header and footer background across the whole width
Position all the content in the middle of the page
This is the code I have, created with help on here:
http://tinkerbin.com/lCNs7Upq
My question is, I have seen a few ways to achieve this. Is this the best? It seems a shame to have to have the empty div as well, is this a bodge?
You can fix and element to the footer using CSS:
position: fixed;
bottom: 0;
However, I'm trying to figure out what exactly your trying to do.
You header and footer should automatically go 100% across the page if it's a div.
Your middle section can be set to height: auto; via css and will fill up the viewport pushing the footer all the way to the bottom, but to do this you also have to set the body to 100% in order to get it to work.
html, body, #content {
height: 100%;
min-height: 100%;
}
#header {
height: 100px;
width: 100%;
background: blue;
position: fixed;
top: 0;
}
#content {
height: auto;
margin: 100px auto;
background: green;
}
#footer {
position: fixed;
bottom: 0;
height: 100px;
width: 100%;
background: red;
}
Your HTML should look somewhat like this:
<div id="header"></div>
<div id="content"></div>
<div id="footer"></div>
Working Example: http://jsfiddle.net/s4rT3/1/
This is the best example I have seen:
http://css-tricks.com/snippets/css/sticky-footer/
* {
margin: 0;
}
html, body {
height: 100%;
}
.page-wrap {
min-height: 100%;
/* equal to footer height */
margin-bottom: -142px;
}
.page-wrap:after {
content: "";
display: block;
}
.site-footer, .page-wrap:after {
height: 142px;
}
.site-footer {
background: orange;
}
<div class="page-wrap">
Content!
</div>
<footer class="site-footer">
I'm the Sticky Footer.
</footer>
Update: In 2019 using flex is a better option.