I'd like to have a sticky header with a %-height property. Sections below the header should take up the remaining height of the page, for example: header=10% all other sections are atleast 90%. This is similar to a related question: CSS Sticky Header/Footer and Fully Stretched Middle Area?, but he's using fixed px-height whereas i want %-height. I tried to use margin on my section, but that doesn't seem to work. Not does it seem to work to use a margin and 90% height on my sections.
For the moment I was able to come up with: http://jsfiddle.net/K9m63/. But a few problems:
The first section dissapears underneath the header.
Because of point 1, the section div's are too high and therefore not taking the remaining size.
HTML
<header>
<nav>Test</nav>
</header>
<section>
<div class="container yellow">1</div>
</section>
<section>
<div class="container pink">2</div>
</section>
<section>
<div class="container purple">3</div>
</section>
CSS
body, html {
height: 100%;
}
header {
height: 10%;
background-color: green;
position: fixed;
top: 0px;
width: 100%;
}
.helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
.nav-image {
vertical-align: middle;
}
section {
height: 100%;
width: 100%;
background-color: red;
}
.container {
width: 72.8125%;
background-color: blue;
margin: 0px auto;
height: 100%;
}
.yellow {
background-color: yellow;
}
.pink {
background-color: pink;
}
.purple {
background-color: purple;
}
Thanks!
Possible solution:
I have wrapped all sections into 2 divs.
<div class="wrapper">//rest 90% of the page
<div class="wrapper2">//100% of parent
<section>
<div class="container yellow">1</div>
</section>
<section>...
</div>
</div>
CSS:
.wrapper {
min-height:90%;
height:auto !important;
position:relative;
top:10%;
}
.wrapper2 {
height:100%;
width:100%;
position:absolute;
}
Also, add z-index:1; to header.
Updated fiddle here.
Based on your drawing, this is how you could* do it. - but there's also "fixed" / or "Sticky" positioning. - and this layout would force you to implement your own scroll below - in the page content, which is a pain.
html, body {
height: 100vh; /* you can use vh or % - but either way... */
height: 100%; /* they don't know their own height... how would they? */
}
body {
margin: 0;
}
.site-header {
background: #ff6666;
height: 10%;
}
.page-content {
background: #6666ff;
height: 90%;
}
<header class="site-header">
header
</header>
<main class="page-content">
main
</main>
Related
I'm new at coding and I've managed to figure out some things, but this one is bugging me deeply as I can't seem to find a solution.
I have an horizontal & vertically centered div on a page. I want to place a header on top of it, without decentering the main div.
How it looks like now (both are centered as a whole):
How I want it to look (yellow is centered, blue header on top):
..
Basic code:
.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
.header {
width: 1000px;
height: 100px;
background-color: blue;
margin-left: auto;
margin-right: auto;
}
.main {
width: 1000px;
height: 500px;
background-color: yellow;
margin-left: auto;
margin-right: auto;
}
<div class="outer">
<div class="middle">
<div class="header"></div>
<div class="main">
</div>
</div>
</div>
This is most likely not the best answer, but it's a start.
Baisically I centered the container using this method. Then I added the -50px to the top attribute of the container (half of the header height), moving the container 50px upwards, making the content div totally centered again. This solution should work on most newer browsers, but has some "limits" more here.
HTML
<div class="centered-container">
<div class="header">
header stuff
</div>
<div class="content">
Content stuff here.
</div>
</div>
CSS
body {
background: #600;
}
.centered-container {
position: absolute;
left: 50%;
top: 50%;
top: calc(50% - 50px);
transform: translate(-50%, -50%);
width: 600px;
background: red;
color: white;
text-align: center;
}
.header {
height:100px;
background:blue;
}
.content {
height:300px;
background:teal;
}
fiddle here.
I made the content 600px wide and 300px high and header 100px high, just so it is easier to see.
The negative margin
<!DOCTYPE html>
<html>
<!-- Handles the init code(javascript,css,links) and style references -->
<!-- Also, use body and head tags (THEY ARE IMPORTANT) -->
<head>
<style>
/** Web browsers load whatever is in the <head> tag FIRST
*/
.outer {
display: table;
position: absolute;
height: 100%;
width: 100%;
}
.middle {
display: table-cell;
vertical-align: middle;
}
/* You can use "margin: 0 auto;" to center this object.
* No need for left and right margin centering.
*
* Also, set the position to be relative then try adding your heading object
*/
.header {
width: 1000px;
height: 100px;
background-color: blue;
margin: 0 auto;
position: relative;
}
/* You don't need the margin to be 0 auto on both right and left
* if you have the width 100%
*/
.main {
width: 100%;
height: 500px;
background-color: yellow;
margin: 0;
}
</style>
</head>
<!-- Everything In Body Tag is style elements or skeletal HTML (div's, span's, format)-->
<!-- Place the heading OUTSIDE of the header element (in the outer div) this shouldn't alter the position of the
header. -->
<body>
<div class="outer">
<div class="middle">
<div class="header"></div>
<div class="main">
</div>
</div>
</div>
</body>
I would like my home page for a website to be responsive across multiple screens (& thus I have been told to use percentages) and I want the website to consist of four parts:
A Navigation Bar at the top of the screen that stretches to the edges of the screen on both sides, including the top. To make this simple, I'll have the background color be black. [Approx. 10% of the height of the page from the top]
Then I want an area of content in the "Middle" portion of the homepage with a background color of green. [60%]
Towards the bottom I want a smaller area with a background color of blue. [20%]
A Footer the same size as the navigation bar that has a color of black as well. [10%]
My problem is this:
- How do I target, or what do I need to do to have the div's inside of the main and footer tags be the colors I want them to be and their respective heights?
P.S. I am capable of changing the background color for the navigation bar, but nothing for the main or footer elements works.
<html>
<head>
<!--Header Stuff-->
</head>
<body>
<div class="wrapper">
<nav id="navigaton_bar">
<!--Navigation Stuff-->
</nav>
<main>
<div class="home_body">
<!--Content-->
</div>
<div class="lower_home_body">
<!--Content-->
</div>
</main>
<footer class="footer_bar">
<!--Footer Stuff-->
</footer>
</div> <!--END OF WRAPPER-->
</body>
</html>
Here is the CSS Code:
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#navigation_bar {
background-color: black;
position: absolute;
height: 10%;
width: 100%;
}
.home_body {
background-color: green;
height: 60%;
width: 100%;
}
.lower_home_body {
width: 100%;
height: 20%;
background-color: blue;
}
.footer_bar {
width: 100%;
height: 10%;
background-color: black;
}
Problems with your code:
Typo in id attribute of nav: navigaton_bar (vs. navigation_bar in CSS).
You need to set height to 100% for the wrapper element.
You need to set height to 80% for the main element, and then use 75% and 25% for the two contained divs in order to achieve 60% and 20% of the overall page height.
You shouldn't have absolute positioning on the navigation bar.
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
.wrapper {
height:100%;
}
#navigation_bar {
background-color: black;
height: 10%;
width: 100%;
}
main {
height:80%;
}
.home_body {
background-color: green;
height: 75%;
width: 100%;
}
.lower_home_body {
width: 100%;
height: 25%;
background-color: blue;
}
.footer_bar {
width: 100%;
height: 10%;
background-color: black;
}
<div class="wrapper">
<nav id="navigation_bar">
<!--Navigation Stuff-->
</nav>
<main>
<div class="home_body">
<!--Content-->
</div>
<div class="lower_home_body">
<!--Content-->
</div>
</main>
<footer class="footer_bar">
<!--Footer Stuff-->
</footer>
</div> <!--END OF WRAPPER-->
I learn more every day, but so far, I have learned responsive design in CSS uses percentages for width of the page, not height.
Responsiveness usually involves beginning the design in the mobile screen size, then adding content as the screen size gets larger. It also means that users usually scroll down to see more and nav bar items that would be horizontal on a full-sized desktop would be stacked on top of each other on a mobile screen size, or minimized and represented by an icon or "menu", to be touched by the user, thereby displaying a stacked menu.
Hope this link helps.
Add basic divs and content to the html file, then start styling with the CSS file for color and height.
Try vh instead of %, for example
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100vh;
}
#navigation_bar {
background-color: black;
position: absolute;
height: 10vh;
width: 100%;
}
.home_body {
background-color: green;
height: 60vh;
width: 100%;
}
.lower_home_body {
width: 100%;
height: 20vh;
background-color: blue;
}
.footer_bar {
width: 100%;
height: 10vh;
background-color: black;
}
I try to create a small website. Now I've problems with the positioning of several html attributes. What I'll do is quite simple: the header should have a width of 100% and fixed on the top. The footer should have also a width of 100% and fixed on the button. The vertical navigation bar should fill the space between the footer and the header. The content, should fill the rest, with a margin of 10px. Here's my actual try:
CSS:
* {
margin: 0px;
padding: 0px;
border: 0px;
}
html, body {
height: 100%;
width; 100%;
}
#pageWrapper {
height: 100%;
}
header{
height: 50px;
width: 100%;
background-color:yellow;
}
footer{
height: 50px;
width: 100%;
background-color:blue;
}
#mainWrapper{
width:100%;
height: 100%;
background-color:black;
}
#mainWrapper #navigation {
width: 250px;
height: 100%;
background-color:orange;
float: left;
}
#mainWrapper #content {
height: 100%;
width: 100%;
background-color: green;
}
HTML:
<body>
<div id="pageWrapper">
<header>
</header>
<div id="mainWrapper">
<div id="navigation">
</div>
<div id="content">
<p>Test content</p>
</div>
</div>
<footer>
</footer>
</div>
</body>
https://jsfiddle.net/6ptmq4ce/3/
What you can see is, that the size of this page is bigger than 100%, there is a scrollbar. How can I get this scrollbar away? And how I can set for the content a margin of 10px?
You are using 100% for all elements which will push the bottom out of the viewport so some elements have to be less than 100%
#mainWrapper {
height: calc(100% - 100px);
}
-100px means you take out the header and footer
if you dont mind using vh you can solve it like this.
We're just making the footer and header each 10% height of the viewport with height:10vh and the content 80% with height:80vh
I'm theming a Drupal website and using the vegas full screen bg.
I want to achieve the following:
But I have some trouble by theming the footer: I want it to be always displayed under the background image (so you have to scroll down to see the footer) now it keeps coming over the background image. Besides that I want the main menu and footer to become full width and not 960px like the container. But I can't seem to get these 2 to 'break out' the container.
Now I've:
#footer {
position: absolute;
bottom:0;
width: 100%;
height:100px;
background-color: #202020;
}
#primary-menu-bar{
position: absolute;
width: 100%;
height: 60px;
padding: 0;
margin: 0;
background-color: rgba(255,255,255,0.70);
padding-top: 10px;
}
Normally something like this does the trick but I'm struggling to get this right...
Anybody any advice or solutions?
You didn't show any HTML, so I just came up with some HTML myself. If the footer is only visible when you scroll down you need to have some sort of wrapper for both your header and your content element. You can then set the wrapper min-height to 100% and use background-image/background-size for a full-screen image background.
HTML:
<div class="wrapper">
<header class="page-head" role="banner">
Header
</header>
<main class="main" role="main">
Content
</main>
</div>
<footer class="page-foot" role="contentinfo">
Footer
</footer>
CSS:
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
background-image: url(http://placehold.it/1200x800);
background-position: center center;
background-size: cover;
}
.page-head {
background: red;
}
.main {
width: 80%;
margin: 0 auto;
background: yellow;
}
.page-foot {
background: blue;
}
See example on this pen.
here is a possible solution: http://jsfiddle.net/09mcoo2h/1/
as i said in the comment below your question: you need to have footer and header outside the container (that is the only with 960px)
To have a footer TO THE BOTTOM of the page, just set the body as position:relative.
HTML
<div id="primary-menu-bar"></div>
<div id="container"></div>
<div id="footer"></div>
CSS
body {
margin:0;
padding:0;
position:relative;
}
#container {
display:block;
width:960px;
height:1600px;
background:#eee;
margin:0 auto;
}
#footer {
position: absolute;
bottom:0;
width: 100%;
height:100px;
background-color: #202020;
}
#primary-menu-bar{
width: 100%;
height: 60px;
top:0;
background-color: #F00;
padding-top: 10px;
}
It's really hard for us to do it like this with out HTML.
So basically what you need to do is place the footer and header outside the container. Because the container is 960px, so the header and footer can go over it.
The structure should be like this:
<body>
<header></header>
<div class="container"></div>
<footer></footer>
</body>
Example on codepen
I know there is a lot of same topics, but is there any CSS way to stick bottom a footer with an height in % without overflowing the body and the header because of absolute position ?
I'm trying to stick this one :
html,body{
height: 100%;
}
#header{
background-color: yellow;
height: 100px;
width: 100%;
}
#holder {
min-height: 100%;
position:relative;
}
#body {
padding-bottom: 100px;
}
#footer{
background-color: lime;
bottom: 0;
height: 100%;
left: 0;
position: relative;
right: 0;
}
with html :
<div id="holder">
<div id="header">Title</div>
<div id="body">Body</div>
<div id="footer">Footer</div>
</div>
Code here : http://jsfiddle.net/TsRkB/
Thanks !
if you use display:table as a base , then your sticky footer can be any size and will be pushed down if content grows.
http://dabblet.com/gist/5971212
html {
height:100%;
width:100%;
}
body {
height:100%;
width:100%;
display:table;
table-layout:fixed;
margin:0 auto;
width:80%;
}
.tr {
display:table-row;
background:turquoise
}
section.tr {
height:100%;
background:yellow
}
for
<header class="tr"> <h1>title</h1><p>make me grow</p></header>
<section class="tr"><article>article</article></section>
<footer class="tr"> <p>Footer</p><p>make me grow</p></footer>
All the other solutions are out of date and have a major shortcoming: they don't work if the height of the footer is variable or unknown.
With the advent of the CSS flex model, solving this problem become very, very easy: while mostly known for laying out content in the horizontal direction, Flexbox actually works just as well for vertical layout problems. All you have to do is wrap the vertical sections in a flex container and choose which ones you want to expand. They'll automatically take up all the available space in their container.
Note how simple the markup and the CSS are. No table hacks or anything.
The flex model is supported by all major browsers as well as allegedly IE11+, though my IE doesn't render this snippet correctly yet.
html, body {
height: 100%;
}
#header {
background: yellow;
height: 100px; /* can be variable as well */
}
#wrapper {
display: flex; /* use the flex model */
min-height: 100%;
flex-direction: column; /* learn more: http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ */
}
#body {
flex: 1;
border: 1px solid orange;
}
#footer{
background: lime;
}
<div id="wrapper">
<div id="header">Title</div>
<div id="body">Body</div>
<div id="footer">
Footer<br/>
of<br/>
variable<br/>
height<br/>
</div>
</div>
Fixed it for you, basically have to put the footer outside the wrapper and move it up...
http://jsfiddle.net/sMdmk/4/
#footer{
background-color: lime;
bottom: 0;
height: 10%;
left: 0;
position: relative;
right: 0;
top: -10%;
}