Fixed Header/Footer + Content height 100% cause undesired vertical Scrolling - html

I am trying to achieve a a horizontal scrolling website with a fixed header and footer.
Goals:
1. Fixed Header and Footer
2. No vertical scrolling
3. Content div fills all space between the header and footer
I used position: absolute on the content to make sure the height:100% takes up the area between the header and the footer. (my third goal)
However this also causes a vertical scrollbar to appear.
live demo:
http://jsfiddle.net/wQ2XR/230/
how can i achieve my goals without a vertical scrollbar to appear?
thanks a lot in advance!
The html code:
<div id="total">
<header id="1">
<div id="a">
<h1>Header</h1>
</div>
</header>
<div id="2">
<div id="b">
<div id="bb">
<h2>Post Title Example One</h2>
<p>hello world! Have you thoroughly searched for an answer before asking your question? </p>
</div>
</div>
</div>
<footer id="3">
<div id="c">
<h1>footer</h1>
</div>
</footer>
</div>
the css:
body, html {
height: 100%;
padding: 0;
margin: 0;
}
body {
width: 100%;
}
header {
}
#a {
position: fixed;
height: 50px;
top: 0;
width: 100%;
background-color: blue;
}
#2 {
position: relative;
padding: 50px 0 25px 0;
}
#b {
height: 100%;
position: absolute;
}
#bb {
position: relative;
height: 100%;
margin: 50px 0 0 0;
width: 2000px;
background-color: yellow;
}
footer {
}
#c {
position: fixed;
height: 25px;
bottom: 0;
width: 100%;
background-color: green;
}

Hmmm, the problem is that the wrapper(s) around your content between the header and footer are taking on the height of the viewport with height:100%. So, when you apply a margin to vertically offset those content wrappers (so that the header becomes visible), they get pushed by that much below the viewport (50px, height of the header). As a result, you get a vertical scrollbar, since the content wrappers are both the full height of the viewport and pushed down - so they can't fit on-screen.
How to avoid this? Well, if your footer and header height won't be dynamic (ie. You'll always be in control of how tall they are through your CSS), you can achieve this in a fairly straightforward manner with position:absolute.
Your structure I modified slightly; I removed the #2 and #b elements, since it looks like they were just there to properly position/size #bb, the actual content-containing element:
<div id="total">
<header id="1">
<div id="a">
<h1>Header</h1>
</div>
</header>
<div id="bb">
<h2>Post Title Example One</h2>
<p>hello world! Have you thoroughly searched for an answer before asking your question?</p>
</div>
<footer id="3">
<div id="c">
<h1>footer</h1>
</div>
</footer>
</div>
Now, with your CSS, I removed the definitions for styling #2 and #b. Additionally, I modified the #bb CSS to read as:
#bb {
position: absolute;
top: 50px;
bottom: 25px;
width: 2000px;
background-color: yellow;
}
Here's an updated JSFiddle to demonstrate what this achieves. Additionally, here's a JSFiddle implementing your multiple-row layout which you gave as a comment in one of the answers.
The reason why overflow:hidden doesn't quite work is because #bb would actually still extend below the viewport - just, no vertical scrollbar would be created because that overflowing region is ignored by the browser. However, when you use a percentage height, it becomes apparent that the height of #bb is not that which is visible. Anyways, hope this helps out! If this isn't what you were looking for, let me know and I'll be happy to help further. Good luck!

To hide the scrollbar use:
overflow: hidden;
However, the text needs to go somewhere (otherwise it will be hidden), so you need to have the container larger or use text-columns.
Do you intend to achieve something like Windows 8 Metro UI for the scrolling?

Related

How to make independent horizontal scrolling on a div container?

I want to make it so that one div can scroll horizontally independently of the other div. Scrolling divs should have a minimum width (e.g. 500px) and not be aligned to the width of the content. The other div has a width of 100%. How can i do this?
<div>
<div #parent style="width: 100%"></div>
<div #child style="position: relative; width: 100%">
<div #child class="child-container"></div>
</div>
</div>
Here is my css:
.child-container {
position: absolute;
overflow: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
min-width: 500px;
}
I edited the post to be more realistic
What you're looking for is the CSS property overflow-x which will allow you to specify the overflow behavior with CSS.
Here is MDN's documentation on this property.
The overflow-x CSS property sets what shows when content overflows a block-level element's left and right edges. This may be nothing, a scroll bar, or the overflow content.
Update
Here is a working example of what you are asking for. If I'm not understanding your question, please let me know.
.padding {
padding:25px;
}
.container {
max-width:400px;
}
.child-container {
background:#dedede;
overflow-x:scroll
}
.child-item {
min-width: 500px;
}
<div class="container padding" style="background:#ededed;">
<div class="padding">
<h1>Parent</h1>
</div>
<div class="child-container padding">
<div class="child-item">
<h1>Hello world</h1>
</div>
</div>
</div>

How can I float a div element to the bottom of the screen with CSS?

I am making a website with different sections, one at the bottom of the screen. I first tried putting the div element inside of a footer element. This didn’t work. The div was still at the top of the screen. I was out of ideas, so I searched for a solution on Google. Nothing I tried worked. How can I do this?
If you want it to always be at the bottom of the screen (IE: touching bottom of monitor) you can use
.myElementClass{
position: absolute;
bottom: 0;
}
Just make sure that it's not inside anything declared with position: relative or it will be stuck at the bottom of that element.
If you want it to be at the bottom of the wrapper element, use
.myWrapperClass{
position: absolute;
}
and put the .myElementClass element inside the .myWrapperClass element
If you need your footer to be seen always on the screen or like this at the end of the page, here is the answer. The explanation is same as zachThePerson's.
.main {
position: relative;
height: 700px;
width: 100%;
background: blue;
}
.box {
position: absolute;
bottom: 0px;
width: 100%;
}
.box1 {
width: 100px;
height: 100px;
background: red;
float: right;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="main">
<h1>The float Property</h1>
<p>In this example, the image will float to the right in the text.</p>
<div class="box">
<div class="box1"></div>
</div>
</div>
</body>
</html>
You could use Flexbox, and the below solution could be useful.
You can put the main content of your page in the div with class main-content, and with the Flexbox settings below, the footer will always be pushed to the bottom.
HTML:
<body>
<div class="flexbox-container">
<div class="main-content">Just a random div with some content</div>
<div class="footer">Div to be on the bottom of the page</div>
</div>
</body>
CSS:
.flexbox-container {
display: flex;
flex-direction: column;
justify-content: space-between;
}
The above code won't change the height of the flexbox-container div, though. So you may want to add something like this:
.flexbox-container {
...
height: 100vh;
}
A good page with explanation for the Flexbox being used is A Complete Guide to Flexbox.

how to override css class with an underlying class

I have written a small code of CSS which goes like this,
<div class="wrapper">
the wrapper has width:1120px; everything inside it
<div class="callout">
I want this to be full width rather than width 1120px
</div><!-- end for callout -->
other things inside it normal at width 1120px
</div><!-- end wrapper -->
How can I achieve this How can I disable the wrapper behavior for the specific class inside it
One standard way to approach this situation is to add .callout beneath (and, importantly, outside) .wrapper in the structural markup...
... and then use css to push .callout back up the display so it no longer appears beneath .wrapper in the browser display.
Of course, if you take this approach, you must be sure to leave enough space for .callout to be moved up into.
.wrapper {
width: 1120px;
border: 1px solid red;
}
div p {
height: 40px;
line-height: 40px;
margin: 20px;
}
.wrapper p:nth-of-type(2) {
margin-bottom: 80px;
}
.callout {
position: relative;
top: -140px;
background-color: blue;
}
<div class="wrapper">
<p>the wrapper has width:1120px;</p>
<p>everything inside it</p>
<p>other things inside it normal at width:1120px</p>
</div>
<div class="callout">
<p>I want this to be full width rather than width:1120px</p>
</div>
You wrote: "I have written a small code of css which goes like this"
I assume that you know this is HTML, not CSS. If that's not clear, you are missing the basics. Anyway:
(This is no copy&pastable code solution, but it explains why you can't do what you want to do easiliy and what you can do if you want to do it anyway...)
Usually you achieve full width for a block element by giving it width: 100%. However, percentage width values are always in relation to the parent element (in your case .wrapper, not the window width). You could use javascript/jQuery to get the viewport/window width and assign that to your .callout class. If you do that, you also have to add .wrapper {overflow-x: visible;} to your CSS, otherwise the .callout's maximum width will still be limited to the wrapper's width.
Another solution would be to have the .callout DIV not inside the .wrapper DIV (but after or before it) in the HTML, so it's independent from it. Then you can simply define width: 100% for .callout. You could also have DIVs with class .wrapper before and after the .callout DIV.
You can also achieve an independent width by using position: absolute for .callout, however, in this case the absolutely positioned element is considered completely idependent (except the position reference), meaning that it will also have no vertical or horizontal space reserved for it in the parent element, which can result in content of the parent element being hidden or displayed in a not-intended way. (you'd have to insert something in the parent element that creates enough empty vertical space for your absolutely positioned child element before the parent content continues.But this solution is not so good, since the needed vertical space will always depend on screen/viewport width and on the content.
If you have to achieve it only using html and css, I think, you can do another way:
<section>
<div class="wrapper">
...
</div>
</section>
<section>
<div class="callout">
...
</div>
</section>
<section>
<div class="wrapper">
...
</div>
</section>
CSS
.wrapper {
width: 1120px;
}
...
If you want to keep the current markup and make no changes (as others noted), you may use position property like this:
Markup
<div class="parent">
<p>parent</p>
<div class="wrapper">
<p>wrapper</p>
<div class="callout">
<p>callout</p>
</div><!-- end for callout -->
</div><!-- end wrapper -->
</div><!-- end parent -->
CSS
.parent{
position: relative;
}
.wrapper{
width: 1120px;
margin: 0 auto;
}
.callout{
position: absolute;
left: 0;
}
In action
.parent{
position: relative;
height: 300px;
padding: 10px 0;
background-color: #99ff99;
text-align: center;
}
.wrapper{
width: 200px;
margin: 0 auto;
height: 100px;
padding: 30px 0;
background-color: #ff9999;
}
.callout{
position: absolute;
width: 100%;
height: 50px;
left: 0;
background-color: #9999ff;
}
<div class="parent">
<p>parent</p>
<div class="wrapper">
<p>wrapper</p>
<div class="callout">
<p>callout</p>
</div><!-- end for callout -->
</div><!-- end wrapper -->
</div><!-- end parent -->

Sticky Footer problems.. Here's another one

Firstly, apologies for what appears to be a very common question, looking at the amount of similar questions, you are forgiven for being annoyed at yet another, but regardless of all the others that I have read (and tried to implement), and many other links found on Google, I'm still struggling to solve my problem, so I'm sorry, but here goes..
The footer I have is fine when the main content is longer than the browser window, but when there is very little content, rather than sticking to the bottom of the browser window, it sticks to the bottom of the main content, leaving a horrible blank space below the footer.
I've created a simple(ish) fiddle HERE using a stripped down version of my code.. here is the code for those that are able to see the issue without "fiddling"..
<body>
<!-- Header -->
<div id="header-wrapper">
<header class="5grid-layout" id="site-header">
<div class="row">
<div class="12u">
<div id="logo">
<h1 class="mobileUI-site-name">HEADER</h1>
</div>
</div>
</div>
</header>
</div>
<!-- Main -->
<div id="main-wrapper" class="subpage">
<div class="5grid-layout">
<div class="row">
<div class="12u">MAIN CONTENT </div>
</div>
</div>
</div>
<!-- Footer -->
<div id="footer-wrapper">
<footer class="5grid-layout" id="site-footer">
<div class="row">
<div class="12u">PROBLEM FOOTER</div>
</div>
</footer>
</div>
</body>
and here is the css..
#header-wrapper {
background: #12ff00;
height: 110px;
position: relative;
padding: 0.0em 0 1em 0;
}
#main-wrapper {
border-top: 3px solid #662d91;
border-bottom: 3px solid #662d91;
background: #ff5a00;
position: relative;
padding: 1em 0 2em 0;
}
#footer-wrapper {
background: #ff00fc;
position: relative;
padding: 1em 0 1em 0;
height: 100px;
}
Granted, a lot of the above Divs aren't needed for the sake of this demo, but I have left them in just in case it is one of these thats causing the problem. I'm still new to this, so I honestly have no idea.
so basically, how on earth do I get that footer to behave, previous attempts based on other Stack Overflow answers have left me with either no footer, or a footer that sits in the center of the screen regardless of whether there is a lot or little content.
Any help will be gratefully received.
I have answered this question before
Click Here
Or see this JSFiddle for a working example of a sticky footer.
HTML
<div class="wrapper">
<div class="header"></div>
</div>
<div class="footer"></div>
CSS
* {margin: 0;}
html, body {height: 100%;}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -200px; /* the bottom margin is the negative value of the footer's height */}
.footer { height: 200px;background-color:#000;}
The idea is that the bottom margin is the negative value of the footers height
Have you tried:
html, body {
height: 100%;
min-height: 100%;
}
#main-wrapper {
height:100%;
}
Here's a working DEMO1
UPDATES:
I've changed a few things in your code, but now its working!
Here are the changes:
added a #container for the header and main divs.
I've changed the footer padding from em to px, because I need precise height.
I gave the main-wrapper's background to #container
and the border-bottom to footer as border-top
DEMO2
The way I ussualy do this is using
http://www.cssstickyfooter.com/using-sticky-footer-code.html
If you can, try to stick close to that, it has compatibility with older browsers.
I didn't find better alternatives to this and is well explained

HTML/CSS - Sidebar Length 100% Page Height, Regardless of Vertical Scrollbar

I'm sure this has been asked quite some times however after quite some time searching for a solution I have finally resorted to asking a question here!
I am working on an HTML Help file for an application I am developing, where the file is split into two sections. One, a sidebar (floated to the left) and two, the main content section (also floating left).
The problem I am facing is that when the main content extends beyond 100% of the page height, the sidebar background stops.
I would use the faux column effect where I assign the a background image of my sidebar however to maintain my design's integrity I have to set a different background image for the body.
Check out this JS Fiddle I set up - http://jsfiddle.net/5gpFx/
Hopefully that can help you see what the issue is if I failed to communicate it well enough!
Cheers!
Just wrap both columns in a container and set the faux-column background on that - this way the body will be available for setting whatever image you want. I've made a simple example here: http://jsfiddle.net/5gpFx/2/
I had the same problem but I could not make work the solution with flexboxes above. So I created my own template, that includes:
a header with a fixed size element
a footer
a side bar with a scrollbar that occupies the remaining height
content
I used flexboxes but in a more simple way, using only properties display: flex and flex-direction: row|column:
I do use angular and I want my component sizes to be 100% of their parent element.
The key is to set the size (in percents) for all parents inorder to limit their size. In the following example myapp height has 100% of the viewport.
The main component has 90% of the viewport, because header and footer have 5%.
I posted my template here: https://jsfiddle.net/abreneliere/mrjh6y2e/3
body{
margin: 0;
color: white;
height: 100%;
}
div#myapp
{
display: flex;
flex-direction: column;
background-color: red; /* <-- painful color for your eyes ! */
height: 100%; /* <-- if you remove this line, myapp has no limited height */
}
div#main /* parent div for sidebar and content */
{
display: flex;
width: 100%;
height: 90%;
}
div#header {
background-color: #333;
height: 5%;
}
div#footer {
background-color: #222;
height: 5%;
}
div#sidebar {
background-color: #666;
width: 20%;
overflow-y: auto;
}
div#content {
background-color: #888;
width: 80%;
overflow-y: auto;
}
div.fized_size_element {
background-color: #AAA;
display: block;
width: 100px;
height: 50px;
margin: 5px;
}
Html:
<body>
<div id="myapp">
<div id="header">
HEADER
<div class="fized_size_element"></div>
</div>
<div id="main">
<div id="sidebar">
SIDEBAR
<div class="fized_size_element"></div>
<div class="fized_size_element"></div>
<div class="fized_size_element"></div>
<div class="fized_size_element"></div>
<div class="fized_size_element"></div>
<div class="fized_size_element"></div>
<div class="fized_size_element"></div>
<div class="fized_size_element"></div>
</div>
<div id="content">
CONTENT
</div>
</div>
<div id="footer">
FOOTER
</div>
</div>
</body>