CSS3 negative margin issue - css

I know that my question was addressed many time on this site. However, I can't seem to find my answer. I'm trying to create a sticky footer on my pages.My pages are divided in 3 div.
top
middle
bottom
I've created a layout.css file. Here it is:
html,body {
margin: 0;
font-family: Tahoma, Geneva, sans-serif;
font-size: 14px;
height: 100%;
margin: 0 auto;
position:relative;
}
#pageTop {
height: 90px;
}
#pageMiddle {
min-height: 100%;
margin-bottom: -50px;
}
#pageMiddle:after {
content: "";
display: block;
}
#pageBottom, #pageMiddle:after {
height: 50px;
}
I don't understand how to remove the pageTop height from the pageMiddle min-height. This seems to be the issue.
Thanks for your help on this matter.

Hi all and thanks for giving me some pointers. After a few days, I finally got the answer.
Like mentioned above, I had 3 div:
top
middle
bottom
I was trying to apply a sticky footer on my bottom div. without any success.
Here is how I managed to solve this issue.
I inserted the top and middle div in a wrapper div. Having only 2 main divs on my page.
It looks like this now.
Wrapper
top
Middle
bottom
here is the css:
#wrapper{
min-height: 100%;
margin-bottom: -50px;
}
#top {
width:1000px;
margin:0 auto;
height: 90px;
}
#middle {
width:1000px;
margin:0 auto;
}
#wrapper:after {
content: "";
display: block;
}
#bottom, #wrapper:after {
width:1000px;
height: 50px;
margin:0 auto;
}
Here is my html template:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My title</title>
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style/style.css">
</head>
<body>
<div id="wrapper">
<div id="top">Header</div>
<div id="middle">Content</div>
<div id="bottom">Footer</div>
</body>
</html>
It is very simple but it works.
Again thanks for your replies.

Related

Html5 CSS Layout Footer

I am trying to create a sticky footer but I'm getting empty space above and below my header & footer.
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
header {
background-color: orange;
position: relative;
height: 400px;
width: 100%;
color: white;
text-align: left;
}
footer {
background-color: #202020;
color: white;
position: absolute;
width: 100%;
height: 60px;
bottom: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Porfolio</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<header>
<h1>header</h1>
<p>header</p>
</header>
<div class="wrapper">
content
</div>
<footer>
footer
</footer>
</body>
</html>
What is the best way to create a sticky footer?
Can anyone explain why I've got this space appearing above header & below footer when I have content (h1 p) in in my header section.
For the header gap, your h1 and p tags have a default padding and margin, you may want to remove them or reduce them to your liking
h1, p {
padding: 0;
margin: 0;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
h1,p {
padding: 0;
margin: 0;
}
header {
background-color: orange;
position: relative;
height: 400px;
width: 100%;
color: white;
text-align: left;
}
footer {
background-color: #202020;
color: white;
position: absolute;
width: 100%;
height: 60px;
bottom: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Porfolio</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<header>
<h1>header</h1>
<p>header</p>
</header>
<div class="wrapper">
content
</div>
<footer>
footer
</footer>
</body>
</html>
A "sticky" div can be achieved using position: fixed; in your footer CSS. Fixed means that the on-screen position will never change. Or rather you should follow the instructions posted there.
Concerning the space, it is probably because of the default styles applied to h1. Use a debugger to see those default styles and override them with your custom css.
Firefox and Chrome have built in debuggers that also let you view styles and are very efficient for debugging. Usually right click > "inspect element" then go for the CSS tab which lets your select and see styles applied to elements.
In your example, you are not "resetting" the h1 and p tags. By default these elements have some extra margin.
Try adding the following code to your css.
h1, p {
margin: 0;
}
Also check out the HTML5 CSS Sticky Footer.
you may use flex prperties
.wrapper may scroll, header & footer are sticky
/* demo purpose */
.wrapper:hover:before {
content:'test';
display:block;
height:1000px;
}
/* end demo purpose*/
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
body {
display:flex;
flex-flow:column;
}
header {
background-color: orange;
position: relative;
width: 100%;
color: white;
text-align: left;
}
footer {
background-color: #202020;
color: white;
width: 100%;
height: 60px;
bottom: 0;
}
.wrapper {
flex:1;
overflow:auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Porfolio</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<header>
<h1>header</h1>
<p>header</p>
</header>
<div class="wrapper">
content
</div>
<footer>
footer
</footer>
</body>
</html>
or just footer is sticky ?, needs an extra imbrication
/* demo purpose */
.wrapper:hover:before {
content:'test';
display:block;
height:1000px;
}
/* end demo purpose*/html,
body {
height: 100%;
margin: 0;
padding: 0;
}
body, main {
display: flex;
flex-flow: column;
}
header {
background-color: orange;
position: relative;
color: white;
text-align: left;
}
footer {
background-color: #202020;
color: white;
height: 60px;
bottom: 0;
}
.wrapper, main {
flex: 1;
}
main {
overflow: auto;
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Porfolio</title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<main>
<header>
<h1>header</h1>
<p>header</p>
</header>
<div class="wrapper">
content
</div>
</main>
<footer>
footer
</footer>
</body>
</html>
footer {
background-color: #202020;
color: white;
//replace absolute with fixed for sticky footer (as in, it sticks at the bottom.)
position: fixed;
width: 100%;
height: 60px;
bottom: 0;
}
I saw this comment you posted on another answer:
If desktop only, then I would go with fixed positioning; however, iOS has problems rendering fixed positioning at times. – SergeantHacker
Try removing height 100% from body and html.

Fixed footer with right margin

Can someone explain me why the first code does result in a fixed footer with a small margin on the right as I used an extra 'div' but that without this as seen in the second code it doesn't show a margin on the right? Thanks!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
body {
width: 100%;
margin: 0px auto;
}
.mymargin {
clear: both;
position: fixed;
bottom: 0px;
text-align: right;
width: 100%;
background-color: fuchsia;
}
footer {
margin-right: 20px;
}
</style>
</head>
<body>
Look in the right corner below!
<div class="mymargin">
<footer>
fixedfooterwithmargin-ontheright
</footer>
</div>
</body>
</html>
Second code without an extra div.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
body {
width: 100%;
margin: 0px auto;
}
footer {
clear: both;
position: fixed;
bottom: 0px;
text-align: right;
width: 100%;
background-color: fuchsia;
margin-right: 20px;
}
</style>
</head>
<body>
Look in the right corner below!
<div class="mymargin">
<footer>
fixedfooter NO hmargin-ontheright
</footer>
</div>
</body>
</html>
Try right instead of margin-right:
footer {
clear: both;
position: fixed;
bottom: 0px;
text-align: right;
width: 100%;
background-color: fuchsia;
right: 20px;
}
jsfiddle: http://jsfiddle.net/ashishanexpert/3eFPt/
Your footer tag is already taking 100% of the width.. So margin will be out of it.
It worked in the first case because you gave the width to the parent. so margin on the child worked. try to add 100% to footer in first case and even that wont work as required.

How to make 2 divs with 2 different backgrounds equal in height in when the height of one of them is set to auto and max height?

I've looked at 20 threads at least so far so sorry if this has been answered before but I couldn't find a solution that suits my particular css layout.
I want to set the height of 2 columns equal to each other in a way that the leftcolumn equals the contentcolumn. I've tried using multiple javascripts like this :
`
$(document).ready(function() {
// get the heights
l = $('#contentcolumn').height();
// get maximum heights of all columns
h = Math.max(l);
// apply it
$('#leftcolumn').height(h);
});
And:
document.getElementById("leftcolumn").style.maxHeight = document.getElementById("contentcolumn").style.height;
And:
$("#contentcolumn").height($("#leftcolumn").height())
The problem with the first code is that it drops the left div to some really long height which I don't even know. The second and third codes change nothing at all.
Can someone please help me I know there's probably a really simple solution to this problem but I just can't find and I just can't go to sleep until I do !
New webpage after clean up:
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="maincontainer">
<div id="topsection"></div>
<div id="leftcolumn"></div>
<div id="contentcolumn">
</div>
</font>
</body>
</html>
New CSS after clean up:
body,
html {
background: #cacaca url(img/bg.png) repeat-x;
}
#maincontainer {
width:1000px;
margin:0 auto;
background: url(img/bg5.png) repeat-x;
}
#topsection {
background: #ffffff url(img/bg4.png) repeat-y;
height: 10px;
}
#leftcolumn {
float:left;
height: 100%;
width: 145px;
background: url(img/bg2.png) repeat-y;
}
#contentcolumn {
margin-left: 145px; /*Set left margin to LeftColumnWidth*/
min-height: 800px;
height: auto;
background: #dbdbdb url(img/bg3.png) repeat-x;
padding:10px;
}
You can do this without javascript--in a cross-browser way, even. This takes advantage of absolutely-positioning elements within relatively-positioned elements. If you set your #maincontainer div to position: relative and your #leftcolumn div to position: absolute, you can then set both top and bottom on #leftcolumn, so it always assumes the height of its parent (#maincontainer), even though #maincontiner's height is being set by its children (#contentcolumn in this case). Use this jsfiddle demo and play with #contentcolumn's height to see how #leftcolumn responds.
HTML:
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<div id="maincontainer">
<div id="topsection"></div>
<div id="leftcolumn"></div>
<div id="contentcolumn"></div>
</div>
</body>
</html>
CSS:
body,
html {
background: #cacaca;
}
#maincontainer {
position: relative;
width:500px;
margin:0 auto;
background: #000;
}
#topsection {
background: #ffffff;
height: 10px;
}
#leftcolumn {
position: absolute;
top: 10px; /* room for #topsection */
bottom: 0;
left: 0;
width: 145px;
background: red;
}
#contentcolumn {
margin-left: 145px; /*Set left margin to LeftColumnWidth*/
min-height: 500px;
height: auto;
background: #dbdbdb;
padding:10px;
}

Full body background with Twitter Bootstrap

I am trying to work on a new project using Twitter's Bootstrap framework, but I am having an issue. I want a full body background, yet the background seems to be limited to the height of the container div. here is the HTML/CSS code:
<!doctype html>
<html lang="en">
<head>
<meta charset='UTF-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css">
<title>Bootstrap Issue</title>
<style>
body { background: black; }
.container { background: white; }
</style>
</head>
<body>
<div class="container">
<h1> Hello, World!</h1>
</div>
</body>
</html>
How can I get the body to take up the entire screen?
You need to either add this:
html { background: transparent }
Or, set the "page background" (background: black) on html instead, which is fine to do.
Why? Inside Bootstrap, there's this:
html,body{background-color:#ffffff;}
(bear in mind the default background value is transparent)
For more information on why this matters, see: What is the difference between applying css rules to html compared to body?
Note that this is no longer an issue with Bootstrap 3+.
Set the height of html and body to be 100% in the CSS.
html, body { height: 100%; }
Then it should work. The problem is that the height of body is automatically calculated to be the height of the contents, rather than the height of the whole screen.
/* here is a pure CSS solution */
<style type="text/css">
html, body {
height: 100%;
width: 100%;
padding: 0;
margin: 0;
}
#full-screen-background-image {
z-index: -999;
min-height: 100%;
min-width: 1024px;
width: 100%;
height: auto;
position: fixed;
top: 0;
left: 0;
}
#wrapper {
position: relative;
width: 800px;
min-height: 400px;
margin: 100px auto;
color: #333;
}
a:link, a:visited, a:hover {
color: #333;
font-style: italic;
}
a.to-top:link,
a.to-top:visited,
a.to-top:hover {
margin-top: 1000px;
display: block;
font-weight: bold;
padding-bottom: 30px;
font-size: 30px;
}
</style>
<body>
<img src="/background.jpg" id="full-screen-background-image" />
<div id="wrapper">
<p>Content goes here...</p>
</div>
</body>
<style>
body { background: url(background.png); }
.container { background: ; }
</style>
this works for a background image if you want it
best solution would be
.content{
background-color:red;
height: 100%;
min-height: 100vh;
}
its automatically take veiwport height(vh) in bootstrap.

how can I config css style about fixing height?

I have some problem about css layout.
I wrote the code like below:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
</head>
<style>
html { height:100%; margin: 0px; padding: 0px; }
body {
height: 100%;
padding: 0px;
margin: 0px;
font-family: Verdana, helvetica, arial, sans-serif;
font-size: 68.75%;
background: #fff;
color: #333;
}
#header {
position: absolute;
width:100%;
background: #c0c0c0;
height: 100px;
}
#wrapper {
height: 100%;
width: 100%;
}
#content {
position: relative;
margin-left: 370px;
background: #ffdab9;
height: 100%;
}
#sidebar {
position: absolute;
background: #eee8aa;
width: 370px;
height: 100%;
}
</style>
<body>
<div id="header">header</div>
<div id="wrapper">
<div id="sidebar">sidebar</div>
<div id="content">content</div>
</div>
</body>
</html>
then, the thing what I want to do is
"header"'s height is 100 pixel.
"sidebar(left side)"'s width id 370 pixel.
"content(right side)"'s width is relative.
When I control the browser smaller, I do not want browser to make "scroll bar".
like http://maps.google.com. google maps never make scroll bar when any resizing browser.
num.4 is most important that I told.
If the goal is made it, my code can be fixed all. Please give me any help.
I think you need two css changes (also see my jsfiddle):
add overflow: hidden to body
body {
...
overflow: hidden;
}
change position to relative in #header:
#header {
position: relative;
...
}
=== UPDATE ===
There are (at least) three possible solutions, but none is perfect:
1.) Your example site from google calculates the (content) height at the beginning and after each resize with javascript.
Negative: not css only, you need a script.
2.) Add the height of the header (here 100px) to the css bottom definition of all elements in the sidebar and content (see demo2).
Negative: if the header height changes, you have to update all bottom definitions too.
#words {
bottom: 102px;
...
}
3.) Use the css function calc for the #wrapper to calculate the real height (see demo3).
Negative: at the moment it works only with firefox4 (and above) and IE9.
#wrapper {
...
height: calc(100% - 100px);
height: -moz-calc(100% - 100px);
}

Resources