Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center in div without text-align?

Tags:

html

css

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;
}
like image 595
Kin Avatar asked Dec 25 '12 15:12

Kin


People also ask

How do I center content in a div?

Center Align Elements To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do I center a div without margins?

use text-align:center for div -> this will align text inside div center. include width property in div (i.e. width:200px) and it will work fine.

How do I center a div in HTML without CSS?

Without setting an explicit width, the <div> tag will automatically expand to 100% of the width of its parent. Therefore, setting margin: 0 auto; will make it center -- with 0px on both the left and right. Save this answer.

How do I center a div without the width?

If the target element is absolutely positioned, you can center it by moving it 50% in one direction ( left: 50% ) and then transforming it 50% in the opposition direction ( transform:translateX(-50%) ). This works without defining the target element's width (or with width:auto ).


2 Answers

#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;
}
like image 75
cck Avatar answered Oct 14 '22 06:10

cck


You can try:

.squares {
    margin-left:auto;
    margin-right:auto;
}
like image 25
ATOzTOA Avatar answered Oct 14 '22 04:10

ATOzTOA