8

I want a div which is centered vertically and horizontally i.e. in the centre of a page. I tried position: absolute and making the div's top right bottom left 0! But the problem is when i zoom in the page it overlaps with other header and other divs! PLEASE HELP ME! How can i position the div at centre of a page without overlapping other divs while zooming in the page?

Like i have tried:

.center{
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      margin: auto;
 }
Fred
  • 147
  • 1
  • 3
  • 9

7 Answers7

7

Try,

html{
  height:100%;
}
body
{ height:100%;
  background-color: #fcfcfc;

}
.center
{
  position: absolute;
  margin: auto;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 100px;
  height: 100px;
  background-color: #ccc;
  border-radius: 3px;
}
<div class="center"></div>
Hash
  • 7,726
  • 9
  • 34
  • 53
4

html, body {
  height:100%;
}

.center {
  border: 2px solid blue;
  margin: auto;
  position: relative;
  text-align: center;
  top: 50%;
  width: 20%;  
}
<div class="center">
  <p>Test Text</p>
</div>
terryeah
  • 583
  • 5
  • 17
2

To horizontally center a block element (like div), use margin: auto

.center {
    margin: auto;
    width: 60%;
    border: 3px solid #73AD21;
    padding: 10px;
}
<div class="center">
  <p><b>Note: </b>Using margin:auto will not work in IE8, unless a !DOCTYPE is declared.</p>
</div>
shiva krishna
  • 222
  • 2
  • 11
2

Flex is the best solution, perfect position.

If you want this for a loader, just do a full size div with position fixed and use flex for the div content.

Flex guide https://css-tricks.com/snippets/css/a-guide-to-flexbox/

body, html {
  height:100%
}

body {
  display:flex;
  margin:0;
  flex-direction:column;
}

.mydiv {
  margin:auto;
}
<div class="mydiv">test</div>
EyWN
  • 137
  • 7
  • I also think flex is the best solution for this. Just beware of some old browsers - it might need another solution for them. You probably shouldn't use flex on the body to avoid influencing all the other elements on your page. Instead, use it on a wrapper-div surrounding your to-be-centered div. Like
    test
    Give the wrapper 100% width and height and position fixed or absolute, as you prefer.
    – Ina Jul 20 '17 at 06:18
  • That still doesn't work! The div just overlaps with other elements while zooming the page! I want to fix it in its position – Fred Jul 20 '17 at 11:42
1

Try This

HTML

<div class="center">
  Lorem ipsum dolor sit amet, 
</div>

CSS

html,body{
  height:100%;
}
.center{
  position: relative;
  top:50%;
  left:50%; 
}

Link for reference

Hope this Helps..

Chandra Shekhar
  • 3,550
  • 2
  • 14
  • 22
1

2020.4.23
coping from real project

    html, body {
        display:flex;
        height: 100%;
        width: 100%;
        margin: 0;
    }


<div id="id01" style="width: 50%;height: 30%;background-color: red; margin: auto;">
</div>
CodeToLife
  • 3,672
  • 2
  • 41
  • 29
0

This is Your HTML Body
<div class="testbox"> <!-- Your div body --> </div>

This is Your CSS
.testbox { width: 800px; margin: 0 auto; position: absolute; top: 0; bottom: 0; left: 0; right: 0; }

Ajay
  • 917
  • 3
  • 12
  • 26