Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center two columns using CSS?

Tags:

html

css

I'm trying to center two columns on my website but there are some problems. The result of every change is left position (see picture). What am I doing wrong? Here's my CSS:

body {
    background - image: url("../img/gray.png");
}

#header {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 100px;
    background: #cc0000;
    text - align: center;
}

#wrapper {
    margin: 100px;
    width: 1280px;
}

#leftcolumn {
    float: left;
    background - image: url("../img/gray.png");
    margin: 10px 0px 10px 0px;
    padding: 10px;
    height: 682px;
    width: 460px;
}

#rightcolumn {
    float: left;
    color: #333;
    border: 1px solid # ccc;
    background: #F2F2E6;
    margin: 10px 0px 10px 0px;
    padding: 10px;
    height: 500px;
    width: 439px;
    display: inline;
    position: relative;
}

Thanks

like image 628
Fastkowy Avatar asked Feb 11 '13 15:02

Fastkowy


People also ask

How do I center align a column in CSS?

Centering Items within a CSS Grid Column To center the items within a column, we use align-items and justify-items instead of the align-content and justify-content in our previous example. Using the item properties, we can align our items just like we did with our columns.

How do I center a column in HTML?

To center align text in table cells, use the CSS property text-align.


2 Answers

Since the two columns' width is less than the width of the wrapper (i.e. 959px vs 1280px), you'll need to place the two columns inside a fixed width container:

<div id="wrapper">
    <div id="column_container">
        <div id="column1"></div>
        <div id="column2"></div>
    </div>
</div>

And then do something like:

#column_container {
    width: 959px;
    margin: 0 auto;
}
like image 167
BenM Avatar answered Oct 25 '22 12:10

BenM


Without seeing it live, something like this should work:

#wrapper { 
   width: 1280px;
   margin:100px auto ;
}

But as has been mentioned, this will only center #wrapper. The columns don't fill the entire 1280 width so they are still left aligned in #wrapper.

like image 20
gotohales Avatar answered Oct 25 '22 13:10

gotohales