1

I am building a two column layout and I want to keep the total column inside a wrapper in the center of the screen even if it's resized. I have tried floating some CSS divs but not helping.

This is my layout:

<div class="wrapper"><div class="leftCol">Left</div><div class="rightCol">Right</div></div>

CSS:

.wrapper{ width:720px; text-align:centre;} 
.leftCol{ width:200px; float:left;} 
.rightCol{ width:510px; float:right;}
DASH
  • 181
  • 1
  • 2
  • 10
  • 2
    I believe your answer is here sir. http://stackoverflow.com/questions/953918/how-to-align-a-div-to-the-middle-horizontally-width-of-the-page – Ryan G Mar 12 '16 at 05:34

4 Answers4

2

All you need to do is set the wrapper as such

CSS:

.wrapper { width:720px; margin:0px auto;}

Furthermore I would recommend using a bit more responsive CSS with percentage to fit with the width as well, so that it doesn't look really tiny on larger screens.

0

You can try the margin trick here on the wrapper class.

.wrapper{ width:720px; margin: 0 auto; } 
TeaCoder
  • 1,958
  • 13
  • 13
0

Float removes your children DIV elements from a static document flow.
DIV elements being block-level elements, unless set as inline or inline-block will not in any case apply to the parent's text-align property.

You have more possibilities, the most common are:

.wrapper{
    width: 720;
    margin: 0 auto; /* keeps me centered */
    overflow: auto; /* to contain floated child elements.
                    Or use a .clearfix  http://nicolasgallagher.com/micro-clearfix-hack/ */
}

or using text-align: center;

.wrapper{
    width: 720;
    margin: 0 auto; /* keeps me centered (use anyways) */
    text-align: center; /* my children are not block-level elements */
}
.leftCol{
    display:inline-block;
    width:200px;
    /* text-align: left; probably you might want to reset text alignment */
} 
.rightCol{
    display: inline-block;
    width:510px;
}

I would discourage you from using (in 2016.+) fixed widths. Use another more responsive unit like % or vw

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
0

Please try below css

.wrapper{ width:720px; text-align:centre; margin: 0 auto;} 
.leftCol{ width: 30%;  margin: 0 auto; display: inline-block;} 
.rightCol{ width: 70%; margin: 0 auto; display: inline-block; }
Dhaval Patel
  • 1,076
  • 6
  • 19