0

i created a maze and i want to center an inside div although i center it with margin: 0 auto; it won't work

(this div shows sad smily face when user enter the wall and lose)

#highlight_lose {
    width: 550px;
    height:550px;
    position: absolute;
    display: none;
    margin: 0 auto;

}

here is the fiddle link:

http://jsfiddle.net/uqcLn/28/

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
alonblack
  • 925
  • 2
  • 13
  • 31

2 Answers2

1

If you're going to use absolute positioning you need to do it like this:

#highlight_lose {
    width: 550px;
    height:550px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -225px 0 0 -225px;
    display: none;

}

Edit: you also need to add position:relative; to the main div. Here is an updated fiddle.

http://jsfiddle.net/FragJ/2/

It looks off because you have other elements that aren't exactly centered.

EDIT: As I stated earlier, the smiley didn't look centered because your code is off. The maze really should be inside a div itself. However I was able to eyeball center it simply by playing with the margins.

http://jsfiddle.net/FragJ/4/

To achieve this you'll need to set your css like this:

#main {
    position: relative;
    width: 550px;
    height: 550px;
    float: left;
    margin-left: 220px;
    margin-top: 100px;
    background: grey;
    overflow: hidden;
}

#highlight_win {
    width: 550px;
    height: 550px;
    position: absolute;
    top: 50%;
    left: 50%;
    display: none;
    margin: -180px 0 0 -180px;
}

#highlight_lose {
    width: 550px;
    height:550px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin: -180px 0 0 -180px;
    display: none;
}
aaronmallen
  • 1,418
  • 1
  • 12
  • 29
0
.outer {
            height: 600px;
            width: 500px;
            background-color: black;
        }

        .inner {
            height: 200px;
            width: 200px;
            margin: auto;
            position: relative;
            top: 200px;
            background-color: red;
        }

markup

<div class="outer">

        <div class="inner">

        </div>

    </div>

The idea is for fixed sized block elements, setting

margin:auto;

fixes horizontal centering

for vertical central alignment the child's top = half the height of the parent - half the height of the child

Arun Aravind
  • 1,238
  • 10
  • 9