Copy link to clipboard
Copied
Anyone can help?
Should I use relative or absolute for position?
Copy link to clipboard
Copied
What is the ultimate goal here?
If you want to have an element hold to the bottom of the screen no matter what and sit on top of any other content, you would use position:fixed...
#bottom_nav {
width:100%;
position:fixed;
bottom:0px;
text-align:center;
z-index:9999;
}
Then in your html...
<div id="bottom_nav">Home | About | Contact | Portfolio</div>
Copy link to clipboard
Copied
But position:fixed won't center. Neither will position:absolute.
Nancy O.
Copy link to clipboard
Copied
Nancy O. wrote:
But position:fixed won't center. Neither will position:absolute.
Nancy O.
That's why I use a 100% width container and then center the content within that using either text-align:center (example above), or a separate <div> with margin:0 auto and a set width within the fixed one.
Copy link to clipboard
Copied
Actually I got 2 divs. Div 1 is the main content already in the right position, which is center of the page. Div2 is a navagation bar with text and background color. I want to position div2 to the center bottom and keep 10px margin-top to div1. Even I resize my browser window, I want div2 to remain 10px margin-top with div1, not overlay it, and the width of div2 expand itself depends on the window size. And I want the text in div2 remain in bottom of the page but not shift to left or right when I resize the browser windows. It would be greatful if anyone can help, thank you!
Copy link to clipboard
Copied
Fixed wouldn't be the right tool for this. Actually, any positioning would probably be wrong here. It sounds like you're describing the default behaviour of two centered <div> tags that are adjacent to each other in the html. If you are using any positioning other than the default "no positioning" (static) it will throw that off.
Could you post a link to your page so we can see what's happening?
Copy link to clipboard
Copied
https://drive.google.com/file/d/0B0kD2070M7JPOEtCb0NhYnFkQlE/edit?usp=sharing
Here you go, the red one is the div1.
Copy link to clipboard
Copied
I would simplify. Is this what you're going for (copy into a new blank document)?
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
#topone, #bottomone {
width:100%;
min-height:50px;
background-color:gray;
}
#middleone {
width:900px;
margin:0 auto;
background-color:red;
min-height:500px;
}
#bottomone {
margin-top:10px;
}
#topinfo, #bottominfo {
width:900px;
margin:0 auto;
}
</style>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div id="topone"><div id="topinfo">you can add another div here with the same width as the middleone and margin:0 auto to center it at the same rate as #middleone</div></div>
<div id="middleone"></div>
<div id="bottomone"><div id="bottominfo">you can add another div here with the same width as the middleone and margin:0 auto to center it at the same rate as #middleone</div></div>
</body>
</html>
Copy link to clipboard
Copied
Yeah, mostly! I just want to bottomdiv to stick with the bottom even I resize my browser window. So the margin between the content div and the bottom div will changed depends the size of my browser window. But the margin between these 2 divs have to remain at least 10px if I smaller my browser size. The bottom div need to stick in the bottom whatever I do.
Copy link to clipboard
Copied
For best cross browser rending, simply build your site logically from top to bottom & everything will fall into place. BTW, no positioning is required for any of this.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Document, 1-Col</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<style>
* {
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
img {
max-width: 100%;
vertical-align: baseline
}
body {
padding: 0;
width: 90%; /**adjust width in px or % as required**/
margin: 0 auto; /**this is centered**/
background: #F5DD83;
color: #2294AE;
font-family: Segoe, "Segoe UI", "DejaVu Sans", "Trebuchet MS", Verdana, sans-serif;
font-size: 100%;
box-shadow: 2px 2px 4px #333;
}
header {
margin: 0;
padding: 0 1%;
width: 100%;
background: #B00202;
color: #FFF;
}
header h1, header h2 {
display: inline;
color: #F5DD83;
padding: 0 3%;
}
article {
padding: 2%;
background: #FFF;
}
nav { background: #69C; }
footer {
background: #B00202;
color: #FFF;
text-align: center;
margin: 0;
}
</style>
</head>
<body>
<!--begin header-->
<header>
<h1>Sitename</h1> <h2>Some pithy slogan...</h2>
</header>
<!--begin main content-->
<article>
<h3>Heading 3</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vitae libero lacus, vel hendrerit nisi! Maecenas quis velit nisl, volutpat viverra felis. Vestibulum luctus mauris sed sem dapibus luctus. Pellentesque aliquet aliquet ligula, et sagittis justo auctor varius. Quisque varius scelerisque nunc eget rhoncus. Aenean tristique enim ut ante dignissim. </p>
<h3>Heading 3</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris vitae libero lacus, vel hendrerit nisi! Maecenas quis velit nisl, volutpat viverra felis. Vestibulum luctus mauris sed sem dapibus luctus. Pellentesque aliquet aliquet ligula, et sagittis justo auctor varius. Quisque varius scelerisque nunc eget rhoncus. Aenean tristique enim ut ante dignissim. </p>
</article>
<!--begin footer-->
<footer>
<!--begin navigation-->
<nav>Horizontal menu goes here...</nav>
<small>© 2014 Your Site Name. All rights reserved</small> </footer>
</body>
</html>
This is what it looks like in browsers:
Nancy O.
Copy link to clipboard
Copied
I see what you mean, Nancy thank you. But how to make that footer stick to the bottom of the browser. In your screen there is a yellow gap between the bottom and the footer/navagation. How to remove that gap and make the footer stick in the bottem so that there will the a margin between the article ant the footer. And that margin will increase or decrease depends on the size of the browser window.
Copy link to clipboard
Copied
For that you would need Fixed Positioning.
Example:
http://alt-web.com/TEMPLATES/FixedLayout.shtml
Tutorial:
http://alt-web.com/DEMOS/CSS2-Sticky-Footer.shtml
I do not advocate the use of hacks that force the footer to the bottom of viewport because it always leads to other more serious problems. The majority of people don't care if the footer butts up with the bottom of their viewport or not. If it's a big issue to you, add more content to your pages.
Nancy O.
Copy link to clipboard
Copied
How important are IE8 and lower to you?
EDIT: I know of a secret squirrel way to make it work without position:fixed (without any position attributes actually), but it won't do it in IE8 and lower (it will display more like my first code, 3 bars with white space under the third). You're also stuck keeping the content of the page within a set pixel height to avoid odd overflow issues.
Copy link to clipboard
Copied
Here's the code I mentioned. It uses vh units, so old browsers dislike it, but if you don't care about them (I don't) and you don't plan to go past 600 pixels in height for your content (right now it's set to 400 min-height for both middle ids) this should work...
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test with VH units</title>
<style>
body {
margin:0;
}
#topone {
width:100%;
height:9vh;
min-height:50px;
background-color:gray;
}
#middleone {
width:900px;
margin:0 auto;
height:81vh;
min-height:400px;
}
#middle_content {
min-height:400px;
background-color:red;
overflow:hidden;
}
#bottomone {
margin-top:10px;
margin-top:1vh;
height:9vh;
min-height:50px;
width:100%;
background-color: gray;
}
#topinfo, #bottominfo {
width: 900px;
margin: 0 auto;
}
</style>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div id="topone">
<div id="topinfo">you can add another div here with the same width as the middleone and margin:0 auto to center it at the same rate as #middleone</div>
</div>
<div id="middleone">
<div id="middle_content">
Your main content here
</div>
</div>
<div id="bottomone">
<div id="bottominfo">you can add another div here with the same width as the middleone and margin:0 auto to center it at the same rate as #middleone</div>
</div>
</body>
</html>
Copy link to clipboard
Copied
@Jon, I've seen Viewport Units successfully used for Typography. But never quite this way before.
@hinc94, Browser support for Viewport Units is still dicey:
http://caniuse.com/viewport-units
My Android Jelly Bean 4.1 hates it
Nancy O.
Copy link to clipboard
Copied
Thank you Nancy.
Copy link to clipboard
Copied
Nancy O. wrote:
@Jon, I've seen Viewport Units successfully used for Typography. But never quite this way before.
I've been playing around with it a lot lately, just getting ready for the day when everything supports it. There are a ton of interesting things you can do with vh, vw, vmax and vmin settings on elements.
Copy link to clipboard
Copied
Thank you very much Jon! That vh units really helpful! Just 1 more question, there is a gap between the bottom div and the browser window, if the browser window size is larger, that gap is getting larger as well, how to remove that gap?
Copy link to clipboard
Copied
hinc94 wrote:
Thank you very much Jon! That vh units really helpful! Just 1 more question, there is a gap between the bottom div and the browser window, if the browser window size is larger, that gap is getting larger as well, how to remove that gap?
There shouldn't be a gap below the bottom gray bar, no matter what your viewport size is.
What browser and version are you using?
If you are using a modern browser, Is the gap you see the bottom scrollbar?
Copy link to clipboard
Copied
In Safari 7
There is no gap if Im not viewing it in full screen
If I switch to full screen mode in safari 7, there is a white gap between the bottom div and the bottom of the screen, so I assume the gap expands depends on the size of the browser window. Therefore, if someone uses a larger monitor such as 27 inch to view my website, the gap would probably getting much larger.
Copy link to clipboard
Copied
This might be one of the limitations of using the vh unit in that particular browser. What happens to the gap if you refresh the browser after resizing it?
Copy link to clipboard
Copied
Still remain. It looks worst if I view it in google chrome 33
Copy link to clipboard
Copied
Hmm, the code I posted works correctly here in Firefox 28, Chrome 33, IE9 & 10 and Opera 20 on my PC.
Maybe a setting got changed by accident?
Could you paste your code (or upload the page to a web server you control) so I can take a look?
Copy link to clipboard
Copied
I tried to copy your code and paste it without any changes, and view it in chrome 33 and here is how it looks like
Copy link to clipboard
Copied
Bummer. Like I mentioned, support isn't 100% across the board for the vh unit.
Looks like the Mac version of Chrome doesn't like it.
This is how it looks on the PC version...