I’m building a website for Advanced Web Design and I’m stuck on something. I want the content of the page (contained in a <div>) to be centered. Using CSS I tried this:
body {text-align: center; margin-left: auto; margin-right:auto;}
That didn’t seem to work. I know you can detect a browsers resolution using Javascript, and I got to thinking. How would I detect the width, and use that to set the left and right margins of the body? It would be ((resolutionWidth - 800) / 2) to determine the margins that I need (the <div> is 800px wide).
margin: 0 autois one of the easiest ways to center content on a website. Don’t use JS to center the content unless you absolutely have to.Check out this jsFiddle on how to use
margin: 0 auto: http://jsfiddle.net/NfRtV/