最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to center div? - Stack Overflow

programmeradmin6浏览0评论

I have a problem with centering div in HTML (vertical & horizontal). My code looks something like this:

<div id="container">SOME HTML</div>

#container{
    width: 366px;
    height: 274px;
    margin: 50%;
    top: -137px;
    left: -188px;
    position:absolute;
}

Only chrome center this div in to the middle of the screen.

I have a problem with centering div in HTML (vertical & horizontal). My code looks something like this:

<div id="container">SOME HTML</div>

#container{
    width: 366px;
    height: 274px;
    margin: 50%;
    top: -137px;
    left: -188px;
    position:absolute;
}

Only chrome center this div in to the middle of the screen.

Share Improve this question asked Apr 24, 2012 at 15:38 WhatWhat 831 gold badge2 silver badges6 bronze badges 1
  • 4 I may be missing something, but how are you expecting to centre a div if you're using absolute positioning? Are you assuming that all screens have the same size and resolution? – Aleks G Commented Apr 24, 2012 at 15:41
Add a ment  | 

6 Answers 6

Reset to default 7

This will center the <div> horizontally:

#container{
    width: 366px;
    height: 274px;
    margin: 0 auto;
}

Centering vertically is not quite simple, you maybe have to use javascript for that, or you try this css solution.

#container{
    width: 366px;
    height: 274px;
    top: 50%;
    left: 50%;
    margin: -137px 0 0 -188px;
    position:absolute;
}

This does the trick (vertical & horizontal):

#container{
    position: absolute;
    width: 366px;
    height: 274px;
    left: 50%;
    top: 50%;
    margin-left: -183px; /* half width */
    margin-top: -137px; /* half height */
}

You could use:

#container {
    // Your other values, but remove position: absolute;
    margin: 0 auto;
}

Alternatively, you can do:

#wrapper, #container {
    border: 1px solid red;
    height: 500px;
    width: 600px;
}

#wrapper {
    bottom: 50%;
    right: 50%;
    position: absolute;
}

#container {
    background: yellow;
    left: 50%;
    padding: 10px;
    position: relative;
    top: 50%;
}

And you're HTML code:

<div id="wrapper">
    <div id="container">
        <h1>Centered Div</h1>
        <p>
            This div has been centered within your browser window.</p>
    </div>
</div>

That will center the <div> in the middle of the browser window.

Try this one:

<div class="cont">
  <div class="box"></div>
</div>

Css:

.cont{
  background-color: tomato;
  width: 600px;
  height: 400px;
  position: relative;
}
.box {
  width:100px;
  height:100px;
  background-color: teal;
  color:#fff;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%)
}

Should be fine to use just CSS:

here is the demo

#container{
    width: 366px;
    height: 274px;
    margin: 50%;
    top: 50%;
    left: 50%;
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
}​
发布评论

评论列表(0)

  1. 暂无评论