My project has the following requirements:
Header fixed to the top of the page
Content area has a white background and 100% height
No scroll bar when content is less than height of the screen
Must support IE7+ (a JS fix for IE is ok)
When content is taller then height of screen, scrolling it should stay within the white content area (not go under the header).
Here is my basic HTML:
<div class="wrap">
<div id="header">header</div>
</div>
<div class="wrap" id="content">content</div>
CSS:
body{background:#C0DEED url('https://si0.twimg.com/images/themes/theme1/bg.png') repeat-x 0px -80px fixed;}
html,body{height:100%;}
.wrap{width:300px; margin:0 auto;}
#header{position:fixed; background:#aaa; top:10px; width:300px;}
#content{height:100%; background:white; margin-top:40px}
Example: http://jsfiddle.net/zw3WS/
First question is how to get the content to have 100% height, not go under the header, and still not have an unnecessary scrollbar?
Second, if the content is taller than the screen, how would I make it scroll only in the white space, and not allow the content to scroll under the to bar as it currently does?
For scrolling "only in the white space", you can do it by setting position: fixed on the wrapper element, then absolutely positioning the header and content elements inside:
body{
background:#C0DEED url('https://si0.twimg.com/images/themes/theme1/bg.png') repeat-x 0px -80px fixed;
overflow: hidden; /* no scrollbars for page body */
}
.wrap {
width: 300px;
position: fixed;
top: 10px;
left: 50%; /* for horizontal centering */
margin-left: -150px; /* for vertical centering */
bottom: 0;
}
#header{
position: absolute;
background:#aaa;
top: 0;
left: 0;
right: 0;
}
#content{
background:white;
position: absolute;
bottom: 0;
top: 30px;
left: 0;
right: 0;
overflow: auto; /* this makes the scrollbar appear inside #content */
}
Demo: http://jsbin.com/osipin/1/edit
For scrolling in the page body, you need to add two elements to your markup: a background for the header, and a background for the content.
The purpose of the header background is to cover up the content when it's scrolled down, where otherwise it would appear underneath the header. What you use to cover the content is simply the same background as the page. You must size this bg element correctly so that it fills the width of the viewport, and is the height of the top margin of your content area. The real header can be horizontally centered within this bg element using a set width and margin: 0 auto.
The content background element should be an empty element which precedes the content, and has a fixed position. Its purpose is to ensure that the white area extends to the bottom of the page even when the content is shorter than the viewport height.
Your new CSS looks like this:
body, .header-bg {
background:#C0DEED url(https://si0.twimg.com/images/themes/theme1/bg.png) repeat-x 0 -80px fixed;
}
.wrap {
width:300px;
margin: 0 auto;
}
.header-bg {
position: fixed;
top: 0px;
left: 0;
right: 0;
height: 40px;
}
#header {
background:#aaa;
width:300px;
margin: 10px auto 0;
}
.content-bg {
background: #FFF;
position: fixed;
width: 300px;
top: 40px;
bottom: 0;
z-index: -1;
}
And your new markup like this:
<div class="wrap">
<div class="header-bg">
<div id="header">header</div>
</div>
<div class="content-bg"></div>
<div id="content">
CONTENT
</div>
</div>
Demo: http://jsbin.com/osipin/4/edit
Related
I want the size of my footer in the bottom of the page to be the same as my navigation bar in the top (with some white space in the corners).
The bar is inside a container which has container{margin:auto;}thats why there is white space in the corners .
I don't understand why the footer took the whole screen width, it is inside the same container as the bar on top.
here is the footer css:
.down{
position: absolute;
margin: auto;
right: 0;
bottom: 0;
left: 0;
padding: 1rem;
background-color: gray;
text-align: center;
}
I don't want to edit the margin left and right because it wont be responsive anymore
Tough to say without the HTML but try adding the following rule to the container:
.container {
position: relative;
}
The footer has a position absolute on it. By giving the container a relative position, the footer will be positioned relative to the container instead of the body.
This will cause the footer to not be attached to the bottom of the window, but the bottom of the content. So in addition, add a minimum height to the container:
.container {
min-height: 100vh;
}
you have to put your <div class="down"> inside the container
<div class="container">
<nav></nav>
<div class="down">
</div>
</div>
.down{
position: absolute;
margin: auto;
right: 0;
bottom: 0;
left: 0;
padding: 1rem;
background-color: gray;
text-align: center;
}
I have absolutely positioned text so that it sits inside of my header image, but I cannot figure out how to keep it from moving outside of the header when the browser gets re-sized. If the browser window gets re-sized to a smaller size, the text moves outside of the header, but if the browser window gets re-sized to a bigger size, the text keeps moving to the right of the header!
The header is 800px wide and 150px tall, and it's positioned in the middle of the browser window.
Here's my HTML code:
<div id="container">
<div id="header">
<img src="images/header.jpg" alt="Site Header Image">
<h1>Our Site</h1>
<h2>Catchy slogan...</h2>
</div>
</div>
Here's my CSS code:
body {
margin: 0px;
padding: 0px;
}
#header h1 {
color: #FFFFFF;
position: absolute;
top: 0px;
left: 305px;
}
#header h2 {
color: #FFFFFF;
position: absolute;
top: 30px;
left: 330px;
}
#header img {
width: 800px;
height: 150px;
display: block;
margin-left: auto;
margin-right: auto;
}
There are two issues here:
Absolute positioned elements are laid out with respect to a relative positioned parent. You didn't specify that either #container or #header are relative positioned, so everything is aligned with respect to body - probably not what you want.
Your two container divs, #container and #header are full browser width. You want to constrain them to 800px, to match the image, and center them with margin: auto:
#header {
position: relative;
width: 800px;
margin: auto;
}
Here's a Codepen:
http://codepen.io/eldarshamukhamedov/pen/dGKJGm
That is because absolute positioning works relative to the body IF it does not have any parent with position:relative
Add this code
#header {
width:800px; /* define a width to the header container */
position:relative; /* see note */
margin:0 auto; /* centers header horizontally */
}
Is there an easy way with just css to make a div expand to the full height of the page - 30px. I have a "footer" at the bottom of the page that is 30px tall and set to position: fixed; bottom: 0px; I don't want any of the content from the rest of the page to show behind this footer.
<body>
<div id="wrapper">
<div id="header">Header added just for demonstration purposes</div>
<div id="content">Main content goes here</div>
<div id="footer">And this is my footer</div>
</div>
now style
html, body {
margin: 0;
padding: 0;
height: 100%;
}
#wrapper {
height: auto !important;
min-height: 100%;
height: 100%;
position: relative; /* Required to absolutely position the footer */
}
#footer {
height: 50px; /* Define height of the footer */
position: absolute;
bottom: 0; /* Sit it on the bottom */
left: 0;
width: 100%; /* As wide as it's allowed */
}
#content {
padding-bottom: 50px; /* This should match the height of the footer */
}
i would probably place all in a wrapper and set the size to 100%,
But in new css3 you have calc() which does exactly what you need: http://updates.html5rocks.com/2012/03/CSS-layout-gets-smarter-with-calc
Please note that not all (even modern) browsers yet have support for calc()
I have a 600px wide footer graphic that I want to be positioned in the center of the page. I currently have it fixed at the bottom of the browser, which is what I want - I want the content to scroll from underneath the footer, if you know what I mean.
With the fixed value my footer hangs on the left. With a value of relative, its centered, but stuck to the bottom of the page, and I end up having a load of blank white space to scroll down until I see the footer. Infact, removing the whole position: property has the same effect as the relative value.
How can I fix this?
I attempted ideas here - How to horizontally center a <div> in another <div>? but none of them worked =[
CSS
#footer {
position: fixed;
margin-top: -110px;
height: 110px;
width: 600px;
margin: 0 auto;
clear:both;
bottom: 0;
}
html:
<div class="footerWrap">
<div class="footer"></div>
</div>
css:
.footerWrap {
width:100%;
position:fixed;
bottom:0;
}
.footer {
margin:auto;
width:600px;
height:110px;
}
working demo
hope this help
Try this
html, body, and other div relative {
width: 100%;
}
#footer {
position: fixed;
margin-top: -110px;
height: 110px;
width: 600px;
clear:both;
bottom: 0;
left: 50%;
margin-left: -300px; /* half size of footer */
}
My page has the main content, a sticky footer and a right panel that is absolutely positioned. I normally never use absolute positioning, but in this case I need the panel to overlap the main content when the browser window is thin. Currently it looks correct until I shrink my window too far vertically. When the content continues below the page break, the absolutely positioned div no longer meets the footer. How can I ensure that the absolutely positioned div is always long enough to meet the footer so there is no gap?
Here's the HTML:
<body>
<div id="abs"></div>
<div class="wrapper">
<p>Website content here.</p>
<div class="push"></div>
</div>
<div class="footer">
</div>
</body>
And the CSS
* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -4em;
}
.footer, .push {
height: 4em;
}
.footer {
background-color:#333;
position:relative;
z-index:40;
}
#abs {
position:absolute;
right:0;
top:0;
z-index: 20;
background-color:#579ECF;
width:100px;
min-height: 100%;
}
p {
width:700px;
}
You absolutely position abs in the body tag. For reasons I won't go into body is the height of the window, not of the content. So when you set it to 100% it is only as tall as the screen.
Instead move the abs div into the main content div, set main content to relative, and specify top: 0 and bottom: 0 to stretch abs out.
http://jsfiddle.net/3jegZ/
.wrapper {
position: relative;
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -4em;
}
#abs {
position:absolute;
right:0;
top:0;
bottom:0;
z-index: 20;
background-color:#579ECF;
width:100px;
}