How can I center a div within another div? [duplic

2020-01-27 10:27发布

I've got an problem. I have the html code like this:

<div id="main_content" >
    <div id="container">
    </div>
</div>

The css like this:

#main_content {
    top: 160px;
    left: 160px;
    width: 800px;
    min-height: 500px;
    height: auto;
    background-color: #2185C5;
    position: relative;
}

#container {
    width: auto;
    height: auto;
    margin: 0 auto;
    padding: 10px;
    position: relative;
}

I suppose that the #container will be centered within #main_content. However, it is not. I just want to find out the reason and how to make it centered.

标签: html css
27条回答
劳资没心,怎么记你
2楼-- · 2020-01-27 11:04
* {
  box-sizing: border-box;
}
#main_content {
    top: 160px;
    left: 160px;
    width: 800px;
    min-height: 500px;
    height: auto;
    background-color: #2185C5;
    position: relative;
}

#container {
    width: 50%;
    height: 50%;
    margin: auto;
    padding: 10px;
    position: absolute;
    border: 5px solid yellow;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}  
查看更多
ゆ 、 Hurt°
3楼-- · 2020-01-27 11:09

Try to add

text-align: center;

to your parent container css declaration. And following to child container:

display: inline-block;

It must do the trick.

查看更多
够拽才男人
4楼-- · 2020-01-27 11:11

Here is a new way to easily center your div using flex display.

See this working fiddle: https://jsfiddle.net/5u0y5qL2/

Here is the css :

.layout-row{
box-sizing: border-box;
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  -webkit-box-direction: normal;
  -webkit-box-orient: horizontal;
  -webkit-flex-direction: row;
  flex-direction: row;
}

.layout-align-center-center{
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-grid-row-align: center;
  align-items: center;
  -webkit-align-content: center;
  align-content: center;
      -webkit-box-pack: center;
    -webkit-justify-content: center;
    justify-content: center;
}
查看更多
一纸荒年 Trace。
5楼-- · 2020-01-27 11:12

here is the solution

 #main_content {
background-color: #2185C5;
height: auto;
margin: 0 auto;
min-height: 500px;
position: relative;
width: 800px;
 }
查看更多
劫难
6楼-- · 2020-01-27 11:12

to make a div in center. no need to assign width of the div.

working demo Here..

http://jsfiddle.net/6yukdmwq/

.container{width:100%; height:500px;display:table;border:1px solid red;text-align:center;}
.center{display:table-cell;vertical-align:middle;}
.content{display:inline-block;text-align:center; border:1px solid green;}



<section class="container">
    <div class="center">
        <div class="content">
            <h1>Helllo Center Text</h1>
        </div>
    </div>
</section>
查看更多
趁早两清
7楼-- · 2020-01-27 11:16

May be you want as this: Demo

html...

<div id="main_content">
<div id="container">vertical aligned text<br />some more text here
</div>
</div>

css..

#main_content{
width: 500px; 
height: 500px; 
background: #2185C5; 
display: table-cell; 
text-align: center; 
vertical-align: middle; 
}
#container{
width: auto; 
height: auto; 
background: white; 
display: inline-block; 
padding: 10px;
}

How? >>> In a table cell vertical align with middle will set to vertically centered to the element and text-align: center; works as horizontal alignment to the element. Noticed why is #container is in inline-block coz this is in condition of row. Any question?

查看更多
登录 后发表回答