首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将一个div放在另一个div中?

如何将一个div放在另一个div中?
EN

Stack Overflow用户
提问于 2013-03-13 11:38:00
回答 22查看 458.6K关注 0票数 165

我认为#container将以#main_content为中心。然而,事实并非如此。为什么这不起作用,我该如何修复它?

#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;
}
<div id="main_content">
  <div id="container">
  </div>
</div>

EN

回答 22

Stack Overflow用户

发布于 2015-06-18 04:42:45

另一种有趣的方式:fiddle

CSS

.container {
   background: yellow;
   width: 100%;
   display: flex;
   flex-direction: row;
   flex-wrap: wrap;
   justify-content: center;
   align-items: center;
}

.centered-div {
   width: 80%;
   height: 190px;
   margin: 10px;
   padding: 5px;
   background: blue;
   color: white;
}

HTML

    <div class="container">
        <div class="centered-div">
            <b>Enjoy</b>
        </div>
    </div>
票数 35
EN

Stack Overflow用户

发布于 2013-03-22 20:36:19

你可以用下面的小代码将浮动div居中:

#div {
    width: 200px;
    height: 200px;
    position: absolute;
    left: 50%;
    top: 50%;
    
    margin-top: -100px;
    margin-left: -100px;
}
票数 14
EN

Stack Overflow用户

发布于 2013-03-13 11:55:48

尝试添加

text-align: center;

添加到父容器CSS声明中。并将以下内容添加到子容器:

display: inline-block;

它必须做到这一点。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15376634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档