Kin
Kin

Reputation: 4596

How to center in div without text-align?

How to center squares in the wrapper without using text-align:center?
I need this because it also centers all text in squares, but I don't need this.

HTML :

    <div id="wrapper">
        <div id="squares">
            <div class="square">lol</div>
            <div class="square">lol</div>
            <div class="square">lol</div>
            <div class="square">lol</div>
            <div class="square">lol</div>
            <div class="square">lol</div>
            <div class="square">lol</div>
        </div>
    </div>

CSS:

html, body{
    height: 100%;
}
body {
    line-height: 18px;
}

#wrapper{
    width: 960px;
    height: 100%;
    border: 1px solid red;
    margin: 0 auto;
}
.square {
    width:251px;
    height:207px;   
    border: 1px solid #d6d6d6;
    -webkit-box-shadow: 1px 1px 3px rgba(0,0,0,.1);
    -moz-box-shadow: 1px 1px 3px rgba(0,0,0,.1);
    box-shadow: 1px 1px 3px rgba(0,0,0,.1);
    margin: 10px;
    overflow:hidden;
    position:relative;
    background-color:#fff;
    display: inline-block;
    cursor: pointer;
}

Upvotes: 11

Views: 30884

Answers (3)

maceyj2
maceyj2

Reputation: 666

Since it is text inside the squares you are styling you could put an H or P tag around the text and then text-align: left; them.

Upvotes: 0

cck
cck

Reputation: 174

#wrapper{
    ...
    text-align: center;
}
.square {
    ...
    text-align: left;
}

---- Another way (I don't recommed!)

How to find 52px: (width of div - (#of squares * width of one square))/ #of squares+1 -> (960-(250*3))/4

#wrapper{ 
... 
padding-left:52px; 
} 
.square {
...
margin: 10px 52px 10px 0;
}

Upvotes: 9

ATOzTOA
ATOzTOA

Reputation: 35940

You can try:

.squares {
    margin-left:auto;
    margin-right:auto;
}

Upvotes: 9

Related Questions