0

I have searched on this site for about 35 minutes, and I am sure it is a really simple answer and I am probably over-looking it, but I am having trouble figuring out how to center my layout.

It is a simple layout, it has a container div, left div for the menu, and right div for the content. (Here is the CSS for this so far..)

#container {
  text-align:center; 
  width: 960px; 
  background-color:#FFFFFF; 
  margin-right:10px; 
  margin-left:10px; 
  display:block;
}
#left {
  text-align:; 
  width:300px; 
  display:block; 
  background-color:#FFFFFF; 
  padding-left:5px; 
  padding-right:5px;
}
#right {
  text-align:left; 
  padding-left:10px; 
  padding-right:10px; 
  width:620px; 
  display:block;
}

When I save it and open in my browser, the layout is just sitting to the left. What can I add to make it go to the center?

I have tried a few things from other peoples' threads and nothing is working. What am I doing wrong?

Connor Tumbleson
  • 3,270
  • 2
  • 25
  • 37

5 Answers5

0

There is a nice trick using margin: 0 auto.
Check out this site or this site for a good explanations of the subject.

EZLearner
  • 1,614
  • 16
  • 25
0

Put those 3 divs inside a div with id 'content', then declare a CSS rule something like this:

#content {
   width: <width in px>;
   margin-left: auto;
   margin-right: auto;
}

Everything inside the 'content' div - i.e. your layout - will then appear in the center of the page.

The auto value of the margins is what centers the div.

davnicwil
  • 28,487
  • 16
  • 107
  • 123
0
#container {
    width: 960px; 
    background-color:#FFFFFF; 
    margin:0 auto;
}
#container:after{
    content:'';
    display:block;
    clear:both;
}
#left {
    width:300px; 
    float:left;
    padding:0 5px;
}
#right {
    padding:0 10px;
    width:620px; 
    float:right;
}
Anon
  • 2,854
  • 14
  • 18
0

Try giving those columns a float.

EG

#left {
  float:left;
}
#right {
  float:right;
}

or if you would like the to sit flush up against each other float them both left!

http://codepen.io/alexbaulch/pen/hzwnE

Alex Baulch
  • 751
  • 1
  • 11
  • 24
0

At the heart of this is margin: 0 auto; on the parent div.

You will save yourself a lot of time and pain if you look up box-sizing: border-box; and you start using percentages and ems instead of px. Trust me. I wouldn't hire someone who had pixels on anything but the occasional border. Plus - it's WAY easier. I would also suggest only using id's (#) for javascript and super special situations. Sticking with .class is the consensus right now. Good Luck!

http://jsfiddle.net/sheriffderek/CN8Ev/

HTML

<div class="container">

    <div class="main">main</div>

    <div class="sub">sub</div>

</div> <!-- .container -->

CSS

*, *:before, *:after {
    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    /* google border box - moves padding to the inside of the box */ 
}

.container {
    width: 90%;
    max-width: 60em;
    margin: 0 auto; /* no margin on the top and bottom - auto margin on the sides */
    overflow: hidden; /* shame - google micro clear fix instead */
}

.main {
    width: 70%;
    float: left;
}

.sub {
    width: 30%;
    float: left;
}
sheriffderek
  • 8,848
  • 6
  • 43
  • 70